CSE 15L: SOFTWARE TOOLS �AND TECHNIQUES
Instructor: Onat Gungor
SCHEDULE
Last Lecture: Clean Coding
Today: Vim
LAST LECTURE REVIEW
LAST LECTURE REVIEW
TO DO
UNIX VIM EDITOR
Why is it useful to know Vim?
Lightweight and found on every Unix system
Fast code editing
Commonly used by industry and popular
Very customizable and extensible
Other Choices: emacs, sublime, nano, …
Vim: Vi Improved
Good resource: vimgenius.com
UNIX VIM EDITOR
k
j
h
l
The cursor keys should also work. But using hjkl, you will be able to move around much faster, once you get used to it.
UNIX VIM EDITOR
Command | Description |
vim <filename> | Edits an existing filename or creates a new one if filename does not exist. |
:q | Exit without changes. Assumes no changes have been made. |
:q! | Exit without changes. Assumes changes have been made. |
:wq | Save the file and exit the editor. |
:w | Write to the file that you are editing without exiting the editor. |
x | Delete the character under the cursor. |
File interaction Commands (open, close, save)
UNIX VIM EDITOR
Vim Modes
Command Mode
Insert Mode
UNIX VIM EDITOR
Command | Description |
i | Enter insert mode at this location |
<esc> | Exit insert mode |
o, O | Insert new line after/before current line |
a, A | Append after cursor, end of current line |
I (capital i) | Insert at beginning of line |
Insert Mode
Command Mode
Command mode has also two sub-modes:
UNIX VIM EDITOR
c[n]m
n: count
c: command
m: motion
e.g., d3w: delete 3 words
Delete Command Motions
Symbol | Description |
d | line |
w | to the start of the next word |
e | to the end of the current word |
$ | to the end of the line |
^ | to the beginning of the line |
Undo and redo changes
u: undo the last command
U: fix a whole line
CTRL-R: redo the command
Pressing just the motion while in Normal mode without a command will move the cursor as specified.
QUIZ TIME
UNIX VIM EDITOR
Command | Description |
[n]Y OR [n]yy | copy (yank) n lines |
P (p) | put yanked or deleted data before (after) the cursor |
r<x> | replace the character at the cursor with x |
ce | change until the end of a word |
cc | change until the end of a line |
:s/old/new/g | substitute ‘new’ for ‘old’ in a line |
:#,#s/old/new/g | substitute ‘new’ for ‘old’ between two lines |
:%s/old/new/g | substitute ‘new’ for ‘old’ in a file |
cnm format is applicable.
QUIZ TIME
UNIX VIM EDITOR
Command | Description |
CTRL-G | show your location in the file |
G | move to the bottom of the file |
gg | move to the start of the file |
<line-number>-G | return to the line-number |
/<phrase> | search for the phrase forward |
?<phrase> | search for the phrase backward |
n | search for the same text again |
% | search for the matching parenthesis or bracket |
Cursor location and file status
Search commands
UNIX VIM EDITOR
“This line should be split.”
“This line should
be split.”
UNIX VIM EDITOR