Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

January 3, 2011

How to pipe STDOUT to vim

I usually use ColorDiff when I want to get a colorized diff from the command line.

mike@shiner $ cvs diff -up | colordiff

I was talking with a friend about vim and he showed me how to utilize vim for a similar purpose.

mike@shiner $ cvs diff -up | vim -

This is great because it gives you the same syntax highlighting benefits, but you don't have to install another application.

December 13, 2010

How to execute vim commands from the command line

I was trying to execute a regular expression over multiple lines when I ran across an example of how to execute vim commands from the command line. I happen to know how to do this from inside vim, so this was a serendipitous find. First, I'm going to show an example of how to do a simple replace. Here's the current contents of our test file

mike@shiner $ cat test_regex.txt
this is
a test

We can run the following command to replace 'this' with 'there'.

mike@shiner $ vim test_regex.txt -c '%s/this/there/' -c 'wq'
mike@shiner $ cat test_regex.txt
there is
a test

The -c arguments are executed sequentially. They're equivalent to typing : and then the contents of the argument. In the example above, we do a search/replace and then write/quit the file. If the "-c 'wq'" is omitted, then vim will remain open. Now back to what I was trying to do originally.

mike@shiner $ vim test_regex.txt -c '%s/there\_.*a/regex' -c 'wq'
mike@shiner $ cat test_regex.txt
regex test

The \_ tells vim that the . will include the new line character. Hence, the expression states that we want to match 'there' and 'a' and anything between these characters. This match is replaced with 'regex'.