1

I want to script vim to edit files from the command line. For example I want to do something along the lines of:

vim -<SOME_OPTION> 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>'

or:

echo 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>' | vim

and have it save the file helloworld.txt with a body of hello world

Is this possible? I've tried a few different approaches but none seem to do it. I realize I can do things like vim +PluginInstall to run Ex commands from the command line, but I'd love to be able to string together arbitrary motions

3
  • Possible duplicate (in another section of StackExchange): unix.stackexchange.com/questions/34983/… Commented Jul 4, 2016 at 13:35
  • That's pretty close to what I want but not quite. If there's an ex command that lets me run arbitrary key presses like I can with mappings then that would definitely do the job Commented Jul 4, 2016 at 13:38
  • Doesn't set -o vi do what you want? Commented Jul 4, 2016 at 13:41

2 Answers 2

1

This can be achieved with the + flag and the :normal command:

$ vim +"norm Iworld" +"norm Ihello " +"wq helloworld.txt"
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, not quite as elegant as I was hoping for but that gets the job done. Thanks!
0

I think what you are looking for is vim's -w/W and -s {scriptin} option. Well in your case you should make a scriptfile, and with -s file to let vim execute all your "key presses"

I think vimgolf has used these options too.

2 Comments

Cool I'll peruse vimgolf for some examples
@JoshBodah you don't need the tmp file if you do vim -s <(echo "whatever")

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.