René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

User defined commands in vim

:com                                Cmd-name   cmd-replacement
:com!                      Existing-cmd-name   cmd-replacement

:com -nargs=n                       Cmd-name   cmd-replacement
:com -nargs=*                       Cmd-name   cmd-replacement
:com -nargs=?                       Cmd-name   cmd-replacement
:com -nargs=+                       Cmd-name   cmd-replacement

:com -complete=augroup              Cmd-name   cmd-replacement
:com -complete=buffer               Cmd-name   cmd-replacement
:com -complete=command              Cmd-name   cmd-replacement
:com -complete=dir                  Cmd-name   cmd-replacement
:com -complete=environment          Cmd-name   cmd-replacement
:com -complete=event                Cmd-name   cmd-replacement
:com -complete=expression           Cmd-name   cmd-replacement
:com -complete=file                 Cmd-name   cmd-replacement
:com -complete=shellcmd             Cmd-name   cmd-replacement
:com -complete=function             Cmd-name   cmd-replacement
:com -complete=help                 Cmd-name   cmd-replacement
:com -complete=highlight            Cmd-name   cmd-replacement
:com -complete=mapping              Cmd-name   cmd-replacement
:com -complete=menu                 Cmd-name   cmd-replacement
:com -complete=option               Cmd-name   cmd-replacement
:com -complete=tag                  Cmd-name   cmd-replacement
:com -complete=tag_listfiles        Cmd-name   cmd-replacement
:com -complete=var                  Cmd-name   cmd-replacement
:com -complete=custom,{func}        Cmd-name   cmd-replacement
:com -complete=##A(customlist#customlist),{func}    Cmd-name   cmd-replacement

:com -range                         Cmd-name   cmd-replacement
:com -range=%                       Cmd-name   cmd-replacement
:com -range=N                       Cmd-name   cmd-replacement
:com -count=N                       Cmd-name   cmd-replacement

:com -bang                          Cmd-name   cmd-replacement
:com -bar                           Cmd-name   cmd-replacement
:com -register                      Cmd-name   cmd-replacement
:com -buffer                        Cmd-name   cmd-replacement
User defined commands must start with an uppercase letter.

Completion

Custom

function! CompletionTest1(ArgLead, CmdLine, CursorPos)
  return "one\ntwo\nthree\n" . a:CmdLine . "\n" . a:CursorPos . "\n" . a:ArgLead . "\n" . "hallo"
endfunction

:command! -complete=custom,CompletionTest1 SomeCommand1
Now, :SomeCommand1 followed by a space can be entered. Pressing the tabulator repeatedly shows
  • one
  • two
  • three
  • nothing (why????)
  • SomeCommand1
In order to be able to do something like :SomeCommand1 t <tab> <tab> <tab>... , the command should be defined like
:command! -nargs=1 -complete=custom,CompletionTest1 SomeCommand1

Customlist

More or less the same thing (???), but returning a list instead of a new line seperated string.
function! CompletionTest2(ArgLead, CmdLine, CursorPos)
  return ['one', 'two', 'three']
endfunction

:command! -complete=customlist,CompletionTest2 SomeCommand2

Attributes

User defined commands can have the following attributes:
  • Bang
  • Register
  • Local to current buffer
Which attributes a command has is shown with :command: Bang attribute: !, register attribute: ", local to current buffer: b.

Examples

func! PrintFirstAndLastLine() range
  echo a:firstline
  echo a:lastline
endfu

com! -range -nargs=* FL <line1>,<line2>call PrintFirstAndLastLine()
Now, typing :FL prints the line number of the first and last selected line in the buffer.
See Searching for special characters for an example of a user command.