Skip to main content

VI/VIM

VIM is the best Linux text editor.


i insert.
A insert at the end of the current line.
I insert at the beginning of the current line.
o Insert on line below.
O Insert on line above.5784/*/
:set number display line numbers
:255 Skip to a specific line
:/string Search a file for a string. Skip to the next found instance of the string using 'n'
:1,100/test Search a string over a given range of lines




Line Numbers

vim can display line numbers using the below command:

:set number
Skip to a specific line number

To skip to line 255:

:255

Searching

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

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

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

Copy & pastse

Copy the currently selected line
:yy
Copy a range of lines

The below would copy lines 1-1000

:1,1000y
Paste the line:
p

 


Saving & exiting 

save and quit:
:wq
don't save and quit
:q!
Save as new file
:w newfilename