220 likes | 390 Views
The vi editor. The vi Editor. vi has 2 modes: command mode (initial or "default" mode) insert mode [Esc] is used to switch to command mode. In general, vi commands: are case-sensitive are not shown on the screen when you type them do not require an [Enter] after the command.
E N D
The vi Editor • vi has 2 modes: • command mode (initial or "default" mode) • insert mode • [Esc] is used to switch to command mode. • In general, vi commands: • are case-sensitive • are not shown on the screen when you type them • do not require an [Enter] after the command.
Open a File • vi is working on a copy of your file in the buffer: your edits will not affect your original file until you save the buffer. • $ vi [filename] • If the filename is omitted, vi will open an unnamed buffer. • A filename • must be unique inside its directory. • can include any ASCII character except a slash • $ vi practice • $ vi letter
Saving/Loading File • First check that you are in command mode by pressing [ESC] • To save file and quit the vi: • ZZ • :wq • To safe file without quitting vi: • :w <filename> (write to specified file) • :w (write again) • :w! <filename> (to override existing file) • To load existing file: • :r <filename> • To edit new file: • :e <filename>
Quitting vi • To return to the last saved version of the file: • :e! [Enter] • :q! [Enter] (with quitting from vi) • vi normally won't let you throw away your edits. The exclamation point added to the :e or :q command causes vi to override this prohibition. • To quit vi if no changes were made • :q
Moving the Cursor • up k • down j • left h • right l • <number><direction>: • 4l moves the cursor four spaces to the right, just as if you had typed l four times (llll) • A line is any text entered between newlines. • beginning of the current line 0 • end of the current line $ • :set nu (to display line numbers)
Moving the Cursor by Blocks • To the next word • w (symbols and punctuation as equivalent to words) • W • To the previous word • b (symbols and punctuation as equivalent to words) • B • Movement commands take numeric arguments • 4w, or 5B • To the line number nn • :nn • nnG • For example, :4 or 4G (goes to the 4th line) • To the last line • :$
Inserting Text • i inserts text before cursor. • a appends text after cursor. • A appends text to the end of current line. • I inserts text at the beginning of line. • With numeric prefixes, you might insert a row of underlines or alternating characters: • 50i*[ESC] inserts 50 asterisks • o opens blank line below cursor for text. • O opens blank line above cursor for text. • All of these commands leave you in insert mode. After inserting text, press [ESC] to escape back to command mode. • J joins two consecutive lines. • Using a numeric argument with J joins that number of consecutive lines: 3J.
Changing Text • Command c replaces any text in your file specified by movement command: • cw to the end of the current word • c2b back two words • c$ to the end of line • c0 to the beginning of line • c, like i and a, leaves you in insert mode until you press the [ESC]. • General format of commands: • (command)(number)(movement command) • (number)(command)(movement command) • Lines shortcuts • cc changes an entire line. • C is shortcut for c$. • r replaces a single character with another single character. No [ESC]. • R overstrikes existing characters with new characters. • s (S) deletes character at cursor (line) and substitute text. • ~ changes a case of letter. No number prefix or movement .
Deleting Text • Command d deletes any text in your file. • d is combined with a movement command to specify what to delete: • dw the current word • d2b back two words • d$ to the end of line • d0 to the beginning of line • Lines shortcuts • dd deletes an entire line (2dd) • D is shortcut for d$. • x deletes a single character before the cursor. • 5x deletes 5 characters before the cursor • :<range>d deletes lines in the range. Examples of the range are • 1,$ • 1,. • .,$ • 5,12 • .,.+2
Moving Text • "cut and paste“ paradigm • p puts the text in the buffer after the cursor position. • P puts the text before the cursor. • :nn , [Enter], p – to puts the text after nn position • Once you delete text, you must restore it before the next change command or delete command; otherwise, deleted text will be lost. • Transposing Two Letters : • xp (delete character and put after cursor command) transposes two letters. • There is no command to transpose words.
Copying Text • A yank command (y) copies the selected text into a buffer. • You can then place this copy elsewhere in the file with the p command. • y can be combined with any movement command (yw, y$, 4yy). • The shortcut yy, Y operates on an entire line. • :<range>y yanks lines in the range. • Yanking uses the same buffer as deleting. Each new deletion or yank replaces the previous contents of the yank buffer.
Repeating or Undoing Your Last Command • . (period) repeats last editing command. Position the cursor where you want to repeat the editing command, and type a period. • Occasionally, vi has problems repeating a command. • u undoes your last command .The cursor need not be on the line where the original edit was made. • U undoes all edits on a single line, as long as the cursor remains on that line. Once you move off a line, you can no longer use U. • pu • uu
Scrolling • Ctrl+D down half screen • Ctrl+U up half screen • Ctrl+F forward one screen • Ctrl+B back one screen • Ctrl+L refresh screen (useful in telnet) • z[Enter] moves current line to top of thescreen and scroll. • z understands a numeric prefix as a line number that it will use in place of the current line.
Searching • The search command is the special character / (slash): /pattern[Enter] • A pattern can be any sequence of characters. • vi begins the search at the cursor and searches forward, wrapping around to the start of the file if necessary. • To begin a search backward: ?pattern[Enter] • n repeats search in the same direction. • N Repeat search in opposite direction. • / [Enter] repeats search forward. • ? [Enter] repeats search backward.
Searching/Replacing • :<range>s/string to change/desired string substitutes first occurence • :<range>s/string to change/desired string/g substitutes all occurences in the range • You can also use % instead of 1,$ to specify every line in a file. • If you'd like to confirm each replacement before it is made, add the c option (for confirm) at the end of the substitute command: :<range>s/string to change/desired string/gc • If you want to make the replacement, you must enter y (for yes) and press [Enter]; otherwise, simply press [Enter].