Vim Masterclass: Tabs

Vim has the ability to load multiple files at once. The command vim a.txt b.txt will load the files ‘a.txt’ and ‘b.txt’ into Vim’s memory. Once loaded, you can switch to the next file with the ‘:n’ command. This will now display the ‘b.txt’ buffer. Now you can use the command ‘:N’ to switch to the previous file; in this instance ‘a.txt’.

However, a modern UI tool for managing multiple documents is tabs.

Vim has supported tabs since version 7, even though it’s a command line tool.

Let’s re-open the files again, now with the ‘-p’ option:

vim -p a.txt b.txt

The same thing happens as before, except now the top line of the screen shows the two files that we have open. Immediately, at a glance, I can see what files I have open. To switch to the next tab you use the command ‘:tabn’; think of ‘tab next’. To switch to a previous file, use ‘:tabp’; think ‘tab previous’.

This is fine with our current tabs open, but what if we wanted to open a file in another tab? For this we’d use the command ‘:tabf’ followed by the file path:

‘:tabf c.txt’

This opens the file ‘c.txt’. If you were currently viewing the ‘a.txt’ tab, then the new file will be open in the second tab. That’s fine, but breaks our ordering. We can rectify this with the ‘tab move’ command:

:tabm 2

This puts the tab into the third position (Vim tabs use a numbering system that starts with 0, and not 1).

A benefit to using tabs in this manner is that you can then run commands across all tabs, such as a search and replace:

:tabdo %s/forlinux/ForLinux/g

This entry was posted in Managed Hosting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>