]> git.rmz.io Git - dotfiles.git/commitdiff
vim: consolidate terminal setings in plugin file
authorSamir Benmendil <me@rmz.io>
Sat, 14 Jan 2023 01:12:52 +0000 (01:12 +0000)
committerSamir Benmendil <me@rmz.io>
Sat, 14 Jan 2023 02:35:28 +0000 (02:35 +0000)
Also supports nvim terminal and make it more like normal vim one.

vim/plugin/terminal.vim [new file with mode: 0644]
vim/vimrc

diff --git a/vim/plugin/terminal.vim b/vim/plugin/terminal.vim
new file mode 100644 (file)
index 0000000..f3170d3
--- /dev/null
@@ -0,0 +1,53 @@
+" adapted from https://www.reddit.com/r/vim/comments/op677w/making_neovim_terminal_behave_like_vim_terminal/h642b6b/
+
+function! s:TermEnter(_)
+  if getbufvar(bufnr(), 'term_insert', 0)
+    startinsert
+    call setbufvar(bufnr(), 'term_insert', 0)
+  endif
+endfunction
+
+function! <SID>TermExec(cmd)
+  let b:term_insert = 1
+  execute a:cmd
+endfunction
+
+if !has('nvim')
+  command! -nargs=* T  terminal <args>
+  command! -nargs=* VT vertical terminal <args>
+else
+  command! -nargs=* T  split  | terminal <args>
+  command! -nargs=* VT vsplit | terminal <args>
+
+  tnoremap <silent> <C-W>.     <C-W>
+  tnoremap <silent> <C-W><C-.> <C-W>
+  tnoremap <silent> <C-W>\     <C-\>
+  tnoremap <silent> <C-W><C-\> <C-\>
+  tnoremap <silent> <C-W>N     <C-\><C-N>
+  tnoremap <silent> <C-W>      <C-\><C-N>:call <SID>TermExec('call feedkeys(":")')<CR>
+  tnoremap <silent> <C-W><C-W> <cmd>call <SID>TermExec('wincmd w')<CR>
+  tnoremap <silent> <C-W>W     <cmd>call <SID>TermExec('wincmd W')<CR>
+  tnoremap <silent> <C-L>      <cmd>call <SID>TermExec('wincmd w')<CR>
+  tnoremap <silent> <C-H>      <cmd>call <SID>TermExec('wincmd W')<CR>
+  tnoremap <silent> <C-W>h     <cmd>call <SID>TermExec('wincmd h')<CR>
+  tnoremap <silent> <C-W>j     <cmd>call <SID>TermExec('wincmd j')<CR>
+  tnoremap <silent> <C-W>k     <cmd>call <SID>TermExec('wincmd k')<CR>
+  tnoremap <silent> <C-W>l     <cmd>call <SID>TermExec('wincmd l')<CR>
+  tnoremap <silent> <C-W><C-H> <cmd>call <SID>TermExec('wincmd h')<CR>
+  tnoremap <silent> <C-W><C-J> <cmd>call <SID>TermExec('wincmd j')<CR>
+  tnoremap <silent> <C-W><C-K> <cmd>call <SID>TermExec('wincmd k')<CR>
+  tnoremap <silent> <C-W><C-L> <cmd>call <SID>TermExec('wincmd l')<CR>
+  tnoremap <silent> <C-W>gt    <cmd>call <SID>TermExec('tabn')<CR>
+  tnoremap <silent> <C-W>gT    <cmd>call <SID>TermExec('tabp')<CR>
+endif
+
+augroup terminal
+  au!
+  if !has('nvim')
+    autocmd TerminalOpen * if &buftype == 'terminal' | setlocal bufhidden=hide | endif
+  else
+    autocmd TermOpen * startinsert
+    autocmd TermOpen * if &buftype == 'terminal' | setlocal bufhidden=hide | endif
+    autocmd CmdlineLeave,WinEnter,BufWinEnter * call timer_start(0, function('s:TermEnter'), {})
+  endif
+augroup END
index 0aee7e322b7c176699b4137233ca5fcf0d6f706d..34afc34f514993bdcd0db7b1883eedc5758d503e 100644 (file)
--- a/vim/vimrc
+++ b/vim/vimrc
@@ -418,11 +418,6 @@ augroup END
 command! NoAutoChecktime let b:autochecktime=0
 command! ToggleAutoChecktime let b:autochecktime=!get(b:, 'autochecktime', 0) | echom "b:autochecktime:" b:autochecktime
 
 command! NoAutoChecktime let b:autochecktime=0
 command! ToggleAutoChecktime let b:autochecktime=!get(b:, 'autochecktime', 0) | echom "b:autochecktime:" b:autochecktime
 
-augroup terminal
-  au!
-  au TerminalOpen * if &buftype == 'terminal' | setlocal bufhidden=hide | endif
-augroup END
-
 " bindings {{{1
 
 " allow both <space> and \ to be <leader>
 " bindings {{{1
 
 " allow both <space> and \ to be <leader>