linuxvimsplitterminalremap

vim: shortcut to resize split windows


When I have split windows in Vim, I can resize the windows using :resize +1/-1. I wanted to add a shortcut for it that worked like split windows in terminator. In terminator, if I have two windows on top of each other, CTRL Shift Up / Down moves the separator between the two windows, meaning, if I'm in the top window and press CTRL Shift Down, the top window increases. On the other hand, if I'm in the bottom window, CTRL Shift Down decreases the bottom window. So, it truly moves the separator.

With split windows in vim, I tried to remap like this:

:nnoremap <silent> <c-Up> :resize -1<CR>
:nnoremap <silent> <c-Down> :resize +1<CR>

This works fine if I'm on the top window (pressing CTRL UP decreases the size of the window and CTRL Down increases the size). But when I move to the bottom window it behaves correctly but it has a weird effect (CTRL UP also decreases the size of the window). So, I can't simulate moving the separator.

Is it possible o run a command depending on which window I'm located at?


Solution

  • You can define a function in .vimrc

    function! MoveSeparator(PlusMinus)
        let num=tabpagewinnr(tabpagenr())
        let pm=a:PlusMinus
        if  num == "2"
            let pm = pm == '+' ? '-' : '+'
        end
        exec "resize " . pm . "1"
    endfunction
    
    nnoremap <silent> <C-UP>   :call MoveSeparator("-")<CR>
    nnoremap <silent> <C-DOWN> :call MoveSeparator("+")<CR>