Text Editing with Vim


Tutorial

If you login to classes.csc and run the command:

$ vimtutor

... vim will open an interactive tutorial. This takes about 30 to 60 minutes and will make sure you're familiar with the basics. It's well worth the time. You can use the notes below as a reference after going through the tutorial.

Vim stands for Vi improved. Vi is a much older editor which Vim is based on. If you're on another system and Vim is behaving strangely (for example, the arrow keys aren't working), it's probably set to compatible mode which makes it behave like the original Vi.


Getting Started

If you call 'vim filename' Vim will either open that file or create a new one with that name if it doesn't exist yet.

$ vim filename.ext

... your new file can have any extension (or no extension) and it will still be a text file.


Don't call 'vim' without any arguments:

$ vim

... if you already did, read the summary of modes below then use the Line Mode command 'q!' to close and exit Vim without saving. You should learn a little more about Vim before starting it without a filename argument.


A Few Vim Modes

Normal Mode: When you open or create a file, Vim begins in normal mode. You can move the cursor but cannot edit text from this mode, but instead use it to enter various commands. Some of these commands are very powerful. Pressing ESC at almost any time will return you to normal mode. If you happen to see 'recording' in the lower left, then you've accidentally started a macro recording. Press ESC then q to exit the recording.

Insert Mode: If you press 'i' from normal mode, you will enter insert mode which allows you to edit text as you would in a typical text editor. You can also enter insert mode with other commands such as 'a' for append (just like insert except the cursor is placed after the highlighted character instead of before it). If Vim is set to show your current mode, you'll see '-- INSERT --' at the bottom of the screen. Press ESC at any time to return to normal mode. One typical thing you can't do well from insert mode is delete line breaks in order to combine two lines together. This can be done from normal mode with J (for join; and you must hold shift for capital J). Regardless of where your cursor is on the line, it will delete the line break at the end of the current line joining it with the next line. Review basic commands below for more information on Join and other commands.

Line Mode: To enter line mode, type a colon ':' (SHIFT + ;) while in normal mode. You'll see a : appear at the lower left and Vim will await your line command. This mode can do many things, but at the basic level you'll use it to save and quit. Type your command and press enter and Vim will execute the command.

:wq	#Save the current document and close it
	#Vim also closes if you only had one file open

:w	#Save the current document without closing

:q!	#Close a document without saving

:qa!	#Close all open documents without saving and exit Vim

		

Some Useful Command Examples

Deleting: You can always enter insert mode and use the delete key, but Vim offers many more powerful ways to delete text from normal mode. Notice in the commands below that there is an optional number N that will repeat a command N times.

x	#delete 1 character under the cursor
5x	#delete 5 characters
Nx	#delete N characters
dd	#delete 1 line
3dd	#delete 3 lines
del	#delete 1 character under the cursor
5del	#does nothing! (del is a special key and doesn't support repeating)
		

Other commands: If you begin typing a number for duplicating a command and make a mistake, just press ESC to start over. Commands are case sensitive, so be sure to hold SHIFT for uppercase lettered commands.

u	#undo the last action (can be pressed repeatedly)
J	#join two lines (deletes the line break from the current line)
e	#move the cursor forward one word (very useful for basic navigation!)
b	#move the cursor backward one word
10e	#move the cursor forward 10 words
b5	#move the cursor backward 5 words
o	#open a new line for editing below the current one and enter insert mode
3o	#same as above, but create 3 copies of whatever you enter
i	#enter insert mode at the cursor
5i	#same as above, but create 5 copies of whatever you enter (which is a little strange)
		

Search: Press the / key to open a search prompt from normal mode. Type a string and press enter for a basic search operation. This can be very useful to quickly jump to a precise location in a long line for editing. And certainly it can be useful to find any text string in a large file. Search goes forward from the cursor by default. Use ? (that is SHIFT+/) to search backward from the cursor. Search is case insensitive by default (on our server).


Vim Cheat Sheet

The notes below summarize everything introduced in the vimtutor tutorial, plus a few extra commands. Wherever you see [brackets], don't type the square brackets, replace them with whatever is called for in the description.

