12 January 2020

Cheat Sheet: The Vim Linux Command Line Editor Cheat Sheet


Screenshot of lines of code

Powerful, efficient, and highly configurable—that’s Vim in a nutshell for you. This cross-platform command line text editor has been around for ages and has a solid online community you can look to for help. The best part is that Vim is free and open source.

If you have only dabbled in Vim until now, the next step is getting comfortable with Vim commands i.e. Vim’s keyboard shortcuts. (This is essential given that Vim is a keyboard-centric text editor.)

We’ve even compiled the best Vim shortcuts into a cheat sheet for you! Scroll down to discover it. You can also download a PDF version of this Vim cheat sheet and save it to your desktop for quick access.

Keep in mind that Vim (Vi Improved) is a modified clone of the Unix-based text editor Vi, which ships with several Linux distros. So you’ll find that several Vim commands also function as Vi commands.

FREE DOWNLOAD: This cheat sheet is available as a downloadable PDF from our distribution partner, TradePub. You will have to complete a short form to access it for the first time only. Download The Vim Cheat Sheet.

The Vim Commands Cheat Sheet

Shortcut Action
Cursor Movement
h Move cursor left
j Move cursor down
k Move cursor up
l Move cursor right
w OR ¹W Move cursor to the start of the next word
b OR ¹B Move cursor to the start of the previous word
e OR ¹E Move forward to the end of a word
0 Move to the start of the line
^ Move to the first non-blank character of the line
$ Move to the end of the line
G Go to the last line of the document
nG OR :n Go to line number n
' To the position before the latest jump, / where the last "m'" / "m`" command was given.
fx Move to next occurrence of character x
tx Move to one character before the character x
} Move to next paragraph
{ Move to previous paragraph
H Move to home (top) of screen
L Move to last line of screen
M Move to middle of screen
3, f, x Move to 3rd instance of character x forward from cursor on current line.
3, F, x Move to 3rd instance of character x back from cursor on current line.
Editing a File
r Replace a single character
dd Cut (delete) a line
yy Yank (copy) a line
2yy Yank (copy) two lines
J Join line below to the current one
cc OR S Change (replace) entire line
cw Change (replace) to the end of the word
c$ Change (replace) to the end of the line
s Delete character and substitute text
xp Transpose two letters (delete and paste)
u Undo
Ctrl r Redo
. Repeat last command
Search and Replace
* Search for word under cursor
/pattern Search for pattern
?pattern Search backward for pattern
n Repeat search in same direction
N Repeat search in opposite direction
:%s/old/new/g Replace all instances of old with new
:%s/old/new/gc Replace all instances of old with new, with confirmations
Screen Movement
Ctrl u Move screen up by half page
Ctrl b Move screen up by one page
Ctrl d Move screen down by half page
Ctrl f Move screen down by one page
zz Center screen on cursor
zt Align top of screen with cursor
zb Align bottom of screen with cursor
Insert Mode (Insert Text)
i Enter insert mode
I Insert at the beginning of the line
a Insert (append) after the cursor
A Insert (append) at the end of the line
o Append (open) a new line below the current line
O Append (open) a new line above the current line
ea Insert (append) at the end of the word
Esc Exit insert mode
Change Case
~ Toggle case (Case => cASE)
gU Uppercase
gu Lowercase
gUU Uppercase current line (also gUgU)
guu Lowercase current line (also gugu)
Visual Mode (Mark Text)
v Start visual mode, mark lines, then do a command (like y-yank)
V Start linewise visual mode
vo Move to other end of marked area
Ctrl v Start visual block mode
vO Move to other corner of block
vaw Mark a word
vab Mark a block with ()
vaB Mark a block with {}
vib Mark inner block with ()
viB Mark inner block with {}
Esc Exit visual mode
Visual Commands
> Shift text right
< Shift text left
#ERROR! Auto-indent current line
<< Shift current line left by shiftwidth
>> Shift current line right by shiftwidth
y Yank (copy) marked text
d Delete marked text
~ Switch case
Folding Commands
zf#j Create a fold from the cursor down # lines
zf/ String creates a fold from the cursor to string
zj Move the cursor to the next fold
zk Move the cursor to the previous fold
za Toggle a fold under cursor
zo Open a fold at the cursor
zO Open all folds at the cursor
zc Close a fold under cursor
zm Increase the foldlevel by one
zM Close all open folds
zr Decrease the foldlevel by one
zR Decrease the foldlevel to zero—all folds will be open
zd Delete the fold at the cursor
zE Delete all folds
[z Move to start of open fold
]z Move to end of open fold
Tabs
#gt Move to tab number #
Ctrl w, t Move the current split window into its own tab
:tabmove # Move current tab to the #th position (indexed from 0)
:tabnew filename/ :tabn filename Open a file in a new tab
:tabclose / :tabc Close the current tab and all its windows
:tabonly / :tabo Close all tabs except the current one
gt / :tabnext / :tabn Move to the next tab
gT / :tabprev / :tabp Move to the previous tab
Working With Multiple Files
:e filename Edit a file in a new buffer
:ene Open a blank file for editing
:bnext / :bn Go to the next buffer
:bprev / :bp Go to the previous buffer
:bd Delete a buffer (close a file)
:sp filename Open a file in a new buffer and split window
:vs filename Open a file in a new buffer and vertically split window
Ctrl w, s Split window
Ctrl w, w Switch windows
Ctrl w, q Quit a window
Ctrl w, v Split window vertically
Ctrl w, h Move cursor to window left
Ctrl w, l Move cursor to window right
Ctrl w, k Move cursor to window above
Ctrl w, j Move cursor to window below
Ctrl w, r Rotate windows clockwise
Ctrl w, T Move current window to a new tab
:on Close all windows except current window
Ctrl w, | Maximize width of active window
Ctrl w, 1, | Minimize width of active window
Ctrl w, _ Maximize height of active window
Ctrl w, 1, _ Minimize height of active window
Ctrl w, = Equalize the size of windows
Exiting a File
:w Write (save) the file, but don't exit
:wq Write (save) and quit
:x OR ZZ Write (save) current file if modified and quit
:q Quit (fails if there are unsaved changes)
:q! Quit and discard unsaved changes
:qa Quit all buffers and windows
ZQ Quit without checking for changes
¹Word can contain punctuation.

Vim Improved

Vim is no doubt a great text editor, but it could do with a few of the useful features that most modern text editors are equipped with. The good news is that you can add top features from other text editors to Vim!

Image Credit: Markus Spiske on Unsplash

Read the full article: Cheat Sheet: The Vim Linux Command Line Editor Cheat Sheet


Read Full Article

No comments:

Post a Comment