How do I create a custom Vim operator that works with any motion or text object?
:set opfunc and g@
Vim's operatorfunc option lets you define your own operators — just like the built-in d, y, or c — that accept any motion or text object.
:set opfunc and g@
Vim's operatorfunc option lets you define your own operators — just like the built-in d, y, or c — that accept any motion or text object.
:let view=winsaveview() | {cmd} | call winrestview(view)
When writing Vimscript functions or mappings, commands like :substitute, gg, or :%normal will move the cursor and change the scroll position.
:breakadd func {funcname}
Vim has a built-in debugger for Vimscript that most users never discover.
:%s/\<\w\+\>/\=toupper(submatch(0))/g
The \= flag in the replacement part of :substitute tells Vim to evaluate what follows as a Vimscript expression instead of treating it as a literal string.
:put =map(getreg('a', 1, 1), 'toupper(v:val)')
By using getreg() with the list flag and applying map(), you can transform register contents with any Vimscript function before pasting.
<C-r>=expand('%:t')<CR>
In insert mode, = opens the expression register.
:source % or :'<,'>source
The :source command executes Vimscript from a file.
command-line #command-line #vimscript #workflow #development
:%s/pattern/\=MyFunc(submatch(0))/g
The \= prefix in Vim's substitute replacement invokes the expression register, which can call any Vimscript function.
:let i=1 then use <C-r>=i<CR> in macro
By combining a Vimscript variable with the expression register inside a macro, you can create a counter that increments on each replay.
<C-r>=expression<CR>
The expression register ("=) evaluates Vimscript expressions and returns the result.
registers #registers #insert-mode #expression #calculator #vimscript