440 likes | 890 Views
Unix vi Editor. Command line full screen editor. Agenda. Announcements: Review of homework Lecture on vi editor Exam review. Objectives. Upon successful completion of this lesson the student, using the vi editor, should: Be able to create a new text file.
E N D
Unix vi Editor Command line full screen editor
Agenda • Announcements: • Review of homework • Lecture on vi editor • Exam review
Objectives Upon successful completion of this lesson the student, using the vi editor, should: • Be able to create a new text file. • Be able to insert data from various locations in the file. • Be able to navigate through a text file using: • Cursor navigation (without using input mode). • Searching forwards and backwards. • Be able to modify existing text in an efficient manner (without using input mode). • Be able to delete unwanted text (without using input mode). • Be able to correctly switch between input and insert mode. • Be able to save or ignore the modifications made to the text file
Topics Covered • Introduction to the vi editor: • How vi interacts with files • vi insert mode • entry • exit • vi edit mode (inline and ex commands) • command syntax • typical navigation commands • typical delete commands • typical modify commands
vi Editor Command Syntax vi –options filename Selected options: • +[num] # start at line num, otherwise the last line • +/pattern # find the first occurrence of the pattern • -r # recover filename (from .filename.swp) • -R # read only mode (does not allow changes)
Why Do We Need to Knowthe vi Editor • Found on almost all Unix systems. • Good to know, as many commands use vi syntax
vi File Interaction nothing (if file does not already exist) FileC vi FileA :r FileC Work space :w (writes but does not exit) :w FileB (if file does not exist)1 :w! FileB (if file exists) 1 FileA :x or ZZ (writes and exit) FileB :q! No changes .FileA.swp 1. Note- :w[!] simply writes to the file, the file being edited is still the same
vi File Interaction (2)Read-only mode read only mode vi -R FileA Work space :w FileB (if file does not exist) 1 :w! FileB (if file exists) 1 FileA FileB ZZ or :q No changes 1. Note- :w[!] simply writes to the file, the file being edited is still the same
vi File Interaction (3)Recovery mode recovery mode vi -r FileA .FileA.swp Work space :w FileB (if file does not exist) :w! FileB (if file exists) :w or ZZ FileA FileB ZZ,:q, or q! No changes 1. Note: The swap file must be manually deleted (e.g. rm .FileA.swp) after recovery has been accomplished.
vi Editor When Things Go Wrong A common error is to accidentally leave the caps lock on when issuing commands. The vi editor is case sensitive and will not necessarily do what you want it to do if the caps lock is on. Another common error is to be in the incorrect mode when typing. Sometimes we begin entering data when we are not in insert mode. Nothing will happen until we type in one of the insert mode characters (e.g. i, o, O, A, etc). Related to this is when you forget to exit the insert mode and try the edit commands. Be sure to hit the Esc key prior to editing.
vi Editor When Things Go Wrong Remember to create the directory, prior to creating a file using the vi editor. For example, if you wish to create a new file called labin in the mydata directory (that does not currently exist), you must do this: • mkdir mydata • vi labin Attempting to do it all using the vi editor will result in errors. If the directory mydata does not exist, do NOT do this: vi mydata/labin
vi Editor When Things Go Wrong A common error is to accidentally type the control-Z instead of the shift-ZZ. The control-Z stops and terminates the editor without saving the contents. If this occurs follow these steps: • type in ps to see the active processes • kill the vi editor using the kill -9 pidnumber command • Upon reentry into the editor, you will be notified of the recovery file .filename.swp. If you wish to save the changes, use the R option and then :w to write the changes out. • upon exiting the editor you will have to delete the .filename.swpfile.
vi Modes • Contains two modes: • edit – command line mode • Default • Most powerful feature of the vi editor • Allows for efficient navigation and editing of existing text. • insert • Used to enter text into a file (exited via Esc key) • Multiple ways to enter this mode • Not efficient for navigation or editing!
Insert Commands NOTE: Insert commands do not have multipliers or scale modifiers
Two Reasons to Know the vi Commands Many of us will discover that learning all of these commands is not important, because we can simply enter input mode and proceed like most Window’s style editors. This is NOT true!!!! Because the files we are editing are small, navigation and changes are fairly simple. In the real world these files can be quite significant in size, often in the thousands of lines. Knowing the editor commands will greatly improve your ability to work with these large files. Because these commands may appear on your test, you might as well practice the commands, which after a bit of use will greatly improve your editing skills.
edit mode • The default edit mode also has two sub modes • in line - where the cursor moves within the text • ex commands - where the cursor moves to the bottom of the screen. ex commands are preceded with a colon (:), a forward slash (/), or a question mark (?)
Edit modeGeneral Command Syntax The general format for commands is ncm Where: • n is an optional multiplier value • c is the command • m is an optional scale modifier Examples: 3dw – delete 3 special character delimited words dW – delete a single, space, delimited word
Delete Commands ndm where modifiers include:
Splitting and Joining Lines • If you wish to split a line between words, position yourself on a space between the words to be split, then use the replace (r) followed by the return key. This will replace the space with a carriage return forming a new line. • If you wish to join or combine two lines go to the upper of the two lines to be joined, then key in the uppercase “J” for join.
Splitting and Joining Lines Example: We wish to split the line below between the word “should” and “be”. “This line should be split.” To join the lines: “This line should be split.” • Position the cursor here • Key in “r” followed by “enter” • Position the cursor anywhere on this line • Key in “J”
Navigation Commands nc where c is:
Copy/Paste Commands Note: to cut and paste use the dm commands to delete then the p or P to paste
Substitute/Replace Command Important: By convention the delimiter for the patterns is the forward slash (/). When using the substitute command, make sure the delimiter is not part of the old pattern or the new pattern. For example if we wished to change the date from mm/dd/yyyy format to mm.dd.yyy format, we cannot use the forward slash (/) or the period (.) as delimiters. we could use something like this: :% s $/$.$ g where the dollar sign ($) is now the delimiter
Some “nice to know” features • To turn line numbers on and off use: • :set nu and :set nu! • To execute a single Unix command from inside the editor use: • :!cmd • To temporarily return to the shell use: • :sh (type “exit” to return to editor) • To repeat a colon command or to go back to earlier colon commands use: • : ↑ or↓(up arrow or down arrow) • To show the name of the current file use: • :f
Topics Covered • Introduced the vi editor: • How vi interacts with files • vi insert mode • entry • exit • vi edit mode (inline and ex commands) • command syntax • typical navigation commands • typical delete commands • typical modify commands
Unix vi Editor Command line full screen editor