Paul - The Programmer

simple & stupid

Auto format XML file

1. Format XML file with VIM.

VIM's auto indent feature supports format the XML files. But generally this feature may be disabled by default. 

To enable the auot indent feature, just add the below options in the .vimrc in user's home directory.

filetype indent on

Next time when the xml file be opened by VIM, the xml file type's indent script will be auotmatically loaded as well. In my environment, the indent script for xml is /usr/share/vim/vim72/ftplugin/xml.vim. The xml.vim's path may vary in different system.

To format the XML contents, just use the vim command in the command mode:


This command will reformat the entire contents.

2. Fomart XML file with xmllint.

The xmllint are generally used for validating the xml file syntax. But it also has the ability to format and align the XML documents. The XMLLINT_INDENT environmnet variable controls the indentation. The default value is two spaces ( "  " ).

Basically, open the xml file with VIM then execute the below vim command in command mode:


:%!xmllint --format %

This command means the xmllint be excuted with the entire xml document contents as input, then the result of xmllint will be used to update the content of current buffer.

The xmllint also do some refine for the xml document, such as, adding the xml version declaration in the document's head.

Remove empty lines by VIM global command

The VIM global command can select the lines that match a certain pattern in the scope of whole file. And the appened commands can operate on the selected lines.

Either the following command can be used to remove the empty lines.


The command ':v' is similar to the command 'grep -v' which select the inverted matched lines.

\S is the meta character for the characters which is not white space or tab.

Alter the word order in lines by VIM regular expression

Somehow, I am required to alter the order of word in lines in a text file.

For example, the input text file is like below.

hello world
hello paul
this  is
for   test

I want this file be updated was below.

world hello
paul hello
is  this
test   for

Thanks to regular expression, I do not need to update the lines one after another.

The vim regular expression is a little different than the Perl's. But most of syntax are similar.

\w  : word charaters, includes both alphabet and number characters.

\s   : space characters.

*   : match zero or more preceding character or meta character like \s, \w.

The patterns can be grouped by enclosing with \( and \). 

& :  the whole matched pattern

\0:   the whole matched pattern

\1:   the matched pattern in the first pair of \( and \)

\2:   the matched pattern in the second pair of \( and \)

So, to accomplish the above task, the substitution regular expression is :



Add prefix and append suffix to each line of text file

I need to update one text file to add prefix and suffix to each line. For instance, let's say the text file I need to update is like:


I want the file be updated as below.

prefix line-number-one suffix
prefix line-number-two suffix
prefix line-number-three suffix

Here are my solutions.

To update with vim:

:%s/.*/prefix & suffix/g

To update with sed:

$ sed 's/\(.*\)/prefix $1 suffix/' sample-file.txt

To update with awk:

$ awk '{ printf( "prefix %s suffix\n", $1 ); }' sample-file.txt


Convert file format from dos to unix

Convert file format by sed

$cp you-file-name your-file-name.bak
$sed 's/^M//g' your-file-name.bak > your-file-name

Covert file format by Perl

$perl -i.bak -pe 's/^M//g;'  your-file-name

Covert file format by vim

:setlocal ff=unix