Vim: Tabs

mr_destructive

Meet Rajesh Gor

Posted on August 14, 2021

Vim: Tabs

Introduction

So, you are playing with Vim and getting good at but something pulls you off. Basically can't get used to switching between files or windows, and that's totally fine. Some may prefer using Window splitting, file managers, and whatnot. But let me introduce you to TABS in Vim. A simple and elegant way to open multiple files in Vim.

Opening Tabs

To open a tab, you can press :tabnew or :tabedit to open a blank Tab with no file open in it. This basically works like the :e command, which opens a buffer for you with no named file.

If you already have an existing file in the current folder you are in, then you can press :tabf filename or :tabnew filename or :tabedit filename. This also applies to opening folders or directories, which will open the file structure in Vim buffer.

tabop.gif

From the above illustration, we can see that the new tab was created using the filename with the tabf command and an empty tab was created with tabnew command. Ya, we can use tabnew for both cases but it saves time to write two more letters. It depends on the preference as you don't have to remember one more command in this case. You can also customize the commands if you feel they are too big to type in like a simple mapping would do the trick for opening the tabs for you.

To open a Tab with a file specified.

  • :tabf filename

  • :tabnew filename

  • :tabedit filename

Open a Tab without any file specified.

  • :tabnew

  • :tabedit

You can open the tabs as per your choice like it could be ideal if you are gonna use certain files for a longer duration of time. This could be very ideal for various programming cases especially in Web, Android, Application Development where we need to edit a few files again and again. If you prefer Window-Splitting, that's totally fine, this is just to tell that there exist other ways as well.

Switching Tabs

Now if you are comfortable with opening tabs, we can now move on to switching between tabs. If you just have few tabs open, you can easily switch to the next tab using gt and to the previous tab using gT commands. But if you are in a great mode and want to open ten-twenty tabs XD, then you can use the numbers before the gt command. Like you can type 5gt to move to the 5th Tab. If you do not know which tab is which, you can type in :tabs and this will open up the currently open tabs along with the numbers.

tabsw.gif

You can see my keystrokes(except the last keystroke) in the lower right corner. We can easily switch between tabs using the three sets of commands and surely configure them as per your preference. We also saw the :tabs command which can be pretty handy if you are working with a number of tabs.

You can move around Tabs using some special commands like :tablast to move to the last tab and :tabfirst to move to the first tab.

  • gt -> Move to the NEXT Tab being in Noraml mode.

  • gT -> Move to the PREVIOUS Tab being in Normal mode.

  • ngt -> Move to the Nth Tab in Normal mode. (n is any number of tab which are opened).

  • :tablast -> Move to the LAST Tab.

  • :tabfirst -> Move to the FIRST Tab.

  • tabs -> Get a list of Tabs which are currently opened. (includes file opened in the tab and the number)

Closing Tabs

So, after opening tabs you want to close em right? That is quite simple as expected. Just type :tabc, this will delete the current tab. You can close the specific tab by prefixing tabc with the number of that tab. Like if you want to delete the 2nd tab, use :2tabc to close the 2nd tab.

If you want to reopen the closed tab, you can look out for the buffer name using :ls and then after finding the number of buffers in which your tab was open, you can type :tabnew +nbuf, here n is that number of the buffer.

If you want to close all the tabs except the current one, you can use :tabo. This will clear the tabs except in which you are in, hence it will also collapse the top tab bar showing the file opened in those tabs.

tabce.gif

As from the above GIF, we can see we located the number of the buffer last closed as we knew the name of the file which was opened in that tab. We also saw how to delete the specific tab using its number and the current tab.

  • :tabc -> Close the current tab.

  • :ntabc-> Close the Nth Tab.

  • :tabo -> Close all the tabs except the current Tab.

Re-ordering Tabs

This is a very tiny little detail but becomes a super tool in many cases. Let's say you want some reference of some content in the file, again and again, it's quite likely you should make the tabs nearby instead of switching tabs again and again. You can use Window splitting in this case, though we will see how to reorder tabs just for having the grasp on using Tabs in Vim.

To reorder tabs, you are basically moving a tab from one position to other. Let's say you have a Tab at position 5 which is your current tab, you want it at position 2. So what you can do is move the current tab to position two, as simple as to speak :tabm 1. This will move the current tab which is at number 5 to the 2nd position. Remember the tab order is 0 based so just use the number you are thinking minus 1. So the command becomes :tabm n, where n is the index of the tab(starts from 0, the left-most tab). If you want to move to the last tab, you would not specify any number just type the tabm command, and that's it.

tabmv.gif

From the above example, we were able to move around the tabs to our desired location without a hassle. This is some basic stuff you can do with Tabs in Vim, surely you can add in your custom mappings to enhance the productivity and improve the workflow in Tabs in Vim.

  • :tabm n -> Move the current opened Tab to the Nth position (Starts from 0).

Conclusion

So, we have seen how we can use Tabs and move around in between files and folders, we are now able to open, close, move, navigate around the tabs in Vim. By using some custom mappings, this can be overhauled for much fewer keystrokes that get in it. There are many other navigation techniques in VIm, and using Tabs is one of them, surely it won't suit everyone but there will be someone who will prefer using this. Thank you for reading till here. I hope you learned something from this to enhance your grasp in Vim. Happy Coding and Viming :)

References

💖 💪 🙅 🚩
mr_destructive
Meet Rajesh Gor

Posted on August 14, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Why use Vim?
vim Why use Vim?

September 6, 2021

Vim: Tabs
vim Vim: Tabs

August 14, 2021