Objectives
Upon completion of this unit, you should be able to:
- Use the three primary modes of vi and vim
- Navigate text and enter Insert mode
- Change, delete, yank and put text
- Undo changes
- Search a document
- Save and exit
Introducing vim
1) New version of vi, the standard Unix text editor
- Executing vi runs vim by default
2) gvim: Graphical version of vim
- Applications + Programming �C> Vi iMproved
- Provided by vim-X11 package
3) Advantages:
- Speed: Do more with fewer keystrokes
- Simplicity: No dependence on mouse/GUI
- Availability: Included with most Unix-like OSes
4) Disadvantages:
- Difficulty: Steeper learning curve than simpler editor. Key bindings emphasizes speed over intuitiveness
vim: A Modal Editor
1) Keystroke behavior is dependent upon vim’s “mode”
2) Three main modes
- Command mode (default): Move cursor, cut/paste text, change mode
- Insert mode: Modify text
- Ex Mode: Save, quit, etc
3) Esc exits current mode
4) EscEsc always returns to command mode
vim Basics
1) To use vim, you must at least be able to
- Open a file
- Modify a file (insert mode)
- Save a file (ex mode)
Openig a file in vim
1) To start vi:
- vim filename
- If the file exists, the file is opened and the contents are displayed
- If the file does not exist, vi creates it when the edits are saved for the first time
Modifying a File �C Insert Mode
1) i begins insert mode at the cursor
2) Many other options exist
- A append to end of line
- I insert at the begining of the line
- o insert a new line (below)
- O insert a new line (above)
Saving a File and Exiting vim �C Ex Mode
1) Enter Ex mode with:
- Creates a command prompt at bottom-left of screen
2) Common write/quit commands:
- :w writes (saves) the file to disk
- :wq wirtes and quits
- :q! quits, even if changes are lost
Using Command Mode
1) Default mode of vim
2) Keys describe movement and text manipulation commands
3) Commands repeate when preceded by a number
4) Example
- Right Arrow moves right one character
- 5, Right Arrow moves five characters
Moving Around Command Mode
1) Move by characters: Arrow Keys, h, j, k, l
2) Move by word: w, b
3) Move by sentence: ), (
4) Move by paragraph: }, {
5) Jump to line x: xG
6) Jump to end: G
Search and Replace Comomand Mode
1) Search as in less: /, n, N
2) Search/ Replace as in sed
- Affects current line by default
- Use x,y range of % for whole file
:1, 5s/cat/dog/
:%s/cat/dog/gi
Manipulating Text Command Mode
|
Change (replace) |
Delete (cut) |
Yank (copy) |
Line |
cc |
dd |
yy |
Letter |
cl |
dl |
yl |
Word |
cw |
dw |
yw |
Sentence ahead |
c) |
d) |
y) |
Sentence behind |
c( |
d( |
y( |
Paragraph above |
c{ |
d { |
y{ |
Paragraph below |
c} |
d} |
y} |
Undoing Changes Command Mode
1) u undo most recent changes
2) U undo all changes to the current line since the cursor landed on the line
3) Ctrl+r redo last “undo” change
Visual Mode
1) Allows selection of blocks of text
- v starts character-oriented highlighting
- V starts line-oriented highlighting
- Activated with mouse in gvim
2) Visual keys can be used in conjuction with movement keys: w, ), }, arrow, etc
3) Highlighted text can be deleted, yanked, changed, filtered, search/replace, etc.
Using multiple “Windows”
1) Multiple documents can be viewed in a single vim screen
- Ctrl+w, s splits the screen horizontally
- Ctrl+w, v splits the screen vertically
- Ctrl+w, Arrow moves between windows
2) Ex-mode instructions always affect the current window
3) :help windows displays more window commands.
Configuring vi and vim
1) Configuring on the fly
2) Configuring permanently
3) A few common configuration items
- :set number
- :set autoindent
- :set text (vim only)
- :set wrapmargin=15
- :set ignorecase
4) Run: help option-list for a complete file
Learning more
1) vi/ vim built-in help
- :help
- :help topic
- Use :q to exit help
2) vimtutor command
End of Unit9
1) Questions and Answers
2) Summary
- Use the three primary modes of vi and vim
- Move the cursor and enter Insert mode
- Change, delete, yank and put text
- Undo changes
- Search a document
- Save and exit