Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 520
Adriano_Pinaffo Avatar asked Apr 27 '26 08:04

Adriano_Pinaffo


2 Answers

Your code wasn't that wrong just needed a little changes. Now you can resize all the panes in both horizontal and vertical way:

:nnoremap <silent> <c-Up> :resize -1<CR>
:nnoremap <silent> <c-Down> :resize +1<CR>
:nnoremap <silent> <c-left> :vertical resize -1<CR>
:nnoremap <silent> <c-right> :vertical resize +1<CR>
like image 81
SdSaati Avatar answered Apr 28 '26 23:04

SdSaati


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>
like image 30
Philippe Avatar answered Apr 28 '26 21:04

Philippe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!