Skip to main content

VI/VIM

Line Numbers

vim can display line numbers using the below command:

:set number

Skip to specific line number

To skip to line 255:

:255

------------------------------------------------------------------------------------------------------------------------------------------  

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

To skip to the next found instance of the word, press the n key.

Search string on a range of lines

In this example, I'm searching for the string 'test' on lines 1-100:

:1,100/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 the 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

------------------------------------------------------------------------------------------------------------------------------------------