Introduction
* Screen-based editor
* Case sensitive
* Two modes
o Command mode- perform administrative tasks such as saving files, executing commands, moving the cursor, cutting (yanking) and pasting lines or words, and finding and replacing.
o Insert mode- insert text into the file
Starting the vi Editor
vi filename- creates a new file or open an existing file
vi -R filename- opens an existing file in read only mode
view filename- opens an existing file in read only mode
Inserting text
vi always opens in command mode.
i- insert text before the current cursor location
Esc – back into command mode.
I - Inserts text at the beginning of current line
a - Inserts text after current cursor location
A - Inserts text at the end of current line
o - Creates a new line for text entry below cursor location
O - Creates a new line for text entry above cursor location
Getting out of vi
:q -quit without saving
:wq / ZZ- quite while saving
:w- save the file
:w filename2- save the file by another name
:q!- enforce to quit without saving
Moving cursor in command mode
k – up one line
j - down one line
h - to the left one character one position
l- to the right one character one position
0/| -beginning of line
$ - end of line
:x – to the line number x
Deleting characters
x -Deletes the character under the cursor location
X - Deletes the character before the cursor location
dd- Deletes the line the cursor is on
2dd-Deletes 2 lines
D - Deletes from the cursor position to the end of the current line
Change commands
cc - Removes contents of the line, leaving you in insert mode
r - Replaces the character under the cursor. vi returns to command mode after the replacement is entered
s - Replaces the current character with the character you type. Afterward, you are left in insert mode
Copy and Paste Commands
yy- Copies the current line
p- Puts the copied text after the cursor
P- Puts the yanked text before the cursor
Advanced Commands
J - Join the current line with the next one. A count joins that many lines
<< - Shifts the current line to the left by one shift width
>> - Shifts the current line to the right by one shift width
~ - Switch the case of the character under the cursor
^G - Press CNTRL and G keys at the same time to show the current filename and the status
U- Restore the current line to the state it was in before the cursor entered the line
u - Undo the last change to the file. Typing 'u' again will re-do the change
:f filename - Renames current file to filename
Searching word and character
/ ? - search the strings
t/ T -search for characters in the current line only
Set commands
:set nu - Displays lines with line numbers on the left side
:set ic - Ignores case when searching
:set ai - Sets autoindent
:set ro - Changes file type to "read only"
Text Buffers in VI
36 buffers for storing pieces of text, a general purpose buffer
“mdd -delete the current line and paste to buffer m
”mp – paste from the buffer m to the current position
Abbreviations
:ab st string- use st as an abbreviation instead of string
:una st – to remove the abbreviation
Running Commands
:! ls - if you want to check whether a file exists before you try to save your file to that filename
Press any key to return to the vi session
Recovering Your Work When Something Goes Wrong with Your Terminal
The VI editor edits a temporary copy of your file
After the editing is complete, or when you tell it to save, it puts the contents of the temporary copy into the original file.
If something goes wrong while you are editing your file, the VI editor will attempt to save whatever work you had in progress, and store it for later recovery.
vi -r newfile – recovering the newfile and we must save it.
No comments:
Post a Comment