Vim#
Table of Contents#
In the terminal, run vim --version to check which version of Vim is installed. Vim has historically been installed at /usr/bin/vim.
Try out vimtutor by running vimtutor! Check out Vim Adventures, too.
To open a file called example.txt in Vim, run vim example.txt. If file example.txt does not yet exist, it will be created when you save it.
There are two modes in Vim: command mode and insert mode. When you open Vim you start in command mode. To enter insert mode, press key i. To enter command mode, press Escape.
To start typing source code, first enter insert mode.
To save the file, enter command mode (press Escape); press key :; press key w for “write”; and press Enter. The file is saved.
To quit Vim, enter command mode (press Escape); press key :; press key q for “quit”; and press Enter: :q. If you have unsaved changes, you may want to save and quit (:wq) or just quit without saving them (:q!).
Resources#
Online
[ o ] StackExchange. “Insert tabs in INSERT mode when expandtab is set”.
YouTube#
[ y ]
03-09-2022. freeCodeCamp. “Vim Tutorial for Beginners”.
Texts#
[ w ] Robbins, Arnold & Elbert Hannah. (2021). Learning the vi & Vim Editors: Power and Agility Beyond Just Text Editing. 8e. O’Reilly.
Notes#
:%!xxd # run a binary file through xxd in vim
:%!xxd -r # convert the text back to binary
:%!base64 # convert a file loaded in vim to base64 encoding
:%!base64 -d # conver it back
ZZ # write changes and quit
:wq # write changes and quit
:w # write changes
:q! # do not write changes and quit
:q # quit
Ctrl-V Tab # insert tab
:set shell=/bin/bash
:shell
:e /path/to/file
Command Mode Shortcuts
iEnter insert mode to the left of the cursor.IEnter insert mode at the start of the current line.aEnter insert mode to the right of the cursor.AEnter insert mode at the end of the current line.ggMove the cursor to the first line.g50gMove the cursor 50 lines ahead.GMove the cursor to the last line.uor:undoUndo the last action.UUndo all recent changes made to the current line.Ctrl-ror:redoRedo.ddor:dDelete (and copy to the clipboard) the current line.d50dDelete (and copy to the clipboard) the next 50 lines.d50ggDelete (and copy to the clipboard) all lines between the current line and line 50.xDelete the current character.50xDelete the next 50 characters.yyor:yCopy the current line.y50wCopy the next 50 words.pPaste below the current line.PPaste above the current line.zzMove the current line to the middle of the screen.:50Move the cursor to line 50.nMove the cursor to the next search match.NMove the cursor to the previous search match.:nohTurn off highlighting until the next search.jMove down.kMove up.hMove left.lMove right.wMove to the beginnging of the next word.WeMove to the end of the next word.EbMove to the beginning of the previous word.B0Move to the beginning of the current line.$Move to the end of the current line.Ctrl-fPage down/forward.Ctrl-bPage up/backward.Ctrl-dHalf-page down/forward.Ctrl-uHalf-page up/backward.
Find and Replace
/fooSearch the file for all instances offoo.:%s/old/new/gReplace all instances ofoldwithnew.
Commands
retabChange the existing tab characters to match the current tab settings.set expandtabThe tab key inserts spaces instead of tab characters.set shiftwidth=2The size of an indent measured in spaces. If, for example, an indent is equal to two tab characters while tabstop=2, then an indent will be four spaces; if in addition expandtab is enabled, then an indent will be just two spaces.set tabstop=2The width of the tab character measured in spaces.
Config
~/.vimrc
set ai " autoindent
set et " expandtab
filetype indent on
"filetype indent plugin on
set nu " number ("display line numbers")
set sw=2 " shiftwidth
set ts=2 " tabstop
"syn on " syntax highlighting on
syn enable " syntax highlighting enabled