I often edit long files in vim that have blocks of code in multiple disparate places in the file that I need to be constantly going back and forth between. Obviously, one way of solving this is to split the window with :split and edit each portion in a different split window, and a :w
in either window will save the whole file. This is well and good if you have a large enough screen but sometimes I have to use vim on a low-resolution laptop and I don't want to reduce my screen space further by splitting the window.
In this case, what I'd really like to do is edit the file in multiple tabs, and treat each separate tab exactly like a separate view split. I can sort of mimic this by using :tabopen <the same filename>
once I have one copy of the file open, but this is sort of hacky — it doesn't work if I've already made changes to the file because vim thinks I'm just opening the file a second time.
Is there a good way to get the behavior I want with tabs in vim?
To directly move to first tab or last tab, you can enter the following in command mode: :tabfirst or :tablast for first or last tab respectively. To move back and forth : :tabn for next tab and :tabp for previous tab. You can list all the open tabs using : :tabs. To open multiple files in tabs: $ vim -p source.
1 Invoking vi on Multiple Files one. When you first invoke vi, you can name more than one file to edit, and then use ex commands to travel between the files. invokes file1 first. After you have finished editing the first file, the ex command :w writes (saves) file1 and :n calls in the next file (file2).
To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast).
The :tab
command takes a command as argument.
So you can do this:
:tab split
This will work even if the buffer is modified, and a save in either tab saves the file, updating the saved state in both.
You can use the :tab
command:
:[count]tab {cmd}`
Execute
{cmd}
and when it opens a new window open a new tab page instead. [...] When[count]
is omitted the tab page appears after the current one. When[count]
is specified the new tab page comes after tab page[count]
. Use:0tab cmd
to get the new tab page as the first one.Examples:
:tab split " opens the current buffer in new tab page :tab help gt " opens tab page with help for "gt"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With