Home > Tip, vim > My favorite VIM tips

My favorite VIM tips

I love using vim as my text editor. It’s almost always the first program I would try to check if it’s installed on a newly installed Linux system. Below are some of the tips that I find quite useful and use quite often:

1. Moving cursor

gg – move cursor to the beginning of the file
G – move cursor to the end of the file
nG – move cursor to the nth line
Ctrl + o – move cursor to the previous cursor position

2. Multiple lines commenting

1) Shift + v to select the first line
2) move up or down arrow key to select more lines
3) :
4) status line will become :'<,’>
5) complete the line as below, followed by the Enter key
:'<,'>s/^/#/g

3. Multiple Lines deletion

Select and delete

1) shift + v to select the first line
2) move up or down arrow key to select more lines
3) hit letter d

Use direct command

:.,$d – Delete all lines starting from current line (inclusive) to the end of the file
:0,.d – Delete all lines starting from beginning of the file to current line (inclusive)
:1,3,5d – Delete lines 1, 3 and 5
:.,+Nd – Delete current line plus N lines below, N>=1
:.,-Nd – Delete current line plus N lines above, N>=1, you will be prompted ‘Backward range given, ok to swap (y/n)?‘ hit y to accept.

4. Typo correction

This is better explained by using the “the” example: say you want to enter the but instead you type teh. To correct the typo:
1) exit edit mode
2) move cursor to the letter e so it becomes teh
3) enter commands xp

5. Line swapping

Suppose you have the following two lines at the beginning of your bash script

echo "hello"
#!/bin/bash

Obviously you want to swap those two lines so it becomes a valid bash script. Here’s how you do it:
1) make sure you are not in edit mode
2) move cursor to (anywhere) the first line
3) now enter commands ddp

6. File opening tips

vim file1 file2 … -o opens file1, file2, … in stacked windows
vim file1 file2 … -O opens file1, file2, … side by side
vim file + opens file and move the cursor to the last line.

That’s all for now. I will add more if I find more interesting enough to the above list. Of course there are a lot more useful tips using vim. As always I welcome comments and feedback.

Advertisements
Categories: Tip, vim
  1. No comments yet.
  1. No trackbacks yet.

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: