Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: can I have the last key of a multi-key binding repeat the command?

Tags:

vim

I want to do window resizing in vim similarly to how I do it in tmux.

With my tmux bindings (of the form bind -r J resize-pane -D 5, where the -r is for repeat) I can do <C-b>JJJ, which is the same number of keys and effect as vim's 15<C-w>+, but much less thinking because I can just move the split until it looks right, rather than deciding a number of rows/cols in advance.

Another example: I want to be able to press <C-w>++++--, and have it perform the same action as pressing <C-w>+<C-w>+<C-w>+<C-w>+<C-w>-<C-w>-. (Although I'd probably want to remap it again to do 5 lines/cols per move instead of 1).

Is this kind of repeat/timeout mode possible in vim bindings?

like image 945
Chris Berkhout Avatar asked May 17 '13 01:05

Chris Berkhout


1 Answers

That's a tricky one, but I'll share my method of handling this, as I think it's even easier than the <C-w> methods (which I used for a long time, and found bothersome):

" simplify resizing splits
if has('unix')
    nnoremap ^[j <C-w>-
    nnoremap ^[k <C-w>+
    nnoremap ^[h <C-w><
    nnoremap ^[l <C-w>>
else
    nnoremap <M-j> <C-w>-
    nnoremap <M-k> <C-w>+
    nnoremap <M-h> <C-w><
    nnoremap <M-l> <C-w>>
endif

The ^[ characters must be replaced by an actual escape. Create it in insert mode by pressing Ctrl+v, then escape. This was required to use meta+x in bash on Linux. Now in both OSes I can just hold Alt and the hjkl keys to resize splits, including holding down, say, Alt+h to horizontally shrink the split I'm in. They also work with counts.

like image 172
Gary Fixler Avatar answered Sep 19 '22 20:09

Gary Fixler