12

Please let me know how to show the close button on each tab page in GVIM.

Also, is it possible to set a warning if I am closing GVIM with multiple tab pages open?

3 Answers 3

71

Short Answer

Use buffers effectively!

Long answer

You might consider use :tabclose to close your tabs. This has the benefit of closing the tab (and keeping you away from your mouse) but also being just annoying enough to motivate a workflow change over to using buffers and splits. As for a warning about closing a tab with multiple windows, I see little benefit if you have 'hidden' set then the buffers are still there so no harm. If you do not have 'hidden' than any modified buffers will be prompted before they are closed.

Why not tabs

Tab pages are great. However it all comes down to vim treating tab pages as more of view-ports into a group of windows or workspaces. Tab pages just don't scale well. Just open some other GUI editor and open up 20 or so tabs. It becomes tricky to deal with. I have had over a 100 buffers open and switch between them without worrying. Buffers just scale in ways that tabs do not. Buffers and splits also work well with smaller windows which is often the case when using terminals.

Don't just take my word for it. Here are some more supporting information:

Quick Buffer Tips and Tricks

  • set hidden will hide buffers when abandoned.
  • Use :sb to switch to a buffer you already opened.
  • :sb and :b both can take partial names and globs. e.g. :sb foo*
  • Take a look at 'switchbuf' and change to your preferences.
  • Learn to love :sp, :new, and all type of split commands.
  • Learn some <c-w> commands to make your life with splits easier.
  • Use capital letter marks to jump back to buffers where you know you will jump back to.
  • Use tags to jump to definitions.
  • Possibly use cscope along with tags
  • Use <c-6> to switch to the previously edited file.
  • Use <c-o> and <c-i> to move older/newer positions in your jump list.
  • Possibly make use of one of the many buffer plugins out there. e.g CtrlP

For more information see:

:h :sb
:h windows
:h 'hidden'
:h 'swb'
:h ctrl-6
:h ctrl-o
:h tags
:h cscope
:h tab-page-commands
4
  • Not a direct answer, but a valid argument and very nice selection of links! Jan 24, 2014 at 19:32
  • lol, bit off topic, but the best case I have heard yet for navigating your open files using buffers - after reading this, I will give using buffers a try! Oct 19, 2015 at 23:11
  • "Use capital letter marks to jump back to buffers where you know you will jump back to." Wow. This is what was missing from my life. May 3, 2018 at 23:55
  • fzf.vim for switching buffers, opening files with fuzzy name input.
    – Ejaz
    Feb 15, 2019 at 20:13
8

Showing the close button

The 'tabline' option can include a %X marker for the close tab button, but that only works for the console version of the tab line. :help 'guitablabel' explicitly states:

Note that syntax highlighting is not used for the option. The %T and %X items are also ignored.

So, to literally get what you want, you'd have to switch to the (uglier) text-based tab line with

:set guioptions-=e

But note that using the mouse (even though you may be customized to that by other applications like web browser tabs) is discouraged in Vim. If you right mouse button-click on a GUI tab label, you can alternatively also choose |Close tab| from the popup menu.

Preventing tab close

(Note that asking multiple questions is frowned upon here; please ask them separately.)

I don't think this is possible; there is a VimLeavePre event that you could hook into, but it cannot prevent the closing of Vim; the action (e.g. :qall!) cannot be undone. If you are used to closing Vim always via a mapping or command, you could overwrite that and include a check in there (using tabpagenr('$') > 1).

If you're concerned with losing your window / tab layout, rather have a look at Vim sessions (which can be persisted automatically in the background). This will allow to re-launch Vim with the previously opened windows.

3
  • Is there any way to hook into vim basic commands, like tabc or let ? I.E replace them with your own version that may or may not call original.
    – majkinetor
    Jan 24, 2014 at 14:22
  • @majkinetor Not generally; the common workaround is to define custom upper-cased variants, and override the original (lowercased) built-in command through the cmdalias.vim plugin. Jan 24, 2014 at 14:25
  • That is not workaround. Its something totally different. :) Its bad such thing is not there IMO.
    – majkinetor
    Jan 24, 2014 at 16:54
0

If you need this, you are probably doing it wrong. You don't need mouse with vim.

But how about

nnoremap <MiddleMouse> :tabclose<CR> 

Other then that see TabLineSet.vim : A Vim7 tabline customization which has

closers : add hot spots ("!") to the tab for click-to-close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.