1

Is it possible to send a command to vim from the command-line? For example, to open the last file something like:

$ vim -e "`0"      # execute `0 after opening vim

Or, if I need to run a command, then:

$ vim -r ":normal!`0"

Is it possible to do this from the command-line before entering vim? When I tried running a normal --cmd arg I get:

$ vim --cmd "normal! \`0"
Error detected while processing pre-vimrc command line:
E20: Mark not set
Press ENTER or type command to continue
0

Most concise:

$ vim +'norm! `0'

In Bash, single quotes imply that their contents will not be expanded. Since backquotes imply that their contents will be evaluated, enclosing the Ex command within single quotes ensures that Bash takes the backquote literally, so it won't be looking for a second backquote to terminate the first one.

Also, vim +{command} is the shorter equivalent to vim -c {command}, meaning you can replace one with the other. When opening vim, you can chain a maximum of 10 of either of them, in the form vim +{command1} +{command2}...

Lastly, if you wanted to run more than 10 Ex commands, you could also chain them with a bar in between, but there are a few exceptions. Below you can see the commands :3d and :x combined in one.

$ vim +"3d|x" fileName.txt
| improve this answer | |
2

You can use the -c option:

-c {command} -- {command} will be executed after the first file has been read (and after autocommands and modelines for that file have been processed). "command" is interpreted as an Ex command. If the "command" contains spaces, it must be enclosed in double quotes (this depends on the shell that is used).

And running it would be:

$ vim -c "normal! \`0"

Note the need to escape the ` in the command-line.

See :h -c for more information.

| improve this answer | |
  • Single quotes obviate the need for escaping. – D. Ben Knoble Jul 4 at 2:15
  • 1
    @D.BenKnoble thanks for that tip. Is there a link I can see as to using " and ' in shell, and why with the later I wouldn't need to escape? – David542 Jul 4 at 2:54
  • 2
    There are probably a few tutorials online around shell quoting; man bash is also good (there are sections on quoting). The gist of it is that single quotes stop all expansion, including backtick expansion, which is a legacy from of $() subshell expansion. Backticks are thus shell meta-characters and need quoted when used for being just a backtick – D. Ben Knoble Jul 4 at 15:33

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.