3

I have the following Vim code:

function! TabClose(...)
    if a:1 == ""
        tabonly
    elseif a:1 == "left"
        Tabops Close Left
    elseif a:1 == "right"
        Tabops Close Right
    else
        execute "tabclose " a:1
    endif
endfunction

command -nargs=* TabClose call TabClose("<args>")

I would really like to completely rebind the tabclose command. So I tried the following.

command -nargs=* tabclose call TabClose("<args>")

But it did not work because tabclose is called in the function. How can I name my command tabclose but still use the original unmodified tabclose in that function?

1
  • 1
    You can't override built-in commands; user-defined commands must start with a capital letter. There is no way around this I'm afraid, other than the abbreviation "correction" that Vivian posted in their answer. You can also map your custom behaviour to a key (e.g. nnoremap TC :TabClose ), which I find quite convenient for common commands anyway. Aug 20, 2022 at 17:04

1 Answer 1

5

If you want to keep your habit typing tabclose you could use:

cnoreabbrev tabclose TabClose

Or as mentioned by @Rich to avoid that the abbreviation triggers in search or as part of other commands:

cnoreabbrev <expr> tabclose (getcmdtype() == ":" && getcmdline() == "tabclose") ? 'TabClose' : 'tabclose'

The getcmdtype() == ":" condition ensures that the abbreviation not used when using the command line to search (/, ?) or non execution command line modes.

The getcmdline() == "tabclose" conditions ensures that the abbreviation is used only for the :tabclose command and not as part of some more fancy commands like :e tabclose

With this when you enter the command:

:tabclose

Vim will translate it to :TabClose when you hit Enter

2
  • 2
    I think this is close enough to what the asker requested to not need the disclaimer you added at the start. ;) It’s pretty unlikely to matter in this case, but probably worth mentioning the possible issues of this triggering in search or when not at the start of the command line, and how to fix these with getcmdtype and getcmdpos?
    – Rich
    Aug 20, 2022 at 18:00
  • 2
    Thanks @Rich. I have used getcmdline but I'm interested to know how getcmdpos could be used for the same (e.g. getcmdpos()==len('tabclose') + 1) and what is the advantages over the getcmdline solution. Aug 20, 2022 at 18:47

Your Answer

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

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