Consider the file tmp.txt whose contents are:


I want to open it in VIM and move cursor to the c character. So I run VIM with the arguments:

$ vim -c "/c" tmp.txt

But it sets the cursor on a. It looks like VIM was able to find c but placed the cursor at the line begin. Why does it work different if I execute /c in VIM normal mode when file is open?

  • @SatoKatsura When I run your version the cursor is placed on the first line. Have you tried it before suggesting? – ka3ak Jul 5 '16 at 18:32
  • -c executes Ex commands, so your /c is actually equivalent to :/c. There doesn't seem to be any way around this. – Satō Katsura Jul 5 '16 at 19:30

You can position the cursor on the first match using the -s (script) option. According to the vim manual:

-s {scriptin}
The script file {scriptin} is read. The characters in the file are interpreted as if you had typed them. The same can be done with the command ":source! {scriptin}". If the end of the file is reached before the editor exits, further characters are read from the keyboard.

You could use a temporary file with the keystrokes, or even (if you are using bash) process substitution. For example:

vim -s <(printf '/c\n') tmp.txt

This approach works with more complicated searches than a single character.

  • Thank you very much. Works! As you've correctly recognized there won't be only single characters I'll have to search for. I've just tried to keep my SSCCE as short as possible. – ka3ak Jul 6 '16 at 7:07

First attempt

I considered using the +/ command option which is documented in the man page as

+/{pat} – For the first file the cursor will be positioned on the first occurrence of {pat}.

However, the description in the man page is misleading, i.e., the cursor is actually placed on the first character of the first line that contains the search pattern, similar to how search works when using line-based ex commands.

Update: As of Vim 8.1, its man page has been updated to reflect the actual behaviour of this option:

For the first file the cursor will be positioned in the line with the first occurrence of {pat}

Working solution

A better solution is to use the -c option with :execute (required to so that \n can be used to pass a newline character to the :normal command) to carry out the search as if you already had Vim open in Normal mode.

General solution:

vim -c 'execute "normal /pattern\n"' filename

Solution for above example:

vim -c 'exe "norm /c\n"' tmp.txt
  • For an unknown reason "vim +/c tmp.txt" places the cursor not on "c" but on the begin of the line that contains "c". – ka3ak Jul 6 '16 at 7:09
  • @ka3ak I've clarified in my answer that the behaviour of Vim differs from how it’s described in the man page. While the +/ command option does not behave as you’d desire, it’s still a useful feature – along with the option to open a file with the cursor positioned on a specific line number. – Anthony Geoghegan Jul 6 '16 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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