Tag: vim

Have I ever said that VIM is awesome?

I am learning new things about vim all the time. Today’s revelation was the pipe symbol (|). I had to update a function call across multiple files, here is what I was doing.

  1. cd to the root of the project
  2. vimgrep functionname ** to get the list of matching files.
  3. Now I wanted to modify the call in all files, but only after reviewing the individual call. Initially I was doing individual %s followed by w and cn, but given the large number of files this seemed very inefficient (I don’t want carpal tunnel syndrome!).

So, a quick search revealed the pipe/bar (|). This lets you specify multiple commands at once. With this new knowledge, I replaced step 3 above with…

:%s | w | cn

…and now I can quickly review the call before doing the modification.

Cleaning up XML using VIM

Here is a quick function for cleaning up XML without any line breaks. This is a quick and dirty solution with some minor issues (e.g. turning <test></test> to <test>\n</test>), but the goal of this it not to be too accurate, but to quickly put a non-readable XML into a readable form for reference.

1
2
3
4
5
6
function! PrettifyXML()
	set ft=xml
	:%s/></>\r</g
	:0
	:norm =G
endfunction

Put this in your vimrc file and call it using :call PrettifyXML()

Quick vim tip: Copying matching lines to the end of file

I recently had the need to cleanup a long diff file and move certain lines to the end of the file for further analysis. The diff file that I got was something like the following, only much longer (produced using diff -qr xxx yyy).

Files xxx/abc1 and yyy/abc1 differ
Only in xxx: cde1
Files xxx/abc3 and yyy/abc3 differ
Files xxx/abc4 and yyy/abc4 differ
Only in xxx: cde2
Only in xxx: cde5
Files xxx/abc5 and yyy/abc5 differ
Only in xxx: cde3

I didn’t care as much about the “Only…” lines, but the files that differed needed more attention. To accomplish this I wanted to get this file in the following format.

Only in xxx: cde1
Only in xxx: cde2
Only in xxx: cde5
Only in xxx: cde3
Files xxx/abc1 and yyy/abc1 differ
Files xxx/abc3 and yyy/abc3 differ
Files xxx/abc4 and yyy/abc4 differ
Files xxx/abc5 and yyy/abc5 differ

I could go in and copy and paste all the lines that matched, but that would be a lot of manual work. So, I looked around for a minute and came up with the following command.

:%g/^Files/m$

Vim is awesome!