Search code examples
vim

Resize window in vim with a keyboard map


I am trying to set the + and _ keys to increase or decrease the size of a window pane in vim. Here is what I have so far in my .vimrc:

 nnoremap + :res +5
 nnoremap _ :res -5

However it doesn't appear to work. What would be the proper way to map the resize pane in vim? Also, is there a way to press [enter] automatically after entering the command so that it executes automatically?


Solution

  • The :res commands are fine, but you need to append <CR> (for Carriage Return) to the mappings to actually execute them when you press + or _ . So, your mappings should look like this:

    nnoremap + :res +5<CR>
    nnoremap _ :res -5<CR>
    

    It should be noted that there are built-in hotkeys in Vim to increase and decrease the window height and width, with the default number being 1:

    • Increase height (by 1): Ctrl-W +
    • Decrease height (by 1): Ctrl-W -
    • Increase width (by 1): Ctrl-W >
    • Decrease width (by 1): Ctrl-W <

    To use the above hotkeys with values other than 1, simply prepend the hotkey with the value:

    • Increase height by 5: 5 Ctrl-W +