Methods to Switch Tabs in Vim
To navigate the tabs there are multiple methods in Vim, let’s view these methods one-by-one:
Note: Tabs in Vim cannot be switched by their names because the tab name is the name of the current tab buffer.
Using gt and gT Shortcuts
To switch between the tabs in Vim, use the gt and gT commands. The gt shortcut will navigate to the next tab, while the gT to the previous tab.
gt | Switch to the next tab |
gT | Switch to the previous tab |
The gt and gT are the quickest key bindings that can help to navigate the tabs in the Vim editor. But there are some commands as well that perform the same operation, let’s learn them in the following sections.
Using tabnext Command
To switch to the next tab in Vim, use the :tabnext command in the NORMAL mode. The tabnext command switches to the next tab and if you continuously execute this command then it will loop through all the opened tabs.
You can also use the :tabn command to perform the same operation.
The tabnext command provides various operations to navigate the tabs:
:+tabnext / :-tabnext | Switch to next / previous tabs |
:tabnext +1 / :tabnext -1 | Switch to next / previous tabs |
:tabnext + / :tabnext – | Switch to next / previous tabs |
:+3tabnext / :-3tabnext | Switch to the 3 next tabs (Jump 2 tabs and open the third one) |
:1tabnext / :$tabnext | Switch to the first and last tab |
:tabnext # | Switch to the last accessed tab |
Note that you can replace the :tabnext with the :tabn keyword to perform the same operations in the NORMAL mode.
Using tabprevious Command
To switch to the previous tab in Vim, use the :tabprevious command or :tabp in the NORMAL mode. Continuously executing the same command will loop through all the tabs in Vim.
Using tabfirst and tablast Commands
To switch the first tab in Vim, use :tabfirst command:
You can also use the :tabfir command to switch to the first tab.
Similarly, to switch to the last tab, use the :tablast command or :tabl in the NORMAL mode.
Using tabmove Command
The :tabmove command technical moves the position of the tab. You can use :tabmove or :tabm commands to switch the position of the tabs. For example, to switch the position of the currently opened tab to the left or right, use :-tabmove and :+tabmove commands respectively.
In the same way, to move the position of the current tab to any other position, use :tabmove [N] where N is the count number. For instance, use the command :tabmove 4 to relocate the current tab to the fourth position in tabs.
Other tabmove commands are listed below:
:+tabmove / :-tabmove | Switch position of the tab left / right |
:0tabmove or :tabmove 0 | Set the current tab as the first tab |
:$tabmove or :tabmove $ | Set the current tab as the last tab |
:#tabmove or :tabmove # | Move the position of the current tab after the last accessed tab |
Using Mouse
In Vim, using the mouse is the easiest and quickest way to switch between the tabs. Before using this functionality, you have to enable the mouse in the Vim editor.
To enable mouse functionality in Vim use :set mouse=a in the NORMAL mode. Where a indicates the mouse activation for all the modes:
Note that the above command will enable the mouse for the current session, to keep the mouse enabled permanently place the above command in the vimrc file.
Now, you can click on the tab to access it just like GUI-based windows.
Switching to a Specific Tab in Vim
To switch to a specific tab in Vim, you can use the tab number [tab count] with :tabnext/:tabn, and gt/gT commands in the Vim editor.
For example, to move to tab number 3 use :tabnext command with the count number.
Similarly, to switch tab number 3 in Vim use 3gt in the NORMAL mode:
Or to move back first or second tab, just use 1gt or 2gt.
Mapping Keys for Quick Tab Switching in Vim
Well, switching tabs should be kept simple, because no one would like to type a whole command just to switch between the tabs. Vim has a solution for it in the form of the key mapping functionality.
Let’s map the left and right directional keys to switch tabs between left and right with the control key. Open the vimrc file place the below-given lines and save the file by pressing shift+zz or executing :wq command.
noremap <C-Right> : tabprevious<CR>
Note that you can change the key mapping at any time or use any other preferred keys to quickly switch the tabs.
Conclusion
If you need to switch between tabs in Vim, use gt and gT to move to the next and previous tab, respectively. If you are finding it difficult to use these keys, simply map the desired keys to switch the tabs. You can also enable the mouse functionality to switch the tabs in the Vim editor. The :tabnext and :tabprevious are commands that perform the tab-switching functionality, but I personally would not like to type a command just to switch tabs, however, these commands can be helpful to land on a specific tab in Vim.