Vim is the main text editor available on the Linux Command Line Interface to create and edit files. The following shows some commands you will find useful. By default, Vim starts in command mode.
To create/edit a file in the Vim editor run the below command
vim filename
Search
Type / then search term and press enter. Press n for next instance and N for previous instance.
If you want to search and replace the pattern in the entire file, use the percentage character. To replace all occurrences of the search pattern, use the g flag.
:%s/foo/bar/g
If the string part is omitted, it is considered as an empty string, and the matched pattern is deleted.
:%s/foo//g
Will return the number of matches and the number of lines those matches occur on, without doing any substitution
:%s/some_pattern//n
To escape a special character, precede it with a backslash ( \ ). For example, to search for the string “anything?”
/anything\? and press Return
For more information on searching and replacing with Vim
https://docs.oracle.com/cd/E19253-01/806-7612/editorvi-62/index.html
Will show line numbers
:set nu
Hide line numbers
:set nonu
Go to a specific line number
:<line number> and press the enter button
To quit
:q
Saves the file you’re working on but don’t exit
:w
To save and quit
:wq
To save and throw away any unsaved changes
:q!
To undo a change
u
To redo a change
ctrl + r
Move to the top of the screen
H
Move to the middle of the screen
M
Move to the bottom of the screen
L
Jump to the end of the line
$
Jump to the start of the line
0
Go to the first line of the document
gg
Go to the last line of the document
G
Move one word to the right
w
Move ahead three words
3w
Move one word to the left
b
Move back three words
3b
Delete the current line
dd
To delete a range of lines the syntax is as follows:
:[start],[end]d
Example: :3,5d In this command the editor will delete the lines from 3 to 5
Delete all lines at or below the current one
dG
Duplicate the current line
yyp
Copy the current line
yy
Paste the contents of the buffer after the cursor position
P
Switch to insert mode to start adding text to your file
i
Switch to command mode
Esc
Find out the latest news, updates, scripts, tips and more on the official Vim website:
One of the coolest plugins for Vim is the Matrix screensaver
Install details
add matrix.vim script to plugin directory
start screensaver with :Matrix, press keys a few times to exit