Virenk's Blog

Archive for November 2010

Some useful vim utilities.  Have been using these, thought people might find them useful. Will add more later.

#Paste contents of another file in current cursor location.
:r file2.txt
will substitute contents of file2 in current location.


#Deleting after a pattern till the end of the line. (including the pattern itself)
:g/pattern/normal nd$

pattern = pattern, normal is the mode
n => at each matching line look for next occurance of pattern matching word.
d => delete
$ => delete till the end of the line

I am doing good.
You should be doing this right now.

:g/doing/normal nd$

will result in : (content after ‘doing’ is removed)
I am
You should be

To retain pattern word, you can do:
:g/doing/normal nwd$

Here, ‘w’ advances the cursor to next word.

#Deleting before a pattern till the beginning of the line.

:g/pattern/normal nd^
I am doing good.
You should be doing this right now.

:g/doing/normal nd^

will result in : (content before ‘doing’ is removed)
doing good.
doing this right now.

# Map command:
map command can be used to map a sequence of steps into one single key mapping.

comment a line using map.

:map <F3> 0i/*<ESC>A*/<ESC><enter>

comment would look like:
>>before comment:
hello how are u.

>>after comment:
/*hello how are u.*/

Just place the cursor on the line that needs comment, and then press <F3>

Assuming we are in non-edit mode:
<F3> => the key used for mapping.
0i => 0 moves cursor to beginning of line.
i => start insert mode.
/* => write the beginning of comment
<ESC> the escape key to come out of insert mode.
A => moves cursor to end of line and enables the insert mode.
*/ => put the close of comment.
<ESC> come out of insert mode.
<enter> press enter key

#Execute a sequence of commands in a vim script file and apply to any no. of files.
Extremely useful when u have to do same editing on multiple files.

put the commands in script.vim and do
$vim  -u NONE -s script.vim <filename>

If u have to replace every occurance of ‘India’ with “INDIA’, in file1, then u can put this in script file (script.vim):

and then execute following command in shell:
$vim -u NONE -s script.vim  file1

vim will execute the commands from script.vim and will save and close the file.

To execute same set of operations on multiple files :
Do above editing on all files which have .txt extension.
$ls *.txt | while read f ; do vim -u NONE -s script.vim $f ; done;

# Search for exactly 4 digit numbers in the file:

#Move things in newline
Hello how are u. I am fine . Hope everything is good.

>>will result in:
Hello how are u.
I am fine .
Hope everything is good.


#Search for exact match of a pattern
will exclude lines where ‘world’ occurs as a sub string, like myworld.


#Remove all lines which match a given pattern.

#Delete a block of text enclosed in braces.
Example (input file):
(a b c d)
test 1
test 2
test 3
{ test test
{ test

:s/{/normal d%
will delete all {} braces and data between them.
:s/(/normal d%

will delete () braces and data between them.

% => matches the current open bracket to its matching close bracket.

# See the difference between two files in vim
$vim -d file1 file2
Select two (or more files) and select ‘diff with vim’ option.

# Execute a command and see its result.
will print out the name of your computer. <works on both linux and windows>
On windows try using GVIM.

Pls let me know if there are any mistakes in above.



  • None