I'm trying to create a custom key mapping which start bring up commandline mode with some text inserted. I would like to have the cursor position not at the end but in a specific position, like this:


Where | represents the cursor.

It would be perfect if the cursor could be positioned to the nextposition place if user types <Tab> or some other key stroke.

How to achieve this?

  • 1
    With <Left><left><Left>.
    – romainl
    Commented Aug 5, 2016 at 6:03

1 Answer 1


You can use that:

nnoremap <key> :s//nextposition/g<c-b><right><right>

The mapping will first go into command mode: :

Then write s//nextposition/g in the command lines

Go to the beginning of the command <c-b>

And move to the desired position: <right>

Some interesting command line mappings can be found here: :h cmdline-editing

Now about the part where you want to move the cursor to another place with <tab> it is something that you can do in insert mode with a snippet engine like UltiSnips but I'm not sure you can make it work in command line.

Also not exactly related but Damian Conway has two pretty interesting mappings described in his how I vim:

First he says that to simplify his substitutions he created the following mapping:

nmap  S  :%s//g<LEFT><LEFT>

So he can press S and then just pattern/replacement to do the substitution.

Then to improve it he created the following:

nmap <expr>  M  ':%s/' . @/ . '//g<LEFT><LEFT>'

This way he can make a search /pattern, see the results and simply type M followed by the replacement string and the pattern is automatically added to the substitution command. (Note that it is convenient to see the pattern in the command but doing :%s//replacement/g will also use the last search pattern)

I know that it is not what OP is asking for but I thought it could be an inspiration source.

  • +1000 Wow, thank you, I'm using this for vifm
    – SdSaati
    Commented Sep 10, 2020 at 16:15

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.