1 © 2012 John Urrutia. All rights reserved. Chapter 6 The vi Editor.
-
Upload
marvin-summers -
Category
Documents
-
view
217 -
download
0
Transcript of 1 © 2012 John Urrutia. All rights reserved. Chapter 6 The vi Editor.
1© 2012 John Urrutia. All rights reserved.
Chapter 6
The vi EditorThe vi Editor
2© 2012 John Urrutia. All rights reserved.
TopicsAbout vi
Getting Started: Creating and Editing
Introduction to vi Features
Command Mode – Moving the Cursor
Changing text
3© 2012 John Urrutia. All rights reserved.
TopicsSearching & Substituting for a String
Miscellaneous Commands
Yank Put and Delete
Reading and Writing Files
Setting Parameters
Advanced Editing Techniques
4© 2012 John Urrutia. All rights reserved.
About viStarted out as ed
Line editor – processes commands against a single line
Displays only 1 line at a time
Cumbersome and awkward to use
5© 2012 John Urrutia. All rights reserved.
About viNext out as ex
Line editor – similar to ed
Added vi mode – visual mode
Displayed full screen of data
Provided Additional functionality
6© 2012 John Urrutia. All rights reserved.
About viToday vi
Many variations LINUX uses vimvi has different options than vim
Extremely Powerful
Complex command structure
Works when all else fails
7© 2012 John Urrutia. All rights reserved.
TopicsAbout vi
Getting Started: Creating and Editing
Introduction to vi Features
Command Mode – Moving the Cursor
Changing text
8© 2012 John Urrutia. All rights reserved.
Getting StartedSpecifying a terminal
Normally system will assign terminal attributes
vi takes advantage of terminal capabilitiesSmart vs Dumb
9© 2012 John Urrutia. All rights reserved.
Getting StartedAn editing session
Start vi by typing
…$ vi someNewFileName
vi presents a screen identifying this as a new file.
10© 2012 John Urrutia. All rights reserved.
Indicates file being created
~ are used as place holder
11© 2012 John Urrutia. All rights reserved.
Getting StartedAn editing session
vi has two modeCommandInput
Default is Command Mode
Quit vi without saving by typing in command mode
:q!
12© 2012 John Urrutia. All rights reserved.
The : sets last line mode
The q! says to quit
immediately
13© 2012 John Urrutia. All rights reserved.
Getting StartedCommand Mode
Executes commands based on the keys pressed.
Commands (case sensitive)Cursor movementChange or Delete existing textSwitch to input mode
14© 2012 John Urrutia. All rights reserved.
Getting StartedCommand Mode
Switching to input modei – insert before cursorI – insert at beginning of line a – append after cursor A – append at end of line o – open line below cursor O – open line above cursor
15© 2012 John Urrutia. All rights reserved.
Getting StartedInput Mode
Anything you type is stored in the file buffer and echoed back to the terminal.
Navigation may be available through the arrow keys depending on your terminal
16© 2012 John Urrutia. All rights reserved.
Getting StartedInput Mode
The terminal control keys can be used in input mode to navigate.
Cntl+W, Cntl+H, Cntl+ULimited control
17© 2012 John Urrutia. All rights reserved.
Getting StartedInput Mode
Trouble Getting around?Go back to command mode. It’s easy
Press ESCape keyIf already in command mode terminal will beep/click/flash or otherwise complain
18© 2012 John Urrutia. All rights reserved.
Getting StartedCommand mode Navigation
Arrow key will move
– Move left one character
– Move right one character
– Move down one line
– Move up one line
h
l
j
k
Keyboard
19© 2012 John Urrutia. All rights reserved.
Getting StartedCommand Mode
Deleting Textdd – delete one line at cursord – delete two lines at cursor dw – delete word to the right of
cursor
Oops-- Undo last action – u
20© 2012 John Urrutia. All rights reserved.
Getting Started
I Quit !!!!ZZ – writes the file and exits
:q! – exit Now! No save
21© 2012 John Urrutia. All rights reserved.
TopicsAbout vi
Getting Started: Creating and Editing
Introduction to vi Features
Command Mode – Moving the Cursor
Changing text
22© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
Simple Online Help:help
Similar to info or man for vim
Tutorial vimtutor
23© 2012 John Urrutia. All rights reserved.
Introduction to vi FeaturesThe five faces of vi
ex – command mode
ex – input mode
vi – is a mode of the ex editorCommand modeInput modeLast line mode
24© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
The vi displayThe status line
Error messagesFile status (read-only)
Special characters~ - Identifies un-used area
25© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
The vi displayIf the terminal becomes
unreadable due to scrolling or messages from othersUse the re-draw function
Cntl+LClears screen and re-paints buffer
26© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
Correcting Text as you typeBack-space to the area you
want and change the input mode to replace (Shift R)
Remember commands are case sensitive!
27© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
The Work BufferAllocated when vi is started
Changes are made to the buffer
Overlays the file when saved
Use view for read-only files
28© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
Line Length & File SizeMaximum line length is limited
to the size of memory
Maximum file size is limited to the available drive space
29© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
Split Screens:vi – can support multiple
split screens
Switching between screensCntl+W:e
30© 2012 John Urrutia. All rights reserved.
Introduction to vi FeaturesSplit Screens can be manipulated to the foreground or background:bg – send to background
:fg – rotate to foreground
:display s – display all screens in background
31© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
File LockingWhen vi opens a file it locks it
Other vi sessions will allow others to only read the original file.
How does it do that?
32© 2012 John Urrutia. All rights reserved.
Introduction to vi Features
The same way it can recover an aborted editing session.Creates a .swp file which can
be used to re-cover some or all of the previous editing session
vi –r filename
33© 2012 John Urrutia. All rights reserved.
TopicsAbout vi
Getting Started: Creating and Editing
Introduction to vi Features
Command Mode – Moving the Cursor
Changing text
34© 2012 John Urrutia. All rights reserved.
Cmnd Mode - Moving the Cursor
For one line onlyfchr – find next character
Fchr – find previous character
W – cursor to next word
B – cursor to previous word
35© 2012 John Urrutia. All rights reserved.
Cmnd Mode - Moving the Cursor
) – go to end of sentence
( – go to beginning of sentence
} – go to next paragraph mark
{ – go to prev. paragraph mark
36© 2012 John Urrutia. All rights reserved.
Cmnd Mode - Moving the Cursor
Cntl+B – Back 1 screen
Cntl+U – Up ½ screen
Cntl+D – Down ½ screen
Cntl+F – Forward 1 screen
Hey! BUDF Cntl yourself !#
37© 2012 John Urrutia. All rights reserved.
Cmnd Mode – Changing
Change commandc3w – Change from cursor to end
of 3rd word
Deletes 3 words right of cursor
Sets insert mode
38© 2012 John Urrutia. All rights reserved.
TopicsSearching & Substituting for a String
Miscellaneous Commands
Yank Put and Delete
Reading and Writing Files
Setting Parameters
Advanced Editing Techniques
39© 2012 John Urrutia. All rights reserved.
Search & SubstitutionSearch Command syntax
/expr. – find the next whatever^ – Beginning of line$ – End of line• – Any character\< – Beginning of word\> – End of word[] – Character class definition
40© 2012 John Urrutia. All rights reserved.
Search & SubstitutionSubstitute Command syntax
:addr expr. s/search / replace / options
addr expr.Starting line , ending line
1 – first line of file
• – current line of file
$ – Last line of file
41© 2012 John Urrutia. All rights reserved.
TopicsSearching & Substituting for a String
Miscellaneous Commands
Yank Put and Delete
Reading and Writing Files
Setting Parameters
Advanced Editing Techniques
42© 2012 John Urrutia. All rights reserved.
Join the partyMiscellenous
J –Joins 1 or more lines into one by removing the newline
characters
Cntl+G – Re-displays the status line
•(period) – repeats the last command
43© 2012 John Urrutia. All rights reserved.
TopicsSearching & Substituting for a String
Miscellaneous Commands
Yank Put and Delete
Reading and Writing Files
Setting Parameters
Advanced Editing Techniques
44© 2012 John Urrutia. All rights reserved.
Put that Yank in the Delete BinThe General Purpose Buffer
P or p – copy text from GP buffer to Work Buffer
Y or y – Yank (copy) into GP buffer
D or d – Delete from Work Buffer insert into GP buffer
There are 26 Additional “named” buffers. (“a – “z )
45© 2012 John Urrutia. All rights reserved.
TopicsSearching & Substituting for a String
Miscellaneous Commands
Yank Put and Delete
Reading and Writing Files
Setting Parameters
Advanced Editing Techniques
46© 2012 John Urrutia. All rights reserved.
Read ing, Write ing & RithmaticRead command will copy files into
the addressed line of the Work Buffer. :addr expr. r filename
Write command will write all or part of the Work Buffer to a file.:addr expr. w filename:addr expr. w! filename:addr expr. w>> filename
47© 2012 John Urrutia. All rights reserved.
TopicsSearching & Substituting for a String
Miscellaneous Commands
Yank Put and Delete
Reading and Writing Files
Setting Parameters
Advanced Editing Techniques
48© 2012 John Urrutia. All rights reserved.
Setting ParametersParameters are used to configure vi
at start-up.These can be set or reset at the
command line. Or in lastline command mode.
Common Parameters(no)number, wrapmargin, (no)showmode,
(no)flash, (no)ignorecase,
49© 2012 John Urrutia. All rights reserved.
TopicsSearching & Substituting for a String
Miscellaneous Commands
Yank Put and Delete
Reading and Writing Files
Setting Parameters
Advanced Editing Techniques
50© 2012 John Urrutia. All rights reserved.
Advanced commandsI’ll Edit one more for the road
:e filenameEdits the new file if current buffer is
unchanged
:e! filenameEdits the new file even if current buffer has
changed
Preserves the named buffers
51© 2012 John Urrutia. All rights reserved.
Advanced commandsvi got a new shell
:shSpawns a new shell and then vi sleeps
:! commandExecutes a shell command and returns to
vi
!! commandExecutes a shell command and returns std
output and replaces current line