Computation‎ > ‎

Vim

Vim is a text editor that can be opened using the Terminal or using a variety of GUI programs like MacVim.  There are many advantages over basic text editors like "nano" but it is a bit more complicated to use.

To Open or Create a new file
vim Test_file.txt

When the file opens you will be in the "Command Mode".  Vim has two modes "Command" and "Insert".

Command Mode: allows you to do things like saving files, executing commands, moving the cursor, cutting and pasting lines or words, and find/replace

Insert Mode: allows you to insert text into the file.

THE BASICS

To enter Insert Mode
i

To return to Command Mode (NOTE: When in doubt press Esc twice)
Esc

To quit (Shift-;/: , then q, then enter/return)
:q

To quit, even if you have made changes
:q!

To Save
:w

To Save and Quit
:wq    OR    ZZ
 
To Save as another filename
:w NewFileName.txt

Show/Hide line numbers
# Show line numbers
:set number    or    :set nu
# Hide line numbers
:set nonumber    or    :set nonu


THINGS YOU CAN DO IN COMMAND MODE

Moving Around
k         Moves the cursor up one line.
j         Moves the cursor down one line.
h         Moves the cursor to the left one character position.
l         Moves the cursor to the right one character position.
0 or |    Positions cursor at beg inning of line.
$         Positions cursor at end of line.
1G        Move to the first line of the file
G         Move to the last line of the file
nG        Move to nth line of the file
:n        Move to nth line of the file
fc        Move forward to c
Fc        Move back to c
H         Move to top of screen
M         Move to middle of screen
L         Move to bottom of screen
CTRL+d    Move forward 1/2 screen
CTRL+d    Move forward 1/2 screen
CTRL+f    Move forward one full screen
CTRL+u    Move backward 1/2 screen
CTRL+b    Move backward one full screen
CTRL+e    Moves screen up one line
CTRL+y    Moves screen down one line
CTRL+u    Moves screen up 1/2 page
CTRL+d    Moves screen down 1/2 page
CTRL+b    Moves screen up one page
CTRL+f    Moves screen down one page
CTRL+I    Redraws screen

Editing Contents
i         Inserts text before current cursor location.
I         Inserts text at beginning of current line.
a         Inserts text after current cursor location.
A         Inserts text at end of current line.
o         Creates a new line for text entry below cursor location.
O         Creates a new line for text entry above cursor location

Delete Contents
dd        Deletes the line the cursor is on.
x         Deletes the character under the cursor location.
X         Deletes the character before the cursor location.
dw        Deletes from the current cursor location to the next word.
d^        Deletes from current cursor position to the beginning of the line.
d$        Deletes from current cursor position to the end of the line.
D         Deletes from the cursor position to the end of the current line.

Copy/Paste
yy        Copies the current line.
yw        Copies the current word from the character the lowercase w cursor is on until the end of the word.
p         Puts the copied text after the cursor.
P         Puts the yanked text before the cursor.

Find/Replace
:s/find/replace/g

To access unix command from inside the editor
:! command
:! ls *.txt