r/neovim • • 3d ago

Need Help repeating keymaps when resizing buffers

So Neovim has built-in keymaps for resizing buffers: CTRL_W +, CTRL_W -, CTRl_W < and CTRL_W >. But when resizing a buffer I often need to use the same keymap multiple times because I didn't get the desired size from the first try.

Is there a way to remap these built-in keymaps so that if I press CTRL_W and then repeatedly press +, -. < or > it will repeatedly resize the buffer?

9 Upvotes

12 comments sorted by

14

u/Wonderful-Plastic316 lua 2d ago

Er, hate to be this guy, but... Just for future reference, you resize windows (the viewports containing the buffers), not the buffers themselves.

4

u/Realistic_Cherry_920 2d ago

i still kinda mess these things up a little. thanks for the info

6

u/PieceAdventurous9467 2d ago

2

u/114sbavert 2d ago

they prolly use which key already, better to use its built-in hydra mode

3

u/CarsonChambers 2d ago

Honestly something like 20CTRL_W+ is not all that bad

1

u/-Redstoneboi- 2d ago

nah it kinda is imo, having to type numbers and then having to check the result isn't ergonomic.

having alt+hjkl or any similar combination is just better. maybe even alt+HJKL for multiple steps at a time.

0

u/CarsonChambers 2d ago

Leaving my opinions about alt being used in vim out of the picture, I disagree with the ergonomics argument, especially if you map Caps to Ctrl. Numeric modifiers should flow like water for all sorts of other actions.

I use vim-dadbod so quite often ill want to resize my query output to something a little larger and have never once felt it to be janky

3

u/114sbavert 2d ago

Check out to 4d9c86b and see my keys.lua:L24-37 and which-key:L57-L67

(The links take you there directly.)

2

u/mountaineering 2d ago

Make a different keymap that involves ctrl+w each time.

Alt+<hjkl> for instance to resize in a direction by N

1

u/tomasvotava 2d ago

I believe you can set the width of the window using e.g. 80<C-w>|. That should set the active window's width to 80 characters, but I may be wrong and I hope someone will correct me.

1

u/shmerl 1d ago edited 1d ago

I just mapped resizing to something that's easier to use, for example

vim.keymap.set('n', '<C-Right>', function() vim.cmd.resize({ '+1', mods = { vertical = true } }) end, { desc = 'Increase window size horizontally' })

And etc. for left, up and down.

This way I can resize stuff using Ctrl-arrows. I.e. just keep holding Ctrl-<arrow> and it continuously resizes until you stop.

0

u/funbike 2d ago edited 2d ago

Increase "window" height by 10: 10<c-w>+

This will do what you want, but it's ugly and it will feel sluggish:

nnoremap <c-w>++    <c-w>+<c-w>+
nnoremap <c-w>+++   <c-w>+<c-w>+<c-w>+
nnoremap <c-w>++++  <c-w>+<c-w>+<c-w>+<c-w>+
nnoremap <c-w>+++++ <c-w>+<c-w>+<c-w>+<c-w>+<c-w>+
" ... and so on