Skip to content

Instantly share code, notes, and snippets.

@githubfoam
Last active February 7, 2025 07:52
Show Gist options
  • Save githubfoam/b63d28193bdc7d7e8200a603dec9337f to your computer and use it in GitHub Desktop.
Save githubfoam/b63d28193bdc7d7e8200a603dec9337f to your computer and use it in GitHub Desktop.
Vi Cheat Sheet
----------------------------------------------------------------------------------------------------
Vi stands for Visual,Vim stands for Vi IMproved.
----------------------------------------------------------------------------------------------------
Type o (lower case) to insert, or open, a blank line below the current line.
Type O (upper case) to insert a blank line above the current line
----------------------------------------------------------------------------------------------------
copy is called yank (y)
cut is called delete (d)
paste is called put (p)
----------------------------------------------------------------------------------------------------
end of file -> press the Esc key and then press Shift + G to move cursor to end of file
----------------------------------------------------------------------------------------------------
:set nonumber #disable the absolute line numbers off
:set number! #toggle the line numbers
----------------------------------------------------------------------------------------------------
:set shiftwidth=2
----------------------------------------------------------------------------------------------------
#shift multiple lines with space
#do not type <space>!
Press "SHIFT + v" to enter VISUAL LINE mode
Select the text,either the cursor keys or the "j" and "k" keys
enter ":"
:'<,'>norm I<space><space> # shift right with 2 spaces,
:'<,'>norm I<space><space><space> # shift right with 3 spaces
----------------------------------------------------------------------------------------------------
#shift multiple lines with space
#do not type <space>!
:2,10norm 10i<space> #insert 10 spaces before lines 2-10
:%norm 5i_ #indent every line in a file with five underscore characters
:%norm 5x #remove first 5 characters from every line
:5,13norm 5x #remove first 5 chars between lines 5-13
----------------------------------------------------------------------------------------------------
vi +80 /etc/apache2/apache2.conf #cursor to line 80
----------------------------------------------------------------------------------------------------
#Permanent Settings,add the appropriate command to your .vimrc (Vim configuration file)
vim ~/.vimrc
:set number
#centos 8
yum list installed | grep -i vim
rpm -qc vim-minimal
cat /etc/virc
vi /etc/virc # add "set number" for line numbers by default
----------------------------------------------------------------------------------------------------
# indent right, tab space
Press "SHIFT + v" to enter VISUAL LINE mode
Select the text,either the cursor keys or the "j" and "k" keys
press "SHIFT + ">" character
# indent right, tab space
Press "SHIFT + v" to enter VISUAL LINE mode
Select the text,either the cursor keys or the "j" and "k" keys
press "SHIFT + "<" character
----------------------------------------------------------------------------------------------------
To comment out blocks in vim:
press Esc (to leave editing or other mode)
hit ctrl+v (visual block mode)
the cursor is at the first char of the first line
use the up/down arrow keys to select lines you want (it won't highlight everything)
Shift+i (capital I)
type # then ESC (two times)
----------------------------------------------------------------------------------------------------
To uncomment blocks in vim:
press Esc (to leave editing or other mode)
hit ctrl+v (visual block mode)
use the up/down arrow keys to select the lines to uncomment(selected char must be "#", not the beginning of lines)
type x then ESC
If you want to select multiple characters, use one or combine these methods:
use the left/right arrow keys to select more text
to select chunks of text use shift + left/right arrow key
you can repeatedly push the delete keys below, like a regular delete button
----------------------------------------------------------------------------------------------------
:5,17s/^/#/ comments out line 5-17
:5,17s/^#/ uncomments out line 5-17
----------------------------------------------------------------------------------------------------
#delete lines 3 to 7 in a file named example.txt
vi example.txt
:3,7d
:wq
----------------------------------------------------------------------------------------------------
enter number and press dd # delete lines of specified number
enter number and press dd and p # cut lines of specified number and paste
enter number and press yy and p # copy lines of specified number and paste
----------------------------------------------------------------------------------------------------
press d or x to delete characters, repeatedly if necessary
----------------------------------------------------------------------------------------------------
:14 -> go to a particular line
vi +36 foo.c -> start at a particular line in a file
----------------------------------------------------------------------------------------------------
:%s/WORD-To-Find-HERE/Replace-Word-Here/g
:%s/pattern/replace/g #Replace every occurrence of pattern with replace in the entire file
:%s/unix/Linux/gi #Case Insensitive Find and Replace
:%s/\<UNIX\>/Linux/gc #Find whole words exactly matching ‘UNIX’ to ‘Linux’; and ask for confirmation
:%s/apple\|orange\|mango/fruit/g #Replace all instances of ‘apple’, ‘orange’, and ‘mango’ with ‘fruit’
:%s/\s\+$//e #Remove trailing whitespace at the end of each line
:s/foo/bar/ #search for the first occurrence of the string ‘foo’ in the current line and replace it with ‘bar’
:s/foo/bar/g #replace all occurrences of the search pattern in the current line
100,200s/UNIX/Linux/g #replace with ‘Linux’ all lines between line 100 and line 250
:%s/<dog\/>/<cat\\> #find all occurrences of <dog/> and replace it with <cat\>
:%s@<doc/>@<cat\\>@ #find all occurrences of <dog/> and replace it with <cat\>
----------------------------------------------------------------------------------------------------
#Refining the Search
/^Search #find the next line beginning with “Search”
/search\.$ #find the next line ending with “search.”
/\<search\> #find the next occurrence of the word—as opposed to the string—“search”
/.isinformation #find the next occurrence of “disinformation” or “misinformation,” any character, type a period (.) in the string at the location to be matched
/[a-z]*isinformation #find all strings beginning with a through z and ending with “isinformation” and to find all occurrences of the string “isinformation”
----------------------------------------------------------------------------------------------------
#Certain special characters ( / & ! . ^ * $ \ ?) have special significance to the search process and must be “escaped”
/anything\? #search for the string “anything?” type /anything\? and press Return
----------------------------------------------------------------------------------------------------
:g/search-string/s//replace-string/g
:g/disinformation/s//newspeak/g #replace every occurrence of the string “disinformation” with “newspeak,”
:g/disinformation/s//newspeak/gc #The following command uses gc (adding c for “consult”) to make vi stop at every occurrence of “disinformation”
----------------------------------------------------------------------------------------------------
$ moves to the last character on the line.
0 (zero) gets you to the beginning of the line incl. whitespace
----------------------------------------------------------------------------------------------------
Vim
----------------------------------------------------------------------------------------------------
:set hlsearch
:highlight Search cterm=standout ctermbg=white ctermfg=red #customize
cterm is for formating
ctermfg is for foreground color
ctermbg is for background color
----------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment