16

I want to set :x in vim gui-mode to delete buffer because I always kill the whole gvim, which is kind of annoying. I know i can specifically set gui problems with if has("gui running") but don't know how to remap :x

thanks in advance

ps.: maybe the tag/term remap is wrong but I don't know the correct term, that's why google didn't provide any help at all.

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
  • 1
    possible duplicate of [Vim: Redefine a command](http://stackoverflow.com/questions/2605036/vim-redefine-a-command) – eckes Dec 19 '13 at 10:29

2 Answers2

33

I find the safest alternative is to use an expression abbreviation:

cnoreabbrev <expr> x getcmdtype() == ":" && getcmdline() == 'x' ? 'bd' : 'x'

This will ensure the abbreviation will only be expanded to bd when :x is used otherwise just expand to x.

For more help:

:h map-<expr>
:h getcmdtype()
:h getcmdline()

Upon further inspection there appears to be a plugin that does exactly this by Hari Krishna Dara called cmdalias.vim. It uses a variation of the technique above.

Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
4

This is not as easy as it looks. :map won't work with commands and :command only accepts commands that start with an uppercase letter. But you can use :cabbrev:

if has("gui_running")
  cabbrev x bd
endif

UPDATE: :cmap could actually be used: :cmap x bd, but it doesn't work right: each occurrence of x in a command is immediately replaced by bd.

EDIT: This question is a duplicate of Can I (re)map Ex commands in vim?.

Community
  • 1
  • 1
daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
  • Why `has("gui running")`? I don't believe that'll ever be true. Should be `has("gui")`. – Chris Morgan Sep 22 '11 at 13:41
  • 3
    This has the problem then that if you ever enter "x" as a word, e.g. `:echo "1 + x = 3"` (convoluted, I know!), it'll be turned into `:echo "1 + bd = 3"`. @Peter's solution is better because it doesn't have this flaw. – Chris Morgan Sep 22 '11 at 13:46
  • @ChrisMorgan It should be `has("gui_running")`. It can be found in a list below `:h has-patch` (use `:helpg gui_running`). – ZyX Sep 22 '11 at 16:42
  • @ZyX Correct, my mistake. I've used it before but didn't refer to it... `has('gui')` certainly won't cut it. – Chris Morgan Sep 22 '11 at 22:51