DEV Community

Amin
Amin

Posted on

Become a leader in VIM window and tab creation!

Preface

If you are like me, a VIM user, but have never used windows, now is a great time to do it!

The main reason I didn't use windows in VIM is because I use Tmux. But this is a bad reason, and not everybody is used to use Tmux, even though you should because this is a fantastic tool.

The second bad reason why I don't use windows is because I don't really like the key combination. Since most of the keys are used for something very logical, there is no more space for using window efficiently, meaning we need to use a key modifier (the control key) to create windows.

Leader

For instance, if I want to create a new vertical split window, I will type <C-w>v. Hitting the control key is a bummer for me. I know this is a very bad reason to not use windows in VIM.

What worked for me was to use the leader key.

The default leader key in VIM is a backslash. And this is not that bad and I've actually used it for many years before.

I've seen many people using the space key as their leader key so I put that in my configuration file.

let mapleader = " "
Enter fullscreen mode Exit fullscreen mode

And that's it. At first, I didn't think it would be better but as I used it in my remaps, I felt the difference immediately. It feels more natural and quicker to type Leader-prefixed key combinations when using the space key.

But if you find yourself another key that match your workflow by any means don't update it because of me. In the end, what works for me may not be what works for you. This article is really just my journey I wanted to share with some of you to get inspiration from.

The leader key is one wonderful way of creating our own key combinations without overriding the default one from VIM so that even if you want help from other people, they don't get stopped by your VIM settings and key overrides. They can just use VIM as if it was as vanilla as possible.

Windows

From there, I decided it was time to use windows a little bit more. And when I say windows, I do not talk about the operating system but indeed the window feature of VIM! I really love VIM because I don't really need to think about the keys I am hitting. For instance, if I want to change the inside of a whole word, I will type ciw which expands to change inside word. I need something like that for windows to.

nnoremap <Leader>hs <C-w>s
nnoremap <Leader>vs <C-w>v
Enter fullscreen mode Exit fullscreen mode

Like that, I don't need to think much. If I want to split the screen horizontally, I hit <Leader>hs which in my head expands to horizontal split. Same thing for vertical split. Only the space is odd here but with time I learn to not worry about it too much and it became quite natural now.

Now that we have several windows (and that we are using this wonderful feature in VIM without any plugins), we should also be moving around as quick as possible.

Moving around windows

Same thing, moving around windows is quite tedious, even though it seems logical. <C-w>h will move to the window on the left. And you can use hjkl, our beloved movements keys to move around windows. But if we want speed, hitting that modifier key (control) is way too much time spent to move around, especially with more than two windows.

