4 May, 2021

VI Keyboard Commands

Copy and Paste from Second File

  1. Open the second file while in command mode with :e other-file.md
  2. Navigate to the start of to-be-copied text
  3. Type v to start visual mode
  4. Navigate to the end and type y copy the text
  5. Type :bp to go back to the first document
  6. Navigate to the position where the text is to be inserted
  7. Type p to paste the text after the cursor positon

Indent multiple Lines

  1. Start visual line mode with V
  2. Select lines by movement
  3. Indent with >
  4. Add more indent with .
  5. Undo last indent with u

Rename local Variables

  1. Go to the variable.
  2. gd to go to the declaration
  3. cgn and type the new name
  4. ESC to go in command mode
  5. Repeat renaming next usage with .

Commands

Here are some list of vi commands.

Movement

Keys
h lGo left, right
j kGo down, up
w WGo to start of word (spaces separate words)
e EGo to end of word (no punctuation)
b BGo backward by word (no punctuation)
0 $Go to the start, end of the current line
^Go to the first non-blank character of the current line
*Go start of next same word
- +Go start of current, next line
H M LGo top, middle, bottom of the screen
( )Go by sentence
{ }Go by paragraph
[[ ]]Go by section
gg GGo to top, bottom of file

Scrolling

Keys
zEnterScroll until cursor is at the top of screen
z.Scroll until cursor is in the middle of screen
z-Scroll until cursor is at the bottom of screen
Ctrl+uPage up
Ctrl+dPage down

Edit

The following commands turns vi in edit mode except r.

i aInsert before, after cursor
I AInsert at start, end of line
O oInsert new line above, below cursor
rReplace character at cursor
JJoin current line with line below
CCut from cursor to the end of line and enter edit mode

Undo

uUndo last change
UUndo all changes on line
Ctrl+rRedo last undo

Cut

ddCut line
dwCut word
xCut character
XCut character left from cursor
DCut from cursor to the end of line

Paste

pPaste after cursor
PPaste at cursor

Visual Mode

In visual mode, use movement commands to extend selection.

vStart visual mode
VStart linewise visual mode
yCopy marked text
dDelete (for cut and paste)
< >Indent
.Repeat indent
~Switch case

Multiple Files

:e fileType path and filename, use TAB to autocomplete
:bnGo to next buffer
:bpGo to previous buffer

Multiple Windows

Ctrl+wsSplit window horizontally
Ctrl+wvSplit window vertically
Ctrl+wwSwitch between windows
Ctrl+wqClose window