DEV Community

Cover image for MASTERING VIM – A DEVOPS ESSENTIAL!

MASTERING VIM – A DEVOPS ESSENTIAL!

【 πŸ‘¨πŸ»β€πŸ’» YouTube 】
Watch more other contents related DevOps - AWS

Vim Cheat Sheet & Quick Reference

Getting Started

Motion Diagrams

β–Ό/β–Ά Cursor   β–½/β–· Target

Enter fullscreen mode Exit fullscreen mode

Left-right motions

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ |      
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ 0      $ ──────────────┐ 
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ ^      fe ────────┐    β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€ Fo     te ───────┐│    β”‚
β”‚  β”‚  β”‚β”Œβ”€β”€β”€β”€β”€β”€ To     30| ───┐  β”‚β”‚    β”‚
β”‚  β”‚  β”‚β”‚ β”Œβ”€β”€β”€β”€ ge     w ───┐ β”‚  β”‚β”‚    β”‚
β”‚  β”‚  β”‚β”‚ β”‚ β”Œβ”€β”€ b      e ─┐ β”‚ β”‚  β”‚β”‚    β”‚
β”‚  β”‚  β”‚β”‚ β”‚ β”‚  β”Œh      l┐ β”‚ β”‚ β”‚  β”‚β”‚    β”‚
β–½  β–½  β–½β–½ β–½ β–½  β–½β–Ό      β–Όβ–½ β–½ β–½ β–½  β–½β–½    β–½
   echo "A cheatsheet from quickref.me"

Enter fullscreen mode Exit fullscreen mode

Up-down motions

                 - SCREEN 1 START
   β”Œβ”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β–· #!/usr/bin/python
   β”‚ β”‚     β”Œβ”€β”€β”€β–·     
   β”‚ β”‚     β”‚     print("Hello")
   β”‚ β”‚     { } β–Ά print("Vim")
   β”‚ β”‚       β”‚   print("!")
   β”‚ β”‚       └─▷    
   β”‚ β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β–· print("Welcome")
G gg H M L k j β–Ά print("to")
β”‚        β”‚   └─▷ print("quickref.me")
β”‚        β”‚       print("/vim")
β”‚        β”‚     
β”‚        └─────▷ 
β”‚                - SCREEN 1 END
└──────────────▷ print("SCREEN 2")

Enter fullscreen mode Exit fullscreen mode

Motions

Shortcut Description
h j
/ Half-page up/down
/ Page up/down

Words

Shortcut Description
b / w Previous/Next word
ge / e Previous/Next end of word

Line

Shortcut Description
0 (zero) / $ Start/End of line
^ Start of line (non-blank)

Character

Shortcut Description
Fe / fe Move to previous/next e
To / to Move before/after previous/next o
/ n

Document

