From 6f14c7cfc4c0ec8b775c3a8de432f3b092e15c54 Mon Sep 17 00:00:00 2001 From: Luke Johnson Date: Tue, 28 Feb 2023 14:16:15 -0700 Subject: [PATCH] add LazyGit terminal, create terminal.lua --- lua/custom/plugins/init.lua | 21 ----------- lua/custom/plugins/terminal.lua | 67 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 lua/custom/plugins/terminal.lua diff --git a/lua/custom/plugins/init.lua b/lua/custom/plugins/init.lua index ef246de3..24ce776c 100644 --- a/lua/custom/plugins/init.lua +++ b/lua/custom/plugins/init.lua @@ -77,25 +77,4 @@ return { { "ul", "UrlView", desc = "View buffer URLs" }, }, }, - - -- Toggleterm: multiple terminals - { 'akinsho/toggleterm.nvim', - version = "*", - config = function() - require("toggleterm").setup { - insert_mappings = true, -- whether or not the open mapping applies in insert mode - size = 10, - open_mapping = [[]], - shading_factor = 2, - direction = "float", - float_opts = { - border = "curved", - highlights = { - border = "Normal", - background = "Normal", - }, - }, - } - end, - }, } diff --git a/lua/custom/plugins/terminal.lua b/lua/custom/plugins/terminal.lua new file mode 100644 index 00000000..2e556edb --- /dev/null +++ b/lua/custom/plugins/terminal.lua @@ -0,0 +1,67 @@ +return { + -- Toggleterm: multiple terminals + { + 'akinsho/toggleterm.nvim', + version = "*", + config = function() + require("toggleterm").setup { + -- size can be a number or function which is passed the current terminal + size = 10, + autochdir = true, -- when neovim changes it current directory the terminal will change it's own when next it's opened + start_in_insert = true, + insert_mappings = true, -- whether or not the open mapping applies in insert mode + terminal_mappings = true, -- whether or not the open mapping applies in the opened terminals + persist_size = true, + persist_mode = true, -- if set to true (default) the previous terminal mode will be remembered + direction = 'horizontal', + close_on_exit = true, -- close the terminal window when the process exits + shell = vim.o.shell, -- change the default shell + auto_scroll = true, -- automatically scroll to the bottom on terminal output + open_mapping = [[]], + shading_factor = 2, + } + + -- Set Terminal Keymaps + function _G.set_terminal_keymaps() + local opts = { buffer = 0 } + vim.keymap.set('t', '', [[]], opts) + vim.keymap.set('t', 'jk', [[]], opts) + vim.keymap.set('t', '', [[wincmd h]], opts) + vim.keymap.set('t', '', [[wincmd j]], opts) + vim.keymap.set('t', '', [[wincmd k]], opts) + vim.keymap.set('t', '', [[wincmd l]], opts) + vim.keymap.set('t', '', [[]], opts) + end + + -- if you only want these mappings for toggle term use term://*toggleterm#* instead + vim.cmd('autocmd! TermOpen term://* lua set_terminal_keymaps()') + + -- Set config for LazyGit + local Terminal = require('toggleterm.terminal').Terminal + local lazygit = Terminal:new({ + cmd = "lazygit", + dir = "git_dir", + direction = "float", + float_opts = { + border = "double", + }, + -- function to run on opening the terminal + on_open = function(term) + vim.cmd("startinsert!") + vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "close", { noremap = true, silent = true }) + end, + -- function to run on closing the terminal + on_close = function(term) + vim.cmd("startinsert!") + end, + }) + + function _lazygit_toggle() + lazygit:toggle() + end + + vim.api.nvim_set_keymap("n", "g", "lua _lazygit_toggle()", + { noremap = true, silent = true, desc = "LazyGit Toggle" }) + end + } +}