CS 497C – Introduction to UNIX Lecture 10: The vi/vim Editor Chin-Chih Chang
Published byModified over 6 years ago
Presentation on theme: "CS 497C – Introduction to UNIX Lecture 10: The vi/vim Editor Chin-Chih Chang"— Presentation transcript:
CS 497C – Introduction to UNIX Lecture 10: The vi/vim Editor Chin-Chih Chang firstname.lastname@example.org
String Search vi supports two forms of searches: –Search for a string or a regular expression in the entire file. –Search for a single character in the current line. Search commands in vi: –/str – searches forward for pattern str. –?str – searches backward for pattern str. –n – repeats search in the same direction. –N – repeats search in the opposite direction. –fch – moves cursor forward to first occurrence of character ch in the current line.
String Search –Fch – moves cursor backward to first occurrence of character ch in the current line. –tch – moves cursor forward onto but before first occurrence of character ch in the current line. –Tch – moves cursor backward onto but before first occurrence of character ch in the current line. –; - repeats search in the same direction made with f, F, t, or T. –, - repeats search in a direction opposite to that made with f, F, t, or T.
Searching with Regular Expressions vi accepts a generalized pattern containing some special characters (metacharacters) as the search expression. This pattern is called a regular expression and is used to match a group of similar strings. The regular expression characters used by vi :
Searching with Regular Expressions –* - matches zero or more occurrences of the previous character. –[pqr] – matches a single character which is either a p, q, or r. –[^pqr] – matches a single character which is not a p, q, or r. –. – matches a single character. –^pat – matches pattern pat at beginning of line. –\<pat – matches pattern pat at beginning of a word. –pat\> - matches pattern pat at end of a word.
Search and Replace (:s) Search and replace commands in vi : –:n1,n2s/s1/s2 – replaces first occurrence of string or regular expression s1 with string s2 in lines n1 to n2. –:1,10s/find/look/g – replaces all occurrences of find with look in lines 1 to 10. –:.,$s/find/look/gc – interactively replaces find with look from current line through the end. –:s – repeats the last substitution on the current line (Linux only).
Handling Multiple Files vi uses the Last Line Mode to handle multiple files and buffers. These are advanced file handling commands: –:r note – reads file note below current line. –:r !date – reads output of date between current line. –e note – stops editing current fine, and edits file note. –e! note – stops editing, but after abandoning changes made to current file.
Handling Multiple Files –:e! – loads last saved edition of current file. –[Ctrl-^] – returns to most recently edited file. –e# – returns to most recently edited file. –n – edits next file (when vi is invoked with multiple filenames). –rew – rewinds file list to start editing first file (when vi is invoked with multiple filenames). You can split the screen into multiple windows in vim. –:sp – splits the window. –[Ctrl-w][Ctrl-w] – moves between windows.