Vim Tips Wiki
Advertisement
Tip 1247 Printable Monobook Previous Next

created 2006 · complexity basic · author hari_vim · version 7.0


Occasionally users will improperly use the commands :qa or :qa! instead of the proper :tabclose to close a tab in a session, quitting the entire session instead. A few methods can be used to remap the :tabclose command to a more memoriable command as outlined below.

Remap :tabclose to QA[]

This method requires the plugin cmdalias.vim plugin. This plugin will allow you to keep your abbreviations in the context of a : command. Once this plugin is loaded add the following to your vimrc to create a user command for QA.

command! -bang QA :call TabQAll('<bang>')
function! TabQAll(bang)
  try
    if tabpagenr('$') > 1
      exec 'tabclose'.a:bang
    else
      exec 'qa'.a:bang
    endif
  catch
    echohl ErrorMsg | echo v:exception | echohl NONE
  endtry
endfunction

Finally create an abbreviation for :qa to mean :QA which will allow you to not have to remember :QA making it easier to use the command you are already familiar with to "close" your tab. To accomplish this add the following to your vimrc:

function! InitPlugins()
  call CmdAlias('qa', 'QA')
  au! InitPlugins VimEnter
endfunction

aug InitPlugins
  au!
  au VimEnter * :call InitPlugins()
aug END

Now typing :qa in a tab will close out the tab.

Note the use of a VimEnter autocmd instead of calling CmdAlias directly in the vimrc. This is done, because the plugin needs to be loaded before the functions defined in the plugin can be used. Since plugins are loaded after the .vimrc, VimEnter allows the plugin to load before the function is called.

Firefox Tab Close[]

Some users find it easier to map tab commands to the Firefox key-stroke patterns (Ctrl + T = New Tab, Ctrl + F4 = Close Tab). To do so, add the following to your vimrc:

"Vim 7 specific mappings
if version >= 700
  map <C-t> <Esc>:tabnew<CR>
  map <C-F4> <Esc>:tabclose<CR>
endif

See also[]

Comments[]

Advertisement