Reputation: 1695
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?
Upvotes: 36
Views: 10949
Reputation: 59677
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.
Upvotes: 48
Reputation: 36071
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"
Upvotes: 9