Vim Tips Wiki

Edit a file or jump to it if already open

1,613pages on
this wiki
Tip 1600 Printable Monobook Previous Next

created 2008 · complexity basic · author Fritzophrenic · version 7.0

When using most tabbed editors, if you open a file for editing, and the file is already open in another tab, the editor will simply jump to that tab. There are very good reasons that Vim does not act this way, which are too numerous to go into detail about here – suffice it to say that there are many excellent features in Vim that would not be possible if it acted like "most tabbed editors". Nevertheless, it would be nice if Vim had this functionality when the user wishes to use it.

It turns out, Vim does have this functionality, but it is hard to find.

The :drop command will edit a new file, or jump to the window containing it if it already exists. Using :tab with it (i.e. :tab drop {file}) will open the file in a new tab if it isn't already open.

Dragging and dropping a file into gvim automatically invokes the :drop command. If you hold down the CTRL key while doing this, it will force a window to split (there isn't a way to force a tab, but CTRL-W T right afterwards will do that for you easily). If you hold down SHIFT, it will set Vim's working directory to that of the file.



You can use :command! -nargs=1 -complete=file O tab drop <args> to open the file or jump to it using :O FILENAME.

Around Wikia's network

Random Wiki