Vim, the editor

Other text editors

Your typical editor works like this…

  • When you type almost any key, the corresponding character (letter, number, symbol, or whitespace) appears at the cursor
  • Certain special keys do special operations (control, alt, command, super)
  • For a GUI app, you can/must click menus for other functionality

Vim is different

Vim has modes

Normal
for almost everything except typing! Insert
for typing Visual
for highlighting a region to operate on
(and others you don’t need to know about)

The keyboard is your friend

For the impatient

  • Start vim in terminal: $ vim test.txt
  • Press i to enter insert mode
  • Type to your heart’s content
  • When you’re done, hit Esc to leave insert mode
  • Then type :w to save your document
  • Then type :q to quit
    • if you have unsaved changes, vim will complain
    • to save, see previous, or do :wq in one step
    • to discard changes and quit, do :q!

Normal mode

Now that you’ve seen that you can actually type in vim, let’s learn about the magic of Normal mode!

Moving around (“motions”)

Stepping

  • by line: up k, down j
  • by character: forward l, backward h
  • by word: forward w, backward b
  • by sentence: forward ), backward (
  • by paragraph: forward }, backward {

Moving around (“motions”)

Jumping

  • to start/end of line: 0/$
  • to first/last line of file: gg/G
  • to a character on current line: f/F{char}
  • to identical word: */#
  • to line number: :{#} or {#}G
  • to top, middle, or bottom of screen: {HML}

Insert

  • starting right where you are: i
  • starting at the next character – a
  • starting at the end of the current line – A
  • starting at the beginning of the line – I
  • starting on the next line – o
  • starting on the previous line – O
  • enter ‘replace mode’ right where you are – R
  • replace current character with one other character – r{char}
  • delete current character, then enter insert mode – s
  • delete current line, then enter insert mode – S

Delete (really, this is “cut”)

  • delete character under cursor: x
  • delete character before cursor: X
  • delete to end of line: D
  • delete entire line: dd
  • delete # lines: d#d
  • delete over some motion: d{motion}
  • use backspace in insert mode (like every other editor)

Copy and paste

  • copy (“yank”) – y{motion}
  • copy (“yank”) –
  • paste after cursor – p
  • paste before cursor – P

Search and replace

  • we already saw some of this with jumping
  • search for arbitrary expression – /{expression}
  • search and replace
    • :s/foo/bar/
    • :s/foo/bar/g
    • :%s/foo/bar/g

Do-overs!

  • undo change: u
  • repeat last change: .
  • redo last undone change: Ctrl-r

Extras

  • toggle case – ~

Saving and quitting

  • save and quit: ZZ (or :wq)
  • save and keep editing: :w
  • quit without saving: :q
  • really quit without saving: :q!

Tabs

  • open another file in new tab: :tabe <file>
  • change tabs: forward gt, backward gT

Basic editing

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Operators and repetition

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Yank & paste

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Searching

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Marks & macros

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Various motions

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Various commands

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Places to learn



Bren School logo

The original parts of this work are licensed under a Creative Commons Attribution 4.0 International License.

This website was made with quarto by Posit.