When I substitute text within a paragraph, I've always visually selected (e.g. vap), then :s/ ....
How to do this without the visual selection? More generally, how to apply a colon command on a movement / text object like ap?
When I substitute text within a paragraph, I've always visually selected (e.g. vap), then :s/ ....
How to do this without the visual selection? More generally, how to apply a colon command on a movement / text object like ap?
Some (not all) text objects have corresponding marks. In this case, the equivalent is roughly
:'{,'}substitute…
See :help motion.txt or :help mark-motions for some of these. In general, :help [range].
Here is a relatively simple implementation where g:{motion|object} will prompt for a command, and run it over the lines spanned by the motion. Since commands are linewise, the characters of the motion are ignored, only the lines matter. This approach also works with repeat ., where the previously command entered will be used.
function! MotionCmd(...) abort
if !exists('s:cmd')
let s:cmd = input(':', '', 'command')
endif
execute "'[,']" . s:cmd
endfunction
function! s:setup() abort
unlet! s:cmd
set operatorfunc=MotionCmd
return 'g@'
endfunction
nnoremap <expr> g: <sid>setup()
How it works:
g: we set up things by clearing the stored command, setting an operator function and finally using g@ to place us in operator pending mode. vim waits for additional user input before proceeding.. so we don't need to get the command again.Not sure if that is exactly what you need, but there are ranges in vi/vim/nvim. (:help range)
If you ever pressed : while you were in visual mode, you probably have seen this: :'<,'>. This is a range and basically means:
: (execute) from '(mark) <(special range char, first line/char) , (to) '(mark) > (special range char, last line/char) <command>
In short: execute <command> to the selected text
Lets see some examples...
:2,5 <command> -> execute <command> on lines 2-5:-1,+1 <command> -> execute <command> from relative -1, also known as above the cursor to relative +1, one line above cursor:.,33 <command> -> execute <command> from current cursor position to line 33:/someword/+1<command> -> execute <command> on 1 line after the line which contains "someword":'d,'k -> execute <command> on text from mark d to mark kand many more, as said before, you can read more on help range
Yes I know, you can't execute colon commands on am movement like for example ap... But this is the closest thing I know is possible other than mapping custom key-bindings.
dt(deletes till the next(. I want to run a colon command on a movement liket(. – Ana Nov 13 '22 at 15:58