Commands (work independently or preceded by an N)

Esc	cancels any partial command (returns to normal mode)
i	insert: enter insert mode at cursor
a	append; enter insert mode after cursor
A	move to end of line and append (entering insert mode)
o	open a new line below current line
O	open a new line above current line

x	delete a single character (under the cursor)
dd	delete whole line

u	undo
U	undo/redo all changes to last edited line
ctrl+r	redo

p	put (paste) copy of last single delete command at cursor, for example
	use dd with p to move a single line or make multiple copies of it

r[char]	replace one character under the cursor remaining in command mode
R	enters replace mode instead of insert mode (try it!)

ctrl+g	show status and current line number 
gg	go to first line of the file
[N]gg	go to Nth line
G	go to last line

/	initiate a search (then type text and press enter to find)
?	initiate a backward search
n	repeat last search forward
N	repeat last search backward
ctrl+o	return to last position (before searching); repeatable
ctrl+i	reverse ctrl+o

%	find matching parantheses (if existing) 
	finds backward or forward as appropriate

:w	save already named file to existing name
:w [f]  save to a new file name 

:help	open help (opens as a file in a second window)
:split [file]
        open any file in a second window

ctrl+w	to manage windows; then while managing windows:
        ctrl+w again to switch to next window
        arrows or hjkl to switch focus
        capital H or K to change split style

:r [f]	insert contents of file at cursor
:r ![c]	insert contents of shell command at cursor

ctrl+d	show possible completions for a partial command
	e.g. type :q then ctrl+d to see: qall quit quitall



#Running multiple commands with one line-mode string
#(recall recent commands with up or down arrow)

:[c1] | [c2] | [c3] ...
        chain 2 or more commands together

:w | w file1 | w file2
        for example, saves the open file and saves 2 copies

:![com]	execute any shell command (may include args)
        string multiple shell commands together with;
	once you use ! you can't run additiona Vim commands with |
        instead the pipe | has it's normal shell functionality

:w | !clear; gcc *.c; a.out
        for example: save; clear the screen; compile c code; run it



Partial List of Motions
-can be used alone for cursor movement 
-combine with operators such as delete to define expanded scope
-can be preceded with N for duplication

h j k l	move cursor left, down, up, or right respectively (arrow keys
	weren't originally available or supported by Vi; if arrow
	keys aren't working, try going to command mode and issueing
	the 'set nocp' command aka 'set nocompatible').

space	also moves cursor right
bspace	also moves cursor left
0	home (beginning of line)
$	end  (end of line)
b	move to first letter of current word 
e	move to last letter of current word
w	move to start of next word



List of Operators (use with motions)

d	general form of delete
c	change (deletes then inserts)
y	yank (copy text defined by motions)

Examples:
d$	deletes to end of line
ce	delete to end of word and inserts
3yw	yanks (copies) 3 words (use p after to put (paste))



Search and Replace (press enter after command string)

:s[?]	examples below
	:s/old/new		#S&R old with new one time
	:s/old/new/g		#S&R whole line
	:N,Ms/old/new/g		#S&R lines N to M
	:%s/old/new/g		#S&R whole file
	:%s/old/new/gc		$S&R whole file + prompt



Visual Mode
	
v	press v to enter visual mode and use motions to
	select text (arrows, hjkl, b, e, w   etc.) 

:w [f]	save only the highlighted portion to new file	

:w	would overwrite your file with only the selection 
	Vim warns you and requires w! to actually do it

y	yank (copy) selected text (easier than using motions)

x 	cuts selected text (use p from normal mode to put (paste))



Vim Options and Related Commands
-there are many more options than these
-some are built-in, some must be installed as optional add-ons
-can be set as default behavior in your ~/.vimrc file

:set ic		case insensitive search on
:set hls	highlight search terms on
:set is		incremental search on (shows partial search)

:nohls		disables highlights until your next search

:set nohls	turn off highlighting
:set no[opt]	general form to turn option off
:set [opt]!	switch option  (on to off or off to on)