Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit the same file in multiple tabs in vim?

Tags:

vim

tabs

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?

like image 687
Tneuktippa Avatar asked Jul 03 '12 18:07

Tneuktippa


People also ask

How do I make multiple 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.

How do you edit multiple files in vi?

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).

How do I switch between tabs in Vim?

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).


2 Answers

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.

like image 52
pb2q Avatar answered Sep 23 '22 15:09

pb2q


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"
like image 27
Geoff Reedy Avatar answered Sep 26 '22 15:09

Geoff Reedy