Vim Tips Wiki
Register
(Insert TipProposed template + minor manual clean)
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{TipNew
{{TipProposed
 
|id=0
+
|id=1600
|previous=0
+
|previous=1599
|next=0
+
|next=1601
|created=August 8, 2008
+
|created=2008
 
|complexity=basic
 
|complexity=basic
 
|author=Fritzophrenic
 
|author=Fritzophrenic
 
|version=7.0
 
|version=7.0
 
|subpage=/200808
 
|subpage=/200808
|category1=
+
|category1=File Handling
|category2=
+
|category2=Tabs
 
}}
 
}}
 
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.''
 
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.
As discussed in [[VimTip135]] and [[VimTip1242]], using the <tt>'switchbuf'</tt> option along with <tt>:sbuffer</tt> will ''almost'' get us there, because it will allow us to switch to the already-open tab of a file, if it is already loaded into a buffer. However, you cannot load new files into a buffer in this fashion, nor will tab completion work for file names (it will complete buffer names only).
 
   
  +
The <code>:drop</code> command will edit a new file, or jump to the window containing it if it already exists. Using <code>:tab</code> with it (i.e. <code>:tab drop {file}</code>) will open the file in a new tab if it isn't already open.
The following commands in your vimrc will provide the Tfile and Wfile commands, to open a file in a new tab or window, or jump to whatever tab or window the file is currently open in if it has already been loaded. Filename completion is enabled on the commands, meaning that you can use <Tab> and the wildmenu (if enabled) to complete file names if desired.
 
   
  +
Dragging and dropping a file into gvim automatically invokes the <code>:drop</code> 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.
<pre>
 
command! -nargs=1 -bar -complete=file Tfile call OpenFileOrJump("tab", <q-args>)
 
command! -nargs=1 -bar -complete=file Wfile call OpenFileOrJump("", <q-args>)
 
   
  +
==References==
function! OpenFileOrJump(tab, fileName)
 
  +
*{{help|:drop}}
if bufexists(a:fileName)
 
  +
*{{help|drag-n-drop}}
let sb_save = &switchbuf
 
set switchbuf=usetab
 
exec a:tab." sbuffer ".a:fileName
 
let &switchbuf = sb_save
 
else
 
exec a:tab."new ".a:fileName
 
endif
 
endfunction
 
</pre>
 
   
 
==Comments==
 
==Comments==
  +
I couldn't decide whether it would be better to just merge this tip into [[VimTip1242]] and rename that page. If we keep this as a new tip, [[VimTip1242]] can probably be merged into [[VimTip135]]. I would not have discovered the basis behind this tip (switchbuf and :sbuffer) unless I had read [[VimTip1242]]. --[[User:Fritzophrenic|Fritzophrenic]] 18:03, 8 August 2008 (UTC)
 
  +
You can use <code>:command! -nargs=1 -complete=file O tab drop <args></code> to open the file or jump to it using <code>:O FILENAME</code>.

Revision as of 11:14, 25 September 2013

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.

References

Comments

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