We could use the leader key, but this would be practically the same problem. What we could do is use the alt key, which (correct me if I'm wrong) does not seem to be used that much in VIM. So I ended up remapping those like that.

nnoremap <A-h> <C-w>h
Enter fullscreen mode Exit fullscreen mode

You may be thinking: Hey! I though the modifier keys were lame to use. And you will be right. But here this is the only way (at least from what I experiment) to move around windows as quickly as possible. This is yet another modifier to learn but the time saved is huge. Especially for not having to hit <C-w> and h for instance for moving, worse, you have to hit this combination again to move another time. And I truly think this is a very good trade off.

So now, with windows created, we can quickly move to the left window by using the Alt h combination. This is way quicker, especially if we want to spam the keys for quickly moving to the N leftmost window.

And of course, we can do the same for every movement keys as well.

nnoremap <A-h> <C-w>h
nnoremap <A-j> <C-w>j
nnoremap <A-k> <C-w>k
nnoremap <A-l> <C-w>l
Enter fullscreen mode Exit fullscreen mode

Nice! But we can go even further by resizing windows in the same fashion.

Resizing windows

Here is what it looks like adding the necessary settings.

nnoremap <A-S-h> <C-w><
nnoremap <A-S-l> <C-w>>
nnoremap <A-S-j> <C-w>-
nnoremap <A-S-k> <C-w>+
Enter fullscreen mode Exit fullscreen mode

We can now resize windows using the Alt and Shift modifiers. But we can spam it as much as we want to quickly resize vertical and horizontal split windows.

And again, I'm not saying that there is anything wrong with the default based key combination, this is pretty amazing even because the keys are actually pretty logical. Plus to increase, minus to decrease. Etc... But by doing that, we gain some speed, we prevent ourselves some keystrokes for something that we may be spamming quite often now that we learned to love windows.

And that's pretty much it for windows. There may be even more setting or key combination for things related to windows but that is what is working for me.

Tabs

Next thing is tabs. And creating tabs is actually pretty simple since we have to type the command manually by doing :tabnew. Same thing for closing a tab with :tabclose.

But this is many keystrokes and we could be using our Leader key from before to create tabs more easily and without even thinking about it.

nnoremap <Leader>tc :tabnew<Cr>
nnoremap <Leader>tq :tabclose<Cr>
Enter fullscreen mode Exit fullscreen mode

Now we can create tabs using <Leader>tc which, again in my head, expands to tab create and <Leader>tq which expands to tab quit. That's nice!

There is, actually, a key combination to go to the next tab which is gt and to go back by using gT. But for the latter, we must actually use the shift key for the uppercase t. This is personnal preference, but I decided to map my keys to navigate tabs more naturally, again to prevent thinking about it.

nnoremap <Leader>tn :tabnext<Cr>
nnoremap <Leader>tp :tabprevious<Cr>
Enter fullscreen mode Exit fullscreen mode

And if I do the same exercise as before, <Leader>tn expands to tab next and <Leader>tp expands to tab previous.

Conclusion

That's it! That's my two cents for using windows and tabs a little bit more in VIM. I actually think that you should do you because those are the things that work for me but you should experiment, test things out and set you own VIM configuration for what is working for you.

You may even be using the default key combinations if you think that this is good for you and that is totally okay!

I still use Tmux (for those of you who are used to this application). But now I stopped worrying about sharing things around my VIM instance since I only have one instance now, but several tabs and windows. Actually, this is really not a problem for me because I use NeoVIM and the system clipboard is shared with the yanking/pasting of VIM so I could even not use tabs and windows at all but I guess this is a little more resource-friendly to not have 10 VIM instance for a client/server Web application for instance.

I like to think that VIM manages my project, and that Tmux manages multiple parallel projects and even customers I work for (one session = one customer).

In the end, all those key combinations demonstration was only a mean to an end that was: use windows and tabs more! These are awesome tools that VIM provided for us and I see too many VIM users still create tabs in their terminal instead of VIM only to create a new instance of VIM to edit another file for instance in the same project. You should be using tabs and buffers instead or even split the window if that is what you want (as far as I understand the goal of windows and tabs in VIM).

And here is the wrap up configuration.

" Use space as the leader key
let mapleader = " "

" Split aliases
nnoremap <Leader>vs :vertical split<Cr>
nnoremap <Leader>hs :split<Cr>

" Navigation between window
nnoremap <A-h> <C-w>h
nnoremap <A-j> <C-w>j
nnoremap <A-k> <C-w>k
nnoremap <A-l> <C-w>l

" Resizing window
nnoremap <A-S-h> <C-w><
nnoremap <A-S-l> <C-w>>
nnoremap <A-S-j> <C-w>-
nnoremap <A-S-k> <C-w>+

" Moving window
nnoremap <A-C-h> <C-w>H
nnoremap <A-C-l> <C-w>L
nnoremap <A-C-j> <C-w>J
nnoremap <A-C-k> <C-w>K

" Close window
nnoremap <Leader>wc :q<Cr>

" Creating & navigating tabs
nnoremap <Leader>tc :tabnew<Cr>
nnoremap <Leader>tq :tabclose<Cr>
nnoremap <Leader>tn :tabnext<Cr>
nnoremap <Leader>tp :tabprevious<Cr>
Enter fullscreen mode Exit fullscreen mode

Happy hacking!

Top comments (0)