Skip to main content

Vim Basics: Navigating and Editing

·777 words·4 mins
Linux Learning Lab
Author
Linux Learning Lab
Writing about code, tools, and workflows.
vim-essentials - This article is part of a series.
Part 1: This Article

Why Learn Vim?
#

Vim is installed on virtually every Linux server, container, and Unix-like system. When you SSH into a production box, edit a crontab, or resolve a git merge conflict — Vim (or its predecessor vi) is there.

You don’t need to make it your primary editor to benefit from it. Knowing the basics means you’re never stuck on a remote machine without a way to edit files.

Opening and Closing Files
#

# Open a file
vim file.txt

# Open at a specific line
vim +42 file.txt

# Open multiple files
vim file1.txt file2.txt

Quitting Vim
#

The commands everyone searches for first:

CommandAction
:qQuit (fails if unsaved changes)
:q!Quit without saving
:wSave (write)
:wqSave and quit
ZZSave and quit (shortcut)
ZQQuit without saving (shortcut)

Understanding Modes
#

Vim is a modal editor — the same keys do different things depending on which mode you’re in. This is what makes it confusing at first and powerful once it clicks.

ModePurposeHow to enter
NormalNavigate and manipulate textEsc (default mode)
InsertType texti, a, o, etc.
VisualSelect textv, V, Ctrl-v
CommandRun commands:

The key rule: Press Esc to go back to Normal mode. When in doubt, hit Esc.

Normal Mode Movement
#

In Normal mode, you navigate without touching the mouse. Start with these:

Character and line movement
#

KeyMovement
hLeft
jDown
kUp
lRight

These replace arrow keys. Your fingers stay on the home row.

Word movement
#

KeyMovement
wForward to start of next word
bBack to start of current/previous word
eForward to end of current/next word

Line movement
#

KeyMovement
0Beginning of line
^First non-blank character
$End of line

File movement
#

KeyMovement
ggTop of file
GBottom of file
42GGo to line 42
Ctrl-dScroll down half a screen
Ctrl-uScroll up half a screen

Entering Insert Mode
#

From Normal mode, these keys put you into Insert mode at different positions:

KeyInserts at
iBefore the cursor
aAfter the cursor
IBeginning of the line
AEnd of the line
oNew line below
ONew line above

Type your text, then press Esc to return to Normal mode.

Basic Editing in Normal Mode
#

You don’t need to enter Insert mode for every edit. These commands work directly in Normal mode:

Deleting
#

CommandAction
xDelete character under cursor
ddDelete entire line
dwDelete from cursor to start of next word
d$ or DDelete from cursor to end of line

Copying (yanking) and pasting
#

CommandAction
yyYank (copy) entire line
ywYank from cursor to start of next word
pPaste after cursor
PPaste before cursor

Undo and redo
#

CommandAction
uUndo last change
Ctrl-rRedo

Other essentials
#

CommandAction
rReplace single character under cursor
JJoin current line with the line below
~Toggle case of character
.Repeat last change

Visual Mode (Selecting Text)
#

Visual mode lets you select text and then act on the selection:

KeySelection type
vCharacter-wise selection
VLine-wise selection
Ctrl-vBlock (column) selection

Once text is selected, you can:

  • d — delete the selection
  • y — yank the selection
  • > — indent
  • < — unindent
# Example workflow: delete 3 lines
V       (enter visual line mode)
jj      (select two more lines)
d       (delete selection)

Putting It Together
#

Here are some common tasks using what you’ve learned:

Delete a word and type a new one
#

dw      (delete word)
i       (enter insert mode)
         ...type replacement...
Esc     (back to normal mode)

Copy a line and paste it below
#

yy      (yank line)
p       (paste below)

Delete everything from the cursor to the end of the file
#

dG

Move to line 10 and delete it
#

10G     (go to line 10)
dd      (delete line)

Open a file, add a line at the end, save and quit
#

vim file.txt
G       (go to end of file)
o       (new line below, enter insert mode)
         ...type your text...
Esc     (back to normal mode)
:wq     (save and quit)

Best Practices
#

  • Stay in Normal mode by default — only enter Insert mode when you’re actively typing new text
  • Use u liberally — Vim has unlimited undo, so experiment without fear
  • Practice hjkl instead of arrow keys — it’s awkward for a day, then faster forever
  • Learn the dot command (.) early — it repeats your last edit and saves enormous time
  • Run vimtutor from your terminal for an interactive 30-minute tutorial built into Vim
vim-essentials - This article is part of a series.
Part 1: This Article