VIM Cheat Sheet

mrfdn author
Rafi

Panduan singkat tentang shortcut / keybind VIM / Nvim yang wajib diketahaui


Daftar Isi

Apa itu VIM?

Vim merupakan text editor yang yang ada di hampir semua linux. Hampir semua pekerjaan seputar text bisa dilakukan melalui VIM.

Jika anda baru masuk di dunia linux dan programming, belajar VIM akan sangat membantu kamu untuk bisa mengerjakan pekerjaan lebih cepat di masa yang akan datang.

Saat menggunakan VIM, anda mungkin tidak akan menyentuh mouse lagi, karena sebagian besar perintah dan navigasi bisa dilakukan hanya melalui keyboard saja.

VIM Modes

VIM memiliki 3 mode yaitu:

  • Normal Mode
  • Insert Mode
  • Command Mode

Normal Mode

Pada normal mode, kita bisa memindahkan cursor di dalam file, menghapus text, dan melakukan sejumlah macam perintah.

Untuk bisa masuk ke normal mode, cukup menekan Esc pada keyboard.

Insert Mode

Pada insert mode, anda bisa melakukan input text atau mengetikkan isi dokumen yang ingin dibuat.

Anda bisa masuk ke insert mode dengan menekan i pada keyboard

Command Mode

Pada command mode anda bisa melakukan sejumlah perintah terhadap isi file / dokumen yang sedang dibuka. Seperti search and replace di vim.

Untuk masuk ke command mode, tekan Esc lalu : pada keyboard.

VIM Commands

Global

CommandDescription
:h[elp] keywordOpen help for keyword.
:sav[eas] fileSave the file as file.
:clo[se]Close the current file.
:ter[minal]Open a terminal.
KOpen help for word under cursor.

Cursor movement