Shortcut Description
gg / G First/Last line
:n nG
} / { Next/Previous empty line

Window

Shortcut Description
H / M / L Top/Middle/Bottom screen
zt / zz / zb Top/Center/Bottom this line

Insert Mode

Shortcut Description
i / a Insert before/after cursor
I / A Insert start/end of line
o / O (letter) Insert new line below/above
s / S Delete char/line and insert
C / cc Change to end of/current line
gi Insert at last insert point
Esc

Saving and Exiting

Shortcut Description
:w Save
:q Close file
:wq :x
:wqa Save and quit all files
:q! ZQ
:qa Close all files
:qa! Force quit all files
:w now.txt Write to now.txt
:sav new.txt Save and edit new.txt
:w !sudo tee % Write to readonly file

Normal Mode

Shortcut Description
r Replace one character
R Enter Replace mode
u / 3u Undo changes 1 / 3 times
U Undo changes on one line
J Join with next line
/ 5 Redo changes 1 / 5 times

Cut and paste

Shortcut Description
x Delete character (Cut)
p / P Paste after/before
xp Swap two characters
D Delete to end of line (Cut)
dw Delete word (Cut)
dd Delete line (Cut)
ddp Swap two lines
yy Yank line (Copy)
"*p "+p
"*y "+y

In visual mode

Shortcut Description
d x
s Replace selection
y Yank selection (Copy)

Repeating

Shortcut Description
. Repeat last command
; Repeat latest f, t, F or T
, Repeat latest f, t, F or T reversed
& Repeat last :s
@: Repeat a command-line command

Visual mode

Shortcut Description
v Enter visual mode
V Enter visual line mode
Enter visual block mode
ggVG Select all text
> / < Shift text right/left

Macros

- -
qi Record macro i
q Stop recording macro
@i Run macro i
7@i Run macro i 7 times
@@ Repeat last macro

You can save macro for any letters not just i

Vim Operators

Usage

Shortcut Description
d w
Operator Motion

Combine operators with motions to use them

Available Operators

Shortcut Description
d Delete
y Yank (copy)
c Change (delete then insert)
p Paste
= Formats code
g~ Toggle case
gU Uppercase
gu Lowercase
> Indent right
< Indent left
! Filter through external program

Examples

Combination Description
dd Delete current line
dj Delete two lines
dw Delete to next word
db Delete to beginning of word
dfa Delete until a char
d/hello Delete until hello
cc Change current line, synonym with S
yy Copy current line
>j Indent 2 lines
ggdG Delete a complete document
gg=G Indent a complete document
ggyG Copy a whole document

Counts

[count] <operator> <motion>
<operator> [count] <motion>

Enter fullscreen mode Exit fullscreen mode

Combination Description
2dd Delete 2 lines
6yy Copy 6 lines
d3w Delete 3 words
d5j Delete 5 lines downwards
>4k Indent 4 lines upwards

Vim Text objects

Usage

Shortcut Description -
v Β  Β  Β  Β  i / a p
Operator inner / around Text object

Operate with an operator inner or around text blocks

Text objects

Shortcut Description
p Paragraph
w Word
W WORD (surrounded by whitespace)
s Sentence
[ ( { < A [], (), or {} block
] ) } > A [], (), or {} block
' " ` A quoted string
b A block [(
B A block in [{
t A HTML tag block

See :help text-objects

Delete

Shortcut Description
diw Delete inner word
dis Delete inner sentence
di" Delete in quotes
da" Delete in quotes (including quotes)
dip Delete a paragraph

Selections

Shortcut Description
vi" Select inner quotes "..."
va" Select quotes "..."
vi[ Select inner brackets [...]
va[ Select brackets [...]
viw Select inner word
vip Select inner paragraph
vipip Select more paragraph

Misc

Shortcut Description
ciw Change inner word
ci" Change inner quotes
cit Change inner tags (HTML)
cip Change inner paragraph
yip Yank inner paragraph
yap Yank paragraph (including newline)

Vim Multiple files

Buffers

- -
:e file Edit a file in a new buffer
:bn Go to the next buffer
:bp Go to the previous buffer
:bd Remove file from buffer list
:b 5 Open buffer #5
:b file Go to a buffer by file
:ls List all open buffers
:sp file Open and split window
:vs file Open and vertically split window
:hid Hide this buffer
:wn Write file and move to next
:tab ba Edit all buffers as tabs

Windows

- -
s Split window
v Split window vertically
w Switch windows
q Quit a window
T Break out into a new tab
x Swap current with next
- / + Decrease/Increase height
< / > Decrease/Increase width
= Equally high and wide
h / l Go to the left/right window
j / k Go to the up/down window

Tabs

Shortcut Description
:tabe [file] Edit file in a new tab
:tabf [file] Open if exists in new tab
:tabc Close current tab
:tabo Close other tabs
:tabs List all tabs
:tabr Go to first tab
:tabl Go to last tab
:tabm 0 Move to position 0
:tabn Go to next tab
:tabp Go to previous tab

Normal mode

Shortcut Description
gt Go to next tab
gT Go to previous tab
2gt Go to tab number 2

Vim Search and Replace

Search

- -
/foo Search forward
/foo\c Search forward (case insensitive)
?foo Search backward
/\v\d+ Search with regex
n Next matching search pattern
N Previous match
* Search for current word forward
# Search for current word backward

Replace LINE

`
:[range]s/{pattern}/{str}/[flags]

`


:s/old/new Replace first
:s/old/new/g Replace all
:s/\vold/new/g Replace all with regex
:s/old/new/gc replace all (Confirm)
:s/old/new/i Ignore case replace first
:2,6s/old/new/g Replace between lines 2-6

Replace FILE

`
:%s/{pattern}/{str}/[flags]

`


:%s/old/new Replace first
:%s/old/new/g Replace all
:%s/old/new/gc Replace all (Confirm)
:%s/old/new/gi Replace all (ignore case)
:%s/\vold/new/g Replace all with regex

Ranges

- -
% Entire file
’<,’> Current selection
5 Line 5
5,10 Lines 5 to 10
$ Last line
2,$ Lines 2 to Last
. Current line
,3 Next 3 lines
-3, Forward 3 lines

Global command

`
:[range]g/{pattern}/[command]

`


:g/foo/d Delete lines containing foo
:g!/foo/d Delete lines not containing foo
:g/^\s*$/d Delete all blank lines
:g/foo/t$ Copy lines containing foo to EOF
:g/foo/m$ Move lines containing foo to EOF
:g/^/m0 Reverse a file
:g/^/t. Duplicate every line

Inverse :g

`
:[range]v/{pattern}/[command]

`


:v/foo/d Delete lines not containing foo(also :g!/foo/d)

Flags

- -
g Replace all occurrences
i Ignore case
I Don't ignore case
c Confirm each substitution

Substitute expression (magic)

- -
& \0
\1...\9 Replace with the group 0-9
\u Uppercase next letter
\U Uppercase following characters
\l Lowercase next letter
\L Lowercase following characters
\e End of \u, \U, \l and \L
\E End of \u, \U, \l and \L

Examples

`
:s/a|b/xxx\0xxx/g # Modifies "a b" to "xxxaxxx xxxbxxx"
:s/test/\U& file/ # Modifies "test" to "TEST FILE"
:s/(test)/\U\1\e file/ # Modifies "test" to "TEST file"
:s/\v([abc])([efg])/\2\1/g # Modifies "af fa bg" to "fa fa gb"
:s/\v\w+/\u\0/g # Modifies "bla bla" to "Bla Bla"
:s/\v([ab])|([cd])/\1x/g # Modifies "a b c d" to "ax bx x x"
:%s/./\L&/ # Modifies "HTML" to "html"
:s/\v<(.)(\w
)/\u\1\L\2/g # Make every first letter of a word uppercase
:%s/^(.)\n\1/\1/ # Remove duplicate lines
:%s/<\/=(\w+)>/\U&/g # Convert HTML-Tags to uppercase
:g/^pattern/s/$/mytext # Find and append text to the end
:g/pattern/norm! @i # Run a macro on matching lines
/^(.
)(\r\?\n\1)+$ # View the duplicates lines
/\v^(.*)(\r?\n\1)+$ # View the duplicates lines (very magic)
:v/./,/./-j # Compress blank lines into a blank line
:g//,//d # Delete inclusively from to

`

Vimdiff

Usage

`

$ vimdiff file1 file2 [file3]
$ vim -d file1 file2 [file3]

`

Editing

`
:[range]diffget [bufspec]
:[range]diffput [bufspec]

`


Shortcut Description
do / :diffget Obtain (get) difference
dp / :diffput Put difference
:dif Re-scan differences
:diffo Switch off diff mode
:1,$+1diffget Get all differences
ZQ Quit without changes

See: Ranges

Folds

Shortcut Description
zo / zO Open
zc / zC Close
za / zA Toggle
zv Open folds for this line
zM Close all
zR Open all
zm Fold more (foldlevel += 1)
zr Fold less (foldlevel -= 1)
zx Update folds

Jumping

Shortcut Description
]c Next difference
[c Previous difference

Miscellaneous

Case

Shortcut Description
vU Uppercase character
vu Lowercase character
~ Toggle case character
viw U Uppercase word
viw u Lowercase word
viw ~ Toggle case word
VU / gUU Uppercase line
Vu / guu Lowercase line
V~ / g~~ Toggle case line
gggUG Uppercase all text
ggguG Lowercase all text
ggg~G Toggle case all text

Jumping

Shortcut Description
Go back to previous
Go forward
gf Go to file in cursor
ga Display hex, ascii value

Misc command-lines

- -
:h Help open help view
:edit! Reload current file
:2,8m0 Move lines 2-8 to 0
:noh Clear search highlights
:sort Sort lines
:ter Open a terminal window
:set paste Enable Insert Paste sub-mode
:set nopaste disable Insert Paste sub-mode
:cq Exiting with an error(aborting Git)

Navigating

Shortcut Description
% Nearest/matching {[()]}
[( [{
]) ]{
[m Previous method start
[M Previous method end

Counters

Shortcut Description
Increase number
Decrease number

Tags

Shortcut Description
:tag Classname Jump to first definition of Classname
Jump to definition
g] See all definitions
Go back to last tag
Back/forward
:tselect Classname Find definitions of Classname
:tjump Classname Find definitions of Classname (auto-select 1st)

Formatting

- -
:ce 8 Center lines between 8 columns
:ri 4 Right-align lines at 4 columns
:le Left-align lines

See :help formatting

Marks

Shortcut Description
`^ Last position of cursor in insert mode
`. Last change in current buffer
`" Last exited current buffer
`0 In last file edited
'' Back to line in current buffer where jumped from
Back to position in current buffer where jumped from
`[ To beginning of previously changed or yanked text
`] To end of previously changed or yanked text
`< To beginning of last visual selection
`> To end of last visual selection
ma Mark this cursor position as a
`a Jump to the cursor position a
'a Jump to the beginning of the line with position a
d'a Delete from current line to line of mark a
d`a Delete from current position to position of mark a
c'a Change text from current line to line of a
y`a Yank text from current position to position of a
:marks List all current marks
:delm a Delete mark a
:delm a-d Delete marks a, b, c, d
:delm abc Delete marks a, b, c

Calculator

Shortcut Description
= 7*7 Shows the result
= 10/2 Shows the result

Do this in INSERT mode

Shell

- -
:! Interpret Shell Command
:r! Read in output of shell
:r!date Insert date
:!!date Replace current line with date

Command line

Shortcut Description
Insert current word into the command line
" Paste from " register
Auto-completion of path in insert mode

Tricks

Remove duplicate lines

:sort | %!uniq -u

Enter fullscreen mode Exit fullscreen mode

To number the lines in the file

:%!cat -n

Enter fullscreen mode Exit fullscreen mode

Copy whole doc to clipboard

:%w !pbcopy            # Mac OS X
:%w !xclip -i -sel c   # GNU/Linux
:%w !xsel -i -b        # GNU/Linux

Enter fullscreen mode Exit fullscreen mode

Also see

Top comments (0)