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?

  • What do you mean "on a movement"? If you want to visually select and go into cmdline mode, then map a key.
    – 3N4N
    Nov 13, 2022 at 15:57
  • @3N4N Vim uses movements. dt( deletes till the next (. I want to run a colon command on a movement like t(.
    – Ana
    Nov 13, 2022 at 15:58

3 Answers 3


Some (not all) text objects have corresponding marks. In this case, the equivalent is roughly


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')
  execute "'[,']" . s:cmd

function! s:setup() abort
  unlet! s:cmd
  set operatorfunc=MotionCmd
  return 'g@'

nnoremap <expr> g: <sid>setup()

How it works:

  1. When typing 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.
  2. The first time MotionCmd is invoked, there is no command, so we take input in command completion mode. Otherwise, the user is probably using . so we don't need to get the command again.
  3. Finally, we run the command over the spanned lines.

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...

Other Ranges

  • :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 k

and 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.