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.txtWhen 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
To return to Command Mode (NOTE: When in doubt press Esc twice)
To quit (Shift-;/: , then q, then enter/return)
:qTo quit, even if you have made changes
:q!To Save
:wTo Save and Quit
:wq OR ZZ
To Save as another filename
:w NewFileName.txtShow/Hide line numbers
# Show line numbers:set number or :set nu# Hide line numbers:set nonumber or :set nonuTHINGS 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.iEsc0 or | Positions cursor at beg inning of line.$ Positions cursor at end of line.1G Move to the first line of the fileG Move to the last line of the filenG Move to nth line of the file:n Move to nth line of the filefc Move forward to cFc Move back to cH Move to top of screenM Move to middle of screenL Move to bottom of screenCTRL+d Move forward 1/2 screenCTRL+d Move forward 1/2 screenCTRL+f Move forward one full screenCTRL+u Move backward 1/2 screenCTRL+b Move backward one full screenCTRL+e Moves screen up one lineCTRL+y Moves screen down one lineCTRL+u Moves screen up 1/2 pageCTRL+d Moves screen down 1/2 pageCTRL+b Moves screen up one pageCTRL+f Moves screen down one pageCTRL+I Redraws screenEditing 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 locationDelete 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/gTo access unix command from inside the editor
:! command:! ls *.txt