(categories) |
(rewrite to use :drop) |
||
Line 13: | Line 13: | ||
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 |
+ | The <tt>:drop</tt> command will edit a new file, or jump to the window containing it if it already exists. Using <tt>:tab</tt> with it (i.e. <tt>:tab drop {file}</tt>) will open the file in a new tab if it isn't already open. |
+ | Dragging and dropping a file into gvim automatically invokes the <tt>:drop</tt> command. |
||
− | <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) |
||
− | 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) |
Revision as of 20:52, 15 January 2009
Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).
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.