My init.vim code

autocmd FileType cpp nnoremap <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt

I want my cursor position to be exact 31 times <left> in command line.
or after ~/Downloads/

Like this
enter image description here

PS: I got this idea from this shortcut nmap <leader>s :%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>
here the cursor goes 3 char left in command line

but if i use <left> it will take 31 left's in my init.vim code to go in that position
I couldn't find any simple solution. Maybe you folks can help me out a bit.

2 Answers 2


There is no straightforward way to tell vim to land the cursor at a particular place in the cmdline during a map, but there are a number of workarounds.

  1. Direct positioning
autocmd FileType cpp nnoremap <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt<c-r>=setcmdpos(getcmdpos()-31)[1]<cr>

You can use setcmdpos() to place the cursor anywhere on the cmdline in an expression. The only trick here is to have it not enter any text by indexing [1] (which is invalid, and returns empty string).

  1. Alternative to <left>
autocmd FileType cpp nnoremap <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt<s-left><s-left><s-left><s-left><s-left><left>

shift-left moves more than left, thus you will need fewer movements.

  1. Using repeat()
autocmd FileType cpp execute 'nnoremap <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt' . repeat('<left>', 31)

You can create the sequence of <left>s with an expression using the expression register. The map itself will still be quite ugly but the code is cleaner.

  • thanks @mass .. i tried and these code works fine just as i expected.. i'll use with the <s-left> one though
    – Mega Bang
    Commented Sep 5, 2021 at 16:20
  • the third one seems to have a problem.. I'm using neovim v0.5 .. it shows invalid arguments for function repeat
    – Mega Bang
    Commented Sep 5, 2021 at 16:24
  • @MegaBang thanks, fixed (in a somewhat different way, but which is more instructive)
    – Mass
    Commented Sep 5, 2021 at 16:41
  • 1
    there's a small bug though.. tried to fix it but this system doesn't let me.. there won't be a backtick here after closing bracket.. it will be just repeat('<left>', 31) .. vim throws an erorr "invalid expressions: `"
    – Mega Bang
    Commented Sep 5, 2021 at 16:48
  1. Normally prefer :h ftplugin over autocmd FileType. If still want autocmd, at least, put it into augroup.
  2. Use :h map-<buffer> to define a local mapping. Otherwise it will affect all other buffers/windows too.
  3. Typing foo[cursor]bar is as simple as bar<C-B>foo.
  • how can i get the normal order foobar (not barfoo)?
    – Mega Bang
    Commented Sep 5, 2021 at 15:07
  • 1
    @MegaBang As I said, typing "bar" then get to line start then type "foo". Is there any problem with this?
    – Matt
    Commented Sep 5, 2021 at 15:25
  • i rewrote my code just like you said, it works like a charm... but i wonder if there's any straight-forward configuration foo[cursor]bar
    – Mega Bang
    Commented Sep 5, 2021 at 15:29

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.