Wednesday, November 13, 2013

VIM tips

Vim in read-only mode : view

Did you know that vim has a read-only mode ?
Just use :
# view myfile.txt

Searching for the current word :

In normal mode, move the cursor to any word. Press * to search forwards for the next occurrence of that word, or press # to search backwards.

 Delete "empty" lines : 

Sometime you can be annoyed by "empty" lines which contains no character in your file, to delete them you can use :
:g/^$/d

:g will execute a command on lines which match a regex. The regex is "empty line"  (start with nothing and end with nothing) and the command is :d for delete

Delete "blank" lines : 

Beside "empty" line, you might want to delete lines which contains only whitespaces characters (spaces/tabs) in your file :
:g/^\s*$/d

Visual block editing :

Activate the visual block mode :
CTRL+v
Insert Mode before every line of the block :
SHIFT+i
Apply the insert :
ESC

No comments:

Post a Comment