Vim An Advanced Text Editor
Vim An Advanced Text Editor
1
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
2
Introducing vim
●Newer version of vi, the standard Unix text
editor
Executing vi runs vim by default
● Advantages:
Speed: Do more with fewer keystrokes
Simplicity: No dependence on mouse/GUI
Availability: Included with most Unixlike OSes
●Disadvantages
Difficulty: Steeper learning curve than simpler
editors
3
vim: A Modal Editor
●Keystroke behavior is dependent upon vim's
"mode"
● Three main modes:
Command Mode (default): Move cursor,
cut/paste text, change mode
Insert Mode: Modify text
Ex Mode: Save, quit, etc
● Esc exits current mode
● EscEsc always returns to command mode
4
vim Basics
● To use vim, you must at least be able to
Open a file
Modify a file (insert mode)
Save a file (ex mode)
5
Opening a file in vim
●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
6
Modifying a File
Insert Mode
● i begins insert mode at the cursor
Many other options exist
A append to end of line
I insert at beginning of line
o insert new a line (below)
O insert new line (above)
7
Saving a File and Exiting vim
ex mode
Enter Ex Mode with :
Creates a command prompt at bottomleft of
screen
Common write/quit commands:
:w writes (saves) the file to disk
:wq writes and quits
Ese ZZ writes and quits
:x exit saving changes
:w filename save as a filename
:e filesname edit filename
8
:q! quits, even if changes are lost
Using Command Mode
Default mode of vim
Keys describe movement and text manipulation
commands
Commands repeat when preceded by a number
Example
Right Arrow moves right one character
5, Right Arrow moves right five characters
9
Moving Around
Command Mode
Move by character: Arrow Keys, h, j, k, l
Nonarrow keys useful for remote connections to
older systems
Move by word: w, b
Move by sentence: ), (
Move by paragraph: }, {
Jump to line x: xG
Jump to end: G
10
Search and Replace
Command Mode
Search as in less r /, n, N
Search/Replace as in sed
Affects current line by default
Use x,y ranges or % for whole file
:1,5s/cat/dog/
:%s/cat/dog/gi
11
Undoing Changes
Command Mode
u undo most recent change
U undo all changes to the current line since
the cursor landed on the line
Ctrl-r redo last "undone" change
12
Configuring vi and vim
Configuring on the fly
~/.vimrc or ~/.exrc
A few common configuration items
:set number
:set autoindent
:set textwidth=65 (vim only)
:set wrapmargin=15
:set ignorecase
Run :help optionlist for a complete list 13
Learning more
vi/vim builtin help
:help
:help topic
Use :q to exit help
vimtutor command
14