Virenk's Blog

Some useful Vim (editor) commands

Posted on: November 23, 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

example:
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^
example:
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.

Example:
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>

Example:
If u have to replace every occurance of ‘India’ with “INDIA’, in file1, then u can put this in script file (script.vim):
:%s/\<India\>/INDIA/g
:wq

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:
:\<\d\d\d\d\>

—————————————————————–
#Move things in newline
Hello how are u. I am fine . Hope everything is good.
:%s/\./\r\./g

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

——————————————————————————————-

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

—————————————————————————-

#Remove all lines which match a given pattern.
:g/pattern/d

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

Then:
: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
GVIM:
Select two (or more files) and select ‘diff with vim’ option.

# Execute a command and see its result.
:!hostname
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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: