- Vim (Vi IMproved)
- created by Bram Moolenaar
- initial release 1991 (Vi was released 1976)
- still in active development
- 2017 Stackoverflow survey
- 4th most popular for Web developers
- 5th most popular for Desktop developers
- 1st most popular for Sysadmin / DevOps
- 3rd most popular for Data scientists
Vim has a high learning curve but well worth it
(WARNING: emacs is rabbit hole)
https://en.wikipedia.org/wiki/Editor_war
An interesting piece of programmer culture and history. Arguably foremost among the programmer holy wars.
- Speed
- Efficiency
- Comfort
- Simple
- Ubiquitious
Modal editing has clear advantages to an always-insert editing model.
- Touch typing is recommended learning
- The mouse is anathema
- Lots of vim learning resources; start with
vimtutor - Start vanilla then build your config slowly
- Keep a cheatsheet in the beginning
- Seek
:help :q
- Editing text should be structured
- Learn the editing language
- Fluency allows for efficient and surgical text editing
3dd - delete 3 lines daw - delete 'around' word c3f. - change the text up to and including the 3rd instance of a period
Normal mode
- default state
- all your keys are commands
- Press
<ESC>to go back to this mode
Insert mode
- typical editor mode
- accessed via normal mode commands
Visual mode
- Selection/Highlight
- Two types
- Visual block (use this for vertical changes)
- Visual line
Keep your fingers on the home row
^
k Hint: The h key is at the left and moves left.
< h l > The l key is at the right and moves right.
j The j key looks like a down arrow.
v
NOTE:
- there is a difference between display line and line
j* As an aside: ADM-3A
https://en.wikipedia.org/wiki/ADM-3A
When vi was being created by Bill Joy, the dumb terminal keyboard used was the one above. Note the positioning of keys.
0 - beginning of line
^ - first non-blank character
$ - end of line
B - previous WORD
b - previous word
e - end of word
E - end of WORD
ge - previous end of word
gE - previous end of WORD
w - beginning of next word
W - beginning of next WORD
NOTES:
- certain motions (e.g. word motions) can be prefixed with a number to repeat the motion
- difference of semantics of word, WORD, page.
gg - beginning of file
G - end of file
CTRL-b - up 1 page
CTRL-u - up 1/2 page
CTRL-d - down 1/2 page
CTRL-f - down 1 page
f{char} - to the next occurence of character on the right
F{char} - to the next occurence of character on the left
t{char} - same as f but stops short one character
T{char} - same as F but stops short one character
; - repeat forward (F,f) motion
, - repeat backward (T,t) motion
/ - search forward
? - search backward
* - search forward word under cursor
# - search backward word under cursor
PROTIP: end search query with /e to go to end of the search
j* More Motion
:help motion
:help pattern
Highlights:
gd - jump to definition
% - go to match
gi - go to last insert mode location
'. `. '< `< and others - special marks
CTRL-O - go to last jump
:help insert
Commands for putting you in insert mode
o - begin a new line under the cursor
O - begin a new line above the cursor
a - insert text after the cursorasdf
A - insert text at end of line
i - insert text before the cursor
I - insert text at the beginning of the line (first non-blank)
:help change
d,D,x,X - delete
y,Y - yank
J - join lines
gU, gu - change case
gq - formatting
:! - pass through external program
<, > - shift left, right
The following leaves you in insert mode after deleting text
c,C - change
s,S - substitute
r,R - replace
. - redo your last change
:help text-objects
Commands used in conjunction with visual mode or an operator
a - around
i - inner
b, B, (, ) - block
w - word
W - word
s - sentence
p - paragraph
NOTE: it is possible to create your own text objects
:help windows
A buffer is the in-memory text of a file. A window is a viewport on a buffer. A tab page is a collection of windows.
Window commands are prefixed with CTRL-W
s - split current window in two
v - split current window in two (vertically)
n - create new window
c - close window (:q works just as well)
o - only window
Window commands are prefixed with CTRL-W
h,j,k,l - move cursor to other windows
H,J,K,L - move windows around
r - rotate windows
T - move window to new tab page
= - equalize window sizes
- - decrease window height
+ - increase window height
< - decrease window width
> - increase window width
_ - max height
| - max width
PROTIP: set scrollbind to synchronize scrolling (useful for multi page viewing on large monitors)
:tabnew - open new tab
:tabclose - close tab
gt, gT - switch between tabs
PROTIP: remap keys for easier access to tab commands
:ls, :buffers - show all buffers
:bn, :bN - switch between buffers
:b N - switch to targeted buffer
PROTIP: remap keys for easier buffer switching (e.g. :ls<CR>:b)
:help writing
:w - save buffer to current file
:help swap
Vim stores the things you changed in a swap file. Using the original file you started from plus the swap file you can mostly recover your work.
Set the directory option to configure where swap files are created.
:help undo
u - undo
CTRL-r - redo
Vim uses a branching model to keep track of file changes.
All changes can be accessed through the undo list.
Earlier states of a file can be accessed using earlier and later.
Set the undofile option to enable undo persistence.
Set the undodir option to create the undo files in a central location.
:help registers
There are ten types of registers:
- The unnamed register “”
- 10 numbered registers “0 to “9
- The small delete register “-
- 26 named registers “a to “z or “A to “Z
- three read-only registers “:, “., “%
- alternate buffer register “#
- the expression register “=
- The selection and drop registers “*, “+ and “~
- The black hole register “_
- Last search pattern register “/
You can insert register contents with CTRL-r in insert mode or prefix a paste command with "{register}.
PROTIP: recorded macros are stored in registers
:help mark-motions
` - The cursor is positioned at the specified location
' - The cursor is positioned on the first non-blank character in the line of the specified location
m{a-zA-Z} - set a mark at cursor position
:help mapping
{n,i,v,o}map - recursive mapping
{n,i,v,o}noremap - non-recursive mapping
inoremap jj <Esc>
:help mapleader
:help maplocalleader
Namespaced mappings allowing for greater customization without overstepping existing mappings.
- Backgrounding vim
- Standard input
- Edit command line
- Edit history
- Relative line numbers
- …
file:~/.vimrc