The commands :sy, :syn, :synt, and :synta all run the :syntax command. For a script I'd like to know which command the user typed on the commandline, for example:

get_full_command('syn')   " returns 'syntax'

The closest I got is:

getcompletion('syn', 'command')

But this returns the tab completion:

['SyntaxName', 'syncbind', 'syntax', 'syntime']

Which isn't really useful here, since I still don't know which command :syn runs.

How can I get the full command name from these shortcuts?

  • For user commands it's E464. For builtin commands, I believe, the first match in an internal table (ex_cmds.h) wins. So there wouldn't be a nice solution for this. – Matt Feb 4 at 7:51

The way shortcuts work is that cmdidxs1 and cmdidxs2 are defined in ex_cmdidxs.h, which contain a index to the actual command definition. It's not easy to re-implement this in Vim, as it depends on the order in which the commands are defined in ex_cmds.h (the reason :s is :substitute is because this occurs before any other command starting with :s).

The find_ex_command() function contains the logic to read these two cmdidx arrays, but that's not exposed anywhere in VimScript: it's only called when running a function and for exists(':syn'), which isn't much help here. So you'll need to copy those two lists and reïmplement the logic in VimScript.

But since Vim 8.2.2468 (Feb 2021) there is the fullcommand() function, which exposes this:

fullcommand('syn')     →  'syntax'
fullcommand('syntax')  →  'syntax'
fullcommand('s')       →  'substitute'
fullcommand(':%s')     →  'substitute'

As you can see, it removes ranges as well, so it's easier to use.

  • That was quick! MT is TCB. ;) – B Layer Feb 6 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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