4

Is there a way to call vi/vim from the shell to format text? It would be like running a vim-golf sequence from the command line.

It is so easy to compare effects of commands in vim that I can easily test a few options and identify a workable solution. I would like to be able to incorporate those command sequences into command line options.

I have tried something simple like vi -c ":%s/./x/" but it that doesn't seem to work at all in msysgit vim. Really, I would only expect it to do that change and show me the open buffer.

Note: vi -c ':%s/./x/' (with ' instead of ") does run the command on the buffer, but it is still kept in the buffer and not pushed back to stdout. Looks like Windows or msysgit was trying to magic my % (fixed throughout the rest of the question).

What I want to be able to do is use it like you would grep/sed/awk/tr. The defaults are nice and not having to translate line break options from vim to the others would be helpful.

What I really want is a command line option for vim, say --doit='...' where I could specify what I would type, have it do that and spit out the resulting buffer. Something like this would spit out and x for every character in the result of an ls -l command:

ls -l | vi --doit=':%s/./x/g' -

This would reformat text according to linebreaks (assuming textwidth and formatting options are set correctly):

vi --doit='ggqG' sometextfile.txt

It is difficult to search for a solution to this because of the convolution between shell command line and the vim command mode line.

Anyone know if this is possible?

  • Perhaps if you find a form of 'vipe' (moreutils) that runs in msysgitwindows. – VanLaser Aug 18 '15 at 23:41
6

You can launch vim using two flags to accomplish this. The two flags are -e and -s. When vim is launched with the -e flag, vim automatically enters ex mode, which is just vim's command line and nothing else. The -s flag means that vim is silent, which means that nothing is displayed. This wouldn't be very useful if you couldn't pass commands via flags. However, there are two options which can accomplish this. They are -c "{ex command}" and +"{ex command}". You can add up to 10 of these to a single command. For example, say I wanted to replace the word "foo" with "bar" in a file. Here is what I would do:

vim -es +"%s/foo/bar/g" +"wq" foo.txt

This would launch vim in ex mode, run the command %s/foo/bar/g, and then write to the file and quit using the wq command.

You can see more help on these command line flags by running vim -h.

  • Maybe I should have been more specific. I am trying to pipe commands. This is most of the answer, but there have been quite a few gotcha's in the piping process that I am still trying to work out. That and you still can't use " -- must use ' in msysgit. – dhj Aug 18 '15 at 22:52
  • Ah yes, I will add an explanation on that. (once I figure it out) – EvergreenTree Aug 19 '15 at 0:01
  • 1
    Hm, I see what your issue is now. Vim can't seem to read from stdin if it is started in ex mode. I'll have to look into it more. – EvergreenTree Aug 19 '15 at 12:46
2

You can use vim as a command server as described in here: http://vimdoc.sourceforge.net/htmldoc/remote.html

Here is a similar question which describes hot to use it: https://stackoverflow.com/questions/8507687/send-commands-to-vim-programmatically

  • Very cool. I was looking for a command-line / shell solution, but this may be a workable substitute. – dhj Aug 18 '15 at 20:56

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.