
The command line text editor vim is popular in the world of programming,
and for good reason. It can be found on nearly any Unix (and often Windows)
system, making it ideal for loading into remote machines and making quick edits
to files. It is also very powerful in the right hands. If you watch an
expert vim user code with vim, the cursor is bouncing all over
as they quickly write or edit their files, all without ever touching a mouseā¦
I am not one of those people. I picked up vim out of necessity while
working on remote hosts without access to a GUI text editor (Iām partial
to VS Code). But since I needed to use vim, I wanted to get better at it, so I
took a
vim Udemy course,
and Iāve been using what Iāve learned to get a little more efficient in vim.
If youāre a vim beginner, this post will teach you the commands I found
most useful while learning vim. And if youāre a casual vim user
like me, this post can serve as a cheat sheet reminder for all the vim
commands you and I will continue to forget.
The essentials
These are the set of commands that you need to enter a file, make changes, and get out (with or without saving).
-
Enter into a file for editing: Type:
vim some_file.txtorvi some_file.txtto enter a file withvim. -
Exit a file without saving: First press
ESCto enterNormal Mode. Then type:q!+ENTER.qhere means āquitā while!means āforceā, andvimwonāt exit without it if youāve made any changes. -
Save a file and exit: First press
ESCto enterNormal Mode. Then type:wq+ENTER.wmeans āwriteā here so you can also just use:w+ENTERwithoutqto save the file without exiting. -
Enter
Insert Mode: This is the mode where you can add text to the file. While inNormal Mode, pressi(for āinsertā). Recall that you can pressESCwhenever you want to exitInsert Modeto go back toNormal Mode.
Navigation
These commands will get you around the file with ease. Notice where I use
capital vs lowercase letters, as I might not always point it out.
Case matters in vim. For all of the following commands in this section,
unless otherwise specified, Iāll assume you are in Normal Mode
(recall you can get there by hitting ESC).
-
Move up, down, left, and right: There are two main ways to move in these directions. You can use the
up,down,left, andrightarrow keys in any mode. If you are inInsert Mode, these are the only keys for navigating. However, fromNormal Mode, you can navigate in these directions without leaving typing position:hisleft,jisup,kisdown, andlisright. Notice the left-most key of these 4 keys (h) isleft, the right-most key (l) isright, and the middle two are (j)upfollowed by (k)down. -
Go to the start of the file: Press
g+gto go to the start of the file. -
Go to the end of the file: Press capital
G(ieSHIFT-G) to go to the end of the file. -
Go to a specific line in the file: Type
:LIN_NUM+ENTERto go to that line number. Eg.:25+ENTERto go to line 25. -
Go to the start of the current line: Type
0(zero) to go to the start of the current line. You can also press^(carrot)(ieSHIFT-6) to go to the first non-blank character of the line. You might recall that^is the start character forregexexpressions. -
Go to the end of the current line: Type
$(dollar sign)(ieSHIFT-4) to go to the end of the current line. You might recall$is the end character forregexexpressions. -
Move forward one word: Type
wto move your cursor to the next word. I find this to be a quick way of moving through a line. -
Search for a word: type
/+ SEARCH +ENTERto search for a word. E.g./foo+ENTERwill search for all instances of āfooā in the text. Note if thehlsearchsetting is set, found searches will start highlighting as you type. To go to the next found search item, pressn, or to go to the previous search item, press capitalN. You can remove the previous searchās highlight by typing:noh(for no highlight), or if you canāt remember this (and I just looked:nohup so obviously I canāt), just search for something new that doesnāt exist. Eg./asdfasdfas+ENTER.
Entering Insert Mode with style
There are a host of ways to enter Insert Mode to start writing
new text. Here are the most useful ones. For all these
commands, I will assume you are already in Normal mode (ESC).
-
Enter
Insert Modein place: Pressito enterInsert Modeone character before the character your cursor is on. I already mentioned this one in section one. -
Enter
Insert Modeat line beginning: Press capitalIto enterInsert Modeat the beginning of the line containing your cursor. -
Enter
Insert Modewith append: Pressato enterInsert Modeone character after the character your cursor is on. -
Enter
Insert Modeat line end: Press capitalAto enterInsert Modeat the end of the line containing your cursor. -
Enter
Insert Modebelow your current line: Pressoto create a new line below the line containing your cursor and enterInsert Modethere. Note this will not break the line in half if your cursor is mid-line. -
Enter
Insert Modeabove your current line: Press capitalOto create a new line above the line containing your cursor and enterInsert Modethere.
Undo, Redo
Sometimes you make a mistake and need to un-make that mistake.
You could :q! to exit without saving, but letās see if we can find a
less drastic solution. These commands should be performed from
Normal Mode (ESC).
-
Undo: Press
uto undo. UppercaseUundoes all the latest changes on one line (which I donāt ever use). -
Redo: Press
Ctrl-rto redo.
Deleting things (and cut)
There are multiple ways to delete different-sized chunks of code in vim.
Letās take a look at some of the more common ones. d stands for delete in vim so
commands focus around this key. Note that vim always saves the last deleted
thing to the default register (more on registers later). So in a sense,
there is no delete, just cut with short-term memory. All these commands
should be performed from Normal Mode (ESC).
-
Delete/cut a letter: Press
d+lto delete a letter and save it to the defaultregister. -
Delete/cut a word: Press
d+wto delete a word and save it to the defaultregister. -
Delete/cut a line: Press
d+dto delete a line and save it to the defaultregister.
Also, note that while in Insert Mode you can use BACKSPACE and DEL, as usual,
to delete characters before or after your cursor respectively. This form
of deletion does not save to the default register.
Copy/paste
Weāve already seen how to cut (the same as deleting). Here we see
how to copy items to the register and also to paste items from the register.
Copying in vim is called yanking, so those commands use y.
All these commands should be performed from Normal Mode (ESC).
-
Copy/yank a letter: Press
y+lto copy a letter to the defaultregister. -
Copy/yank a word: Press
y+wto copy a word to the defaultregister. -
Copy/yank a line: Press
y+yto copy a line to the defaultregister. -
Paste after the cursor: Press
pto paste whatever is in the defaultregisterimmediately after the character the cursor is hovering over. -
Paste before the cursor: Press capital
Pto paste whatever is in the defaultregisterimmediately before the character the cursor is hovering over.
What is this register I keep mentioning? Briefly, a register can store
copied/cut text. A register can be ānamedā. For example, to copy to the b
named register you could type "b followed by your yank. And then to
use that yanked text, type "b + p. The default register is where
text is stored and pasted from if you donāt use a named register.
Only the latest copy/cut text is stored in a register. Subsequent
stores override the previous store. If registers interests you,
give them a quick google search for more details.
Also, note these commands are only for copy/paste from within vim. Copying and pasting
from the system clipboard is trickier. While it is possible with key
commands, usually Iāve had success just using a good old fashion mouse
right-click + click dropdown item for this purpose, so just do that.
Replace Mode
Replace Mode is like Insert Mode, except all typed characters overwrite
whatever the cursor is hovering. These commands should begin in Normal Mode
(ESC).
-
Replace a single character: Place cursor over the character to be replaced and press
r+ NEW CHARACTER (ier+f) to replace the hovered character with āfā. Vim will immediately re-enterNormal Modeafter replacing this character. -
Replace multiple characters: Place cursor over the character you want to start replacing at and press capital
R. Vim will enterReplace Modeat that character and all subsequent characters typed will replace the following characters on that line. For instance with the cursor over the ādā from ādogā, I could typeRcatto replace ādogā with ācatā. To exit replace mode you can hitESC.
Doing things in multiples
Vim has a cool sentence structure like syntax that you can use to perform
actions. We saw this earlier with commands like d + w for ādelete a wordā.
Adding a number at the start of these sentence-like commands will perform that
command that number of times. For instance to ādelete 3 wordsā I could type
3 + d + w and the hovered word plus the next two will be deleted.
To copy (yank) 2 lines, I can type 2 + y + y. And to undo the last 4
actions I could type 4 + u. Neat, eh?
Bonus: Vim settings
I think vim is much nicer when it is configured the way you like it. Hereās
a list of settings I like to use when running vim. Put these settings
(plus or minus the ones you like) into a file at ~/.vimrc. In the file,
" at the start of a line means it is a comment.
" use syntax highlighting
syntax on
" Show line number
set number
" Show the cursor position
set ruler
" Show incomplete commands
set showcmd
" Highlight searched words
set hlsearch
" Incremental search
set incsearch
" Search ignore case unless capitalized letters used in search
set ignorecase
set smartcase
" Don't line-break mid-word
set lbr
" Set auto-indent (auto indent when the previous line was indented for coding)
set autoindent
" Set smart indent (smartly indent when code indicates indent should occur)
set smartindent
" Replace tab with spaces
set expandtab
" Set tab spacing to 4
set tabstop=4
set shiftwidth=4
set softtabstop=4
Conclusions
These were the vim commands (and settings) I found most useful in breaking
past the bare minimum of what it takes to code in vim.
As I said earlier, I am by no means a vim expert. Iāve only recently been
using vim commands more complicated than the ones I listed in āvim essentialsā.
But now that Iāve learned some of these neat commands, I have to say,
vim is not so scary anymore and is even pretty pleasant to code in.
Vim is very powerful and there is a lot you can do with it. Iām sure vim
experts would disagree with some of my choices for a curated list of commands.
A couple of other vim topics I left out that you should look into if you want
to get āgoodā with vim are:
- Using the
vimmanual to look all these commands and ideas up yourself - Macros (save a series of
vimcommands to 1 or 2 keystrokes) vimreplace (replace words/phrases found withvimsearch)Visual Mode(make more complex edits in grid-like patterns)- Many others I canāt think of right now
If you are interested in a vim deep dive, I highly recommend the
Vim masterclass Udemy course
where you can learn these commands and many others and how to
āthink like a vim userā to really speed up your vim coding.
Comments (0)
You can style your comment using markdown. See this markdown cheat sheet for ideas. Comment as a guest with the below form, or to comment as yourself. Optionally, provide your email to be notified when others comment on this blog post.