You could use operatorfunc
. For example:
Define a function like this in your .vimrc:
function! DeleteEmptyLines(type)
if a:type == 'line'
silent execute ".,'\"g/^$/d"
endif
endfunction
And a mapping:
nnoremap <silent> ,d :set operatorfunc=DeleteEmptyLines<CR>m"g@
,d
performs now just like an operator and accepts a (line-based) motion. You can, for example, insert ,d5j
or ,dG
in normal mode to delete empty lines in the next 5 lines or to the end of file.
You can find more information on how to extend this functionality here:
http://learnvimscriptthehardway.stevelosh.com/chapters/33.html
and of course::h operatorfunc
and :h map-operator
.
From :h map-operator
:
"An operator is used before a {motion} command. To define your own operator
you must create mapping that first sets the operatorfunc
option and then
invoke the g@
operator. After the user types the {motion} command the
specified function will be called."