Viml commands: Difference between revisions
From wikinotes
Line 96: | Line 96: | ||
</source> | </source> | ||
</blockquote> | </blockquote> | ||
= Selection Ranges = | |||
<blockquote> | |||
<syntaxhighlight lang="vim"> | |||
function! PrintSelectedLines() range | |||
echom getline(a:firstline, a:lastline) " array of selected lines | |||
endfunc | |||
command -range PrintSelectedLines <line1>,<line2>call PrintSelectedLines() | |||
</syntaxhighlight> | |||
</blockquote><!-- Selection Ranges --> | |||
= <Plug> commands = | = <Plug> commands = |
Revision as of 14:22, 7 April 2023
Basics
:help user-commands
https://vimhelp.org/map.txt.html#user-commands
" syntax: " command [{attrs}] {name} {instructions} command MyFunc :call MyFunc()
Parameters
Define number of parameters that may be accepted
-nargs=0 No arguments are allowed (the default) -nargs=1 Exactly one argument is required, it includes spaces -nargs=* Any number of arguments are allowed (0, 1, or many), separated by white space -nargs=? 0 or 1 arguments are allowed -nargs=+ Arguments must be supplied, but any number are allowedfunction! Blah(arg) echo a:arg endfunc command -nargs=1 Blah :call Blah('<args>') " <args> is all args as string command -nargs=* Blah :call Blah(<f-args>) " <f-args> is arguments as a list of strs :Blah 'abc' " run command 'Blah'
Parameter Completion
:help command-complete
-complete=arglist file names in argument list -complete=augroup autocmd groups -complete=buffer buffer names -complete=behave :behave suboptions -complete=color color schemes -complete=command Ex command (and arguments) -complete=compiler compilers -complete=cscope :cscope suboptions -complete=dir directory names -complete=environment environment variable names -complete=event autocommand events -complete=expression Vim expression -complete=file file and directory names -complete=file_in_path file and directory names in 'path' -complete=filetype filetype names 'filetype' -complete=function function name -complete=help help subjects -complete=highlight highlight groups -complete=history :history suboptions -complete=locale locale names (as output of locale -a) -complete=mapclear buffer argument -complete=mapping mapping name -complete=menu menus -complete=messages :messages suboptions -complete=option options -complete=packadd optional package pack-add names -complete=shellcmd Shell command -complete=sign :sign suboptions -complete=syntax syntax file names 'syntax' -complete=syntime :syntime suboptions -complete=tag tags -complete=tag_listfiles tags, file names are shown when CTRL-D is hit -complete=user user names -complete=var user variables -complete=custom,{func} custom completion, defined via {func} -complete=customlist,{func} custom completion, defined via {func}
command \ -complete=customlist,ListUsers \ -nargs=1 \ Login :call Login(<args>) function+ ListUsers(ArgLead, CmdLine, CursorPos) return ["you", "me", "other"] endfunction
Selection Ranges
function! PrintSelectedLines() range echom getline(a:firstline, a:lastline) " array of selected lines endfunc command -range PrintSelectedLines <line1>,<line2>call PrintSelectedLines()
<Plug> commands
SO: how to execute plug commands https://stackoverflow.com/questions/18546533/execute-plug-commands-in-vim why you should use plug https://whileimautomaton.net/2008/09/27022735
<Plug>
is designed for use within plugins.
- define the modes the command is available in
- define multiple keybindings for the same action
Defining Plug commands
" define function, to be called function Foo() " ... endfunc " define a command, available within insert inoremap <silent> <Plug>Foo <C-R>=Foo()<CR> " map that command inoremap <c-j> <Plug>FooCalling Plug commands
" run function exec "normal \<Plug>function" " bind command for function command MyCommand silent exec "normal \<Plug>(my-command)"