Let’s say I have single Vim tab displaying 9 buffers (equally separated, like a 3×3 table).
Currently, to get from the top left window to the bottom right one, I have to press 3, Ctrl+W, J, and then 3, Ctrl+W, L. This is cumbersome, and I would like to just be able to press Ctrl+9 to go to the 9th window, and Ctrl+3 to go to the 3rd window, etc.
Is there any easy way I can map something like this in Vim?
There’s a much simpler solution than using the mouse or hard-set movement mappings; they will break if the window numberings are different from what you have in mind for a 3×3 matrix, or if you decide to work with less than 9 windows. Here’s how:
Include the following in your
.vimrc:Now you can just press
<Leader><number>and be taken to the window number you want. I wouldn’t recommend going beyond 9, because IMO, the utility of having multiple viewports follows a Rayleigh distribution and quickly becomes useless with too many viewports in one window.It will be helpful if you have the window number displayed in your
statuslineto aid you in quickly figuring out which window you’re on and which window you want to go to. To do that, use this little function and add it accordingly in yourstatusline.See it in action in your
statusline:Note that the above line will replace your
statusline. It was just meant for illustration purposes, to show how to call the function. You should place it where ever you think is appropriate in yourstatusline. Here’s what mine looks like:Update
romainl asked for my status line in the comments, so here it is:
The last line should be a single line (be careful if your setup automatically breaks it into multiple lines). I know there are ways to keep it organized with incremental string joins in each step, but I’m too lazy to change it. 🙂 The
GitBranch()function (with other git capabilities) is provided by the git.vim plugin. There’s a bug in it as noted here and I use the fork with the bug fix. However, I’m leaving both links and the blog here to give credit to all.Also, note that I use a dark background, so you might have to change the colours around a bit if you are using a light scheme (and also to suit your tastes).