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#

  • [ h ][ g ] Vim Adventures

  • [ h ][ g ] vim-plug

  • [ h ][ g ] YouCompleteMe

  • [ h ][ g ] deoplete.nvim

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

  • i Enter insert mode to the left of the cursor.

  • I Enter insert mode at the start of the current line.

  • a Enter insert mode to the right of the cursor.

  • A Enter insert mode at the end of the current line.

  • gg Move the cursor to the first line.

  • g50g Move the cursor 50 lines ahead.

  • G Move the cursor to the last line.

  • u or :undo Undo the last action.

  • U Undo all recent changes made to the current line.

  • Ctrl-r or :redo Redo.

  • dd or :d Delete (and copy to the clipboard) the current line.

  • d50d Delete (and copy to the clipboard) the next 50 lines.

  • d50gg Delete (and copy to the clipboard) all lines between the current line and line 50.

  • x Delete the current character.

  • 50x Delete the next 50 characters.

  • yy or :y Copy the current line.

  • y50w Copy the next 50 words.

  • p Paste below the current line.

  • P Paste above the current line.

  • zz Move the current line to the middle of the screen.

  • :50 Move the cursor to line 50.

  • n Move the cursor to the next search match.

  • N Move the cursor to the previous search match.

  • :noh Turn off highlighting until the next search.

  • j Move down.

  • k Move up.

  • h Move left.

  • l Move right.

  • w Move to the beginnging of the next word.

  • W

  • e Move to the end of the next word.

  • E

  • b Move to the beginning of the previous word.

  • B

  • 0 Move to the beginning of the current line.

  • $ Move to the end of the current line.

  • Ctrl-f Page down/forward.

  • Ctrl-b Page up/backward.

  • Ctrl-d Half-page down/forward.

  • Ctrl-u Half-page up/backward.

Find and Replace

  • /foo Search the file for all instances of foo.

  • :%s/old/new/g Replace all instances of old with new.

Commands

  • retab Change the existing tab characters to match the current tab settings.

  • set expandtab The tab key inserts spaces instead of tab characters.

  • set shiftwidth=2 The 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=2 The 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