DEV Community

Liz Lam
Liz Lam

Posted on

How to Use Tabs in vim

I started using tabs just recently in vim and found it pretty useful.

Create

Alt Text

There are many ways to create a new file on the command line.

In vim, you can start to create a new file using tabs with the following:

Type :tabnew and then hit ENTER

On my Mac, this drops me into the editor in insert mode.

You may need to enter insert mode manually (i.e. hit i).

Type away to your heart's content!

To write the file:
Hit ESC
Type :w file_name and then enter.

Open

Alt Text

Open an existing file using :tabe name_of_file.

Be mindful of your spelling because if name_of_file doesn't exist, vim will open a tab with an empty buffer as though you were trying to create a new file.

Navigate

Alt Text
In command mode (hit ESC):

Navigate forward between tabs with gt.
Navigate back using gT.

Top comments (4)

Collapse
 
moopet profile image
Ben Sinclair

I like thinking of tabs the same way tmux thinks of windows - that is, I rarely use them but when I do it's to make separate workflows.

A couple of things confused me about your post. Are you running some sort of vim "distribution" that changes the default way of working?

Out the box, neither tabedit or tabnew create a file, they just open a tab. If you write that buffer later, then they will try to create the corresponding file if it doesn't already exist. They also don't open a buffer in insert mode, unless you've made some modifications to your config.

You can get heavily customised layouts using a combination of tabs and windows, so something else that's useful to look into is sessions (made with :mksession) which let you save the whole layout. Kind of like a workspace in an IDE.

Collapse
 
grepliz profile image
Liz Lam

Yes, you are right! I'll update the blog to be more clear about actually needing to write the buffer before the file is actually created. So on my Mac, I can just start typing and I'm in insert mode. I suspect this may be different on Linux. I'll update to make that clearer too. Thanks for your input and questions!!

Collapse
 
francoislp profile image
François

Nice to know!
Could be awesome combined with Nerdtree plugin :)

Collapse
 
grepliz profile image
Liz Lam

I'm not familiar with the Nerdtree plugin. I will have to check it out!