Skip to main content

VI/VIM

 

 

Line Numbers

vim can display line numbers using the below command:

:set number
Searching strings

To search for a string in a text file, the / option can be used. In the below example, we're searching for the word test

:/test

Deleting Lines

Vim can delete lines using a number of methods

Remove X amount of lines.

 The below command would remove 5 lines from (and including) the current line:

:5d
Remove a range of lines:
1,5d

Search and replace

Search and replace a word through whole file:
:%s/wordtosearch/wordtoreplacewith
Search and replace on specific lines

We can couple the search and replace function of vim with the line number range tool. The below would find any instances of the word 'test' within the line range of 1-100, and then replace all instances of the word with 'testing':

:1,100s/test/testing