Vim Tips Wiki
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
  +
{{review}}
{{TipImported
 
  +
{{Tip
 
|id=1333
 
|id=1333
  +
|title=have focus on left tab after tabclose
|previous=1332
 
  +
|created=September 19, 2006 14:25
|next=1334
 
|created=2006
 
 
|complexity=basic
 
|complexity=basic
 
|author=mz
 
|author=mz
 
|version=n/a
 
|version=n/a
 
|rating=12/6
 
|rating=12/6
  +
|text=
|category1=Tabs
 
  +
The default is that when you close a tab, the tab to the right of the tab you just closed becomes the current tab. This mapping closes the current tab and makes the tab to the left of the tab you just closed the current tab. I like this to provide symmetry with tabopen (new tabs go to the right of the tab you were just on). This way, if I'm editing a file, do something real quick in a new tab, then close that tab, I get back to my original tab. Obviously the keys for the mapping can be changed to whatever you want.
|category2=
 
}}
 
By default when a tab is closed, if another tab is open to the right of the tab that was just closed, it will become the "in focus" or current tab. However, when editing quickly this does not provide symmetry with the <code>:tabopen</code> command, because the <code>:tabopen</code> command opens a new tab to the right, and the <code>:tabclose</code> does restore focus to the original tab upon closing. With the following command in the .vimrc file, when editing a file, if the user needs to do something quickly in a new tab, they can use a mapping to close it and return to the original tab.
 
   
 
<pre>
 
<pre>
noremap <silent><C-S-w> :if tabpagenr() != tabpagenr('$')<CR>:tabclose<CR>:if tabpagenr() > 1<CR>:tabprev<CR>:endif<CR>:else<CR>:tabclose<CR>:endif<CR>
+
noremap &lt;silent&gt;&lt;C-S-w&gt; :if tabpagenr() != tabpagenr('$')&lt;cr&gt;:tabclose&lt;cr&gt;:if tabpagenr() &gt; 1&lt;cr&gt;:tabprev&lt;cr&gt;:endif&lt;cr&gt;:else&lt;cr&gt;:tabclose&lt;cr&gt;:endif&lt;cr&gt;
 
</pre>
 
</pre>
   
==Comments==
 
The following accomplishes the same as above, but if a tab is not being closed, will just call the <code>:q</code> command.
 
   
  +
(I hope I'm doing this right... it's my first contribution to vim.org :) )
<pre>
 
 
}}
function! CloseSomething()
 
  +
if winnr("$") == 1 && tabpagenr("$") > 1 && tabpagenr() > 1 && tabpagenr() < tabpagenr("$")
 
 
== Comments ==
tabclose | tabprev
 
  +
When I want to do Tab-Close with ":q" or ":wq", and ":q!", how do it?
else
 
  +
q
 
  +
'''Anonymous'''
endif
 
  +
, December 3, 2006 2:08
endfunction
 
  +
----
map <C-x> :call CloseSomething()<CR>
 
  +
<!-- parsed by vimtips.py in 0.695564 seconds-->
</pre>
 
  +
  +
[[Category:Tabs]]
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)