CommandDescription
hMove left.
jMove down.
kMove up.
lMove right.
gjMove down a screen.
gkMove up a screen.
HMove to top of screen.
MMove to middle of screen.
LMove to bottom of screen.
wMove forward one word.
WMove forward one WORD.
eMove to end of word.
EMove to end of WORD.
bMove back one word.
BMove back one WORD.
geMove to end of previous word.
gEMove to end of previous WORD.
%Move to matching character (default supported pairs: ‘()’, ‘{}’, ‘[]’).
0Move to the start of the line.
^Move to the first non-blank character of the line.
$Move to the end of the line.
g_Move to the last non-blank character of the line.
ggMove to the first line of the file.
GMove to the last line of the file.
5GMove to line 5.
gdMove to the definition of the word under the cursor.
gDMove to the definition of the WORD under the cursor.
fxMove to the next occurrence of character x.
FxMove to the previous occurrence of character x.
txMove to the character before the next occurrence of character x.
TxMove to the character after the previous occurrence of character x.
;Repeat the previous f, t, F, or T movement.
,Repeat the previous f, t, F, or T movement, but in the opposite direction.
}Move to the next paragraph (or function/block, when editing code).
{Move to the previous paragraph (or function/block, when editing code).
zzCenter cursor on screen.
Ctrl + eMove screen down one line (without moving cursor).
Ctrl + yMove screen up one line (without moving cursor).
Ctrl + bMove back one full screen.
Ctrl + fMove forward one full screen.
Ctrl + dMove forward half a screen.
Ctrl + uMove back half a screen.

Insert mode

CommandDescription
iInsert before the cursor.
IInsert at the beginning of the line.
aInsert (append) after the cursor.
AInsert (append) at the end of the line.
oAppend (open) a new line below the current line.
OAppend (open) a new line above the current line.
eaInsert (append) at the end of the word.
Ctrl + hDelete the character before the cursor.
Ctrl + wDelete the word before the cursor.
Ctrl + jbegin a new line.
Ctrl + tShift the current line one tab to the right.
Ctrl + dShift the current line one tab to the left.
Ctrl + nInsert the next completion of the word under the cursor.
Ctrl + pInsert the previous completion of the word under the cursor.
Ctrl + rxInsert the register x contents.
Ctrl + oxTemporarily enter normal mode to issue a single command.
ESCExit insert mode.

Editing

CommandDescription
rReplace a single character.
RReplace multiple characters.
JJoin line below to the current one with one space in between.
gJJoin line below to the current one without space in between.
gwipReformat paragraph.
g~Switch case of character under cursor.
guMake character under cursor lowercase.
gUMake character under cursor uppercase.
ccChange (replace) an entire line.
c$Change (replace) to the end of the line.
CChange (replace) to the end of the line.
ciwChange (replace) the entire word.
cwChange (replace) to the end of the word.
ceChange (replace) to the end of the word.
sDelete character under cursor and substitute text.
SDelete line and substitute text (same as cc).
xpTranspose two letters (delete and paste).
uUndo.
UUndo all changes on line.
Ctrl + rRedo.
.Repeat last command.

Marking text (visual mode)

CommandDescription
vStart visual mode, mark lines, then do a command (like y-yank).
VStart linewise visual mode.
oMove to other end of marked area.
OMove to other corner of block.
Ctrl + vStart visual block mode.
awMark a word.
abA block with ()
aBA block with {}
atA block with <>
ibInner block with ()
iBInner block with {}
itInner block with <>
EscExit visual mode.

Visual commands

CommandDescription
>Shift text right.
<Shift text left.
yYank (copy) marked text.
dDelete marked text.
~Switch case.
uchange marked text to lowercase.
UChange marked text to uppercase.

Registers

CommandDescription
:reg[isters]List all registers.
"xyYank text into register x.
"xpPaste text from register x.
"xPPaste before cursor.
"+yYank into system clipboard.

Marks and positions

CommandDescription
:marksList all marks.
maSet current position for mark a.
``a`Jump to mark a.
y'aYank to mark a.
``0`Jump to start of line.
``"`Jump to last edit position.
``.`Jump to last edit position.
``Jump to last edit position.
:ju[mps]List all jump positions.
Ctrl + oJump to previous position.
Ctrl + iJump to next position.
:changesList all changes.
g,Jump to previous change.
g;Jump to next change.
Ctrl + jJump to next change.

Macros

CommandDescription
qaStart recording macro a.
qStop recording macro.
@aExecute macro a.
@@Execute last macro.

Cut and paste

CommandDescription
yyYank (copy) a line.
2yyYank (copy) 2 lines.
ywYank (copy) the characters of the word from the cursor position to the start of the next word.
y$Yank (copy) to end of line.
yiwYank (copy) inner word.
yawYank (copy) a word including the surrounding white space.
pPut (paste) the clipboard after cursor.
PPut (paste) before cursor.
gpPut (paste) after cursor.
gPPut (paste) before cursor.
ddDelete (cut) a line.
2ddDelete (cut) 2 lines.
dwDelete (cut) the characters of the word from the cursor position to the start of the next word.
DDelete (cut) to the end of the line.
d$Delete (cut) to the end of the line.
diwDelete (cut) inner word.
dawDelete (cut) a word including the surrounding white space.
xDelete (cut) character.

Indent text

CommandDescription
>>Shift text right.
<<Shift text left.
>%Shift selected lines right.
<%Shift selected lines left.
>ibShift inner block right.
<ibShift inner block left.
>atShift block right.
<atShift block left.
3==Auto indent 3 lines.
=%Auto indent selected lines.
=iBAuto indent inner block.
gg=GAuto indent whole file.
]pPaste and indent.

Exiting

CommandDescription
:wWrite (save) the file, but don’t exit.
:w !sudo tee %Write out the current file using sudo.
:wqWrite (save) and quit.
:xWrite (save) and quit.
ZZWrite (save) and quit.
:qQuit (fails if there are unsaved changes).
:q!Quit and throw away unsaved changes.
ZQQuit and throw away unsaved changes.
:wqaWrite (save) and quit on all tabs.
:qaQuit on all tabs.

Search and replace

CommandDescription
/patternSearch for pattern.
?patternSearch backward for pattern.
\vpatternSearch for lines not containing pattern.
nRepeat search in same direction.
NRepeat search in opposite direction.
:%s/old/new/gReplace all old with new throughout file.
:%s/old/new/gcReplace all old with new throughout file with confirmations.
:s/old/new/gReplace all old with new in the current line.
:s/old/new/gcReplace all old with new in the current line with confirmations.
:noh[lsearch]Remove highlighting of search matches.

Search in multiple files

CommandDescription
:vimgrep /pattern/ {file}Search for pattern in multiple files.
:cn[ext]Jump to the next match.
:cp[revious]Jump to the previous match.
:cope[n]Open a window containing the list.
:ccl[ose]Close the window.

Tabs

CommandDescription
:tabeOpen a new tab.
:tabnew {page.words.file}Open a new tab.
gtMove to the next tab.
:tabn[ext]Move to the next tab.
gTMove to the previous tab.
:tabp[revious]Move to the previous tab.
#gtMove to tab #.
:tabm[ove] {number}Move the current tab to the {number}th position (indexed from 0).
:tabc[lose]Close the current tab.
:tabo[nly]Close all tabs except for the current one.
:tabr[ewind]Move to the first tab.
:tabfir[st]Move to the first tab.
:tabl[ast]Move to the last tab.
:tabsList all tabs.
:tabdoRun a command for each open tab.
:tabdo {cmd}Run {cmd} for each open tab.

Working with multiple files

CommandDescription
:e[dit] {file}Edit a file in a new buffer.
:bn[ext]Go to the next buffer.
:bp[revious]Go to the previous buffer.
:bd[elete]Delete a buffer.
:b[uffer]{number}Go to buffer {number}.
:b[uffer] fileGo to buffer {file}.
:lsList all open buffers.
:buffersList all open buffers.
:sp[lit] {file}Open a file in a new buffer and split window.
:vsp[lit] {file}Open a file in a new buffer and vertically split window.
:vert[ical] ba[ll]Open a file in a new buffer and vertically split window.
:tab ba[ll]Open a file in a new buffer and split window.
:tabe[dit] {file}Open a file in a new buffer and split window.
:tabnew {file}Open a file in a new buffer and split window.
:tab sballOpen a file in a new buffer and split window.
Ctrl + wsSplit window.
Ctrl + wwMove to the next window.
Ctrl + wqClose the current window.
Ctrl + wvSplit window vertically.
Ctrl + whMove to the window on the left.
Ctrl + wlMove to the window on the right.
Ctrl + wjMove to the window below.
Ctrl + wkMove to the window above.
Ctrl + w=Make all windows the same height.
Ctrl + wHMove the current window to the far left.
Ctrl + wLMove the current window to the far right.
Ctrl + wJMove the current window to the bottom.
Ctrl + wKMove the current window to the top.

Akhir kata

Ternyata cukup banyak perintah yang bisa dilakukan di VIM. Ini hanya sebagian daftar saja, di antara banyak sekali hal yang bisa dilakukan di VIM.

Anda bisa melihat daftarnya melalui :help di VIM. Tapi daftar ini sudah cukup lengkap untuk sekedar melakukan pekerjaan dengan lebih cepat menggunakan VIM.

Semoga bermanfaat.

Latihan VIM : Menghapus Karakter \n\n\n Pada Setiap Baris
mrfdn author

Rafi

  • 15 year+ of Linux user.
  • 5 years+ blogger and web developer.

Jika artikel yang dibuatnya ternyata bermanfaat, support dengan cara

    Share:

Baca juga


comments powered by Disqus