Editing Files with Vi to End of Line Dd* :N D :Sh Shell out Delete Character (Open Shell) the World’S Best Text Editor Under/Left of Cursor X/X*
Total Page:16
File Type:pdf, Size:1020Kb
vi Modes and Commands vi [ filename ... ] Command summary delete search for pattern lines before/after cursor delete from cursor goto line n Editing Files With vi to end of line dd* :n D :sh shell out delete character (open shell) The World’s Best Text Editor under/left of cursor x/X* Command h,j,k,l,7,9,8,6 Mode move cursor insert at cursor / i/I* one space at first of line :w [name] save file a/A* (stay in editor) append after cursor / at EOL :q! o/O* exit without saving open new line below or above cursor save and exit ! 5 : 5 * may preceed command edit / insert last line ex mode Copyright © 1998-2005 Delroy A. Brinkerhoff. All Rights Reserved. with an integer count mode !cmd5 (command args 5 ) CS 1400 The vi Text Editor Slide 1 of 18 CS 1400 The vi Text Editor Slide 2 of 18 Starting and Stopping vi Cursor Movement Commands Opening and closing files Navigation Pvi is modal (2 to 4 modes depending on who is counting) Ph, 7, ©H move cursor left one character (© is control) < Command mode: all keystrokes are interpreted as directives < Insert: most keystrokes are entered into the document Pj, 9 move cursor down one line < ex: (sub-command mode) interprets commands that begin with : < Last line (access to Unix commands): !command 5 Pk, 8 move cursor up one line Starting vi: P vi [+position] [filename ...] Pl, 6, <space> move cursor right one character < position: line number (end of file if number is omitted), /pat or ?pat for first or last occurrence of pattern pat Pw, b move forward, backward one word < If no file name is given, name the file at save with :w filename < If multiple files are named, the next file is opened with :n PW, B forward, backward 1 space-delimited word PClosing vi Pe move cursor to end of word < :w write (save) file, stay in vi. :w filename write to file filename P$ move to the end of the line < :q! without saving < :wq or ZZ write (save) and quite P^ move to first non-white character on line CS 1400 The vi Text Editor Slide 3 of 18 CS 1400 The vi Text Editor Slide 4 of 18 Scrolling Commands Line Positioning Commands Changing the displayed text Rapid file navigation P©F scroll forward one screen P:n, nG goto line n (:37 or 37G moves cursor to line 37) P©D scroll down ½ screen P:1, 1G move to the first line in the file P©B scroll backward one screen P:$, G move to the last line in the file P©U scroll up ½ screen PH top line on screen P©E, ©Y scroll screen down, up one line PL last line on screen P© denotes a control character PM middle line on screen P+, - next, previous line at first non-white space char P5 next line at first non-white space (same as +) CS 1400 The vi Text Editor Slide 5 of 18 CS 1400 The vi Text Editor Slide 6 of 18 Misc Movement Commands Inserting Text With vi “Advanced” movement commands Editing commands (insert mode commands) P©G, :f5 print the current line number and file status PInserting and appending (entering insert mode) P), ( next, previous sentence < i or I: insert before cursor / at beginning of line < a or A: append after cursor / at end of line P}, { next, previous paragraph < o or O: open a new line below cursor / above cursor P] ], [ [ next, previous section or function PInsert mode commands P% find matching (, ), {, or } < ESC terminate (i.e., exit insert mode and return to command mode) Pma mark current line with letter a < 5 (enter) start a new line < ² (backspace) erase last character P`a move cursor to line marked a < ©W erase last word P'a move cursor to first non-white char on line marked < ©D backtab over outoindent with a (see above) < ^©D backtab to the beginning of the line < ©Vc insert non-printing character c CS 1400 The vi Text Editor Slide 7 of 18 CS 1400 The vi Text Editor Slide 8 of 18 Editing Commands Deleting Text With vi Changing existing text Deleting commands Pcw change word from cursor position to end of word Prc replace character under cursor with character c Px, X delete character under cursor or right of cursor PR replace all characters until <ESC> Pdd delete current line Pxp transpose two characters (delete followed by put) PD delete line (from cursor to end of line) PJ join current and next line (separated by a space) Pdw delete current word (from cursor to end of word) P. repeat last change or command PSUGGESTION: save before deleting large blocks of text PU undo changes on current line-- before line change Pu undo most recent change-- before line change Pp put most recently deleted text (right/below cursor) PP put most recently deleted text (left/above cursor) P~ toggle case of character under cursor CS 1400 The vi Text Editor Slide 9 of 18 CS 1400 The vi Text Editor Slide 10 of 18 vi File Commands Searching In vi Miscellaneous commands Locating text P :r file5 read and insert file at current cursor line P/pattern5 search from current position forward for pattern P ©G, :f5 show line number and file name P?pattern5 search from current position backward for pattern P :n5 edit next file in command line file name list P/5, ?5 search forward, backward for last pattern P :rew 5 rewind to the first file in the argument list Pn, N search forward, backward for last pattern file5 edit file P :e Pfc, Fc find (move to) next, previous character c P :e!5 reedit, discard changes Ptc, Tc move to character before next, following previous P :e#5 edit last file edited (toggles between two files) c P :e!#5 edit alternate file, discard changes P ; , repeat, repeat inverse of last f, F, t, or T CS 1400 The vi Text Editor Slide 11 of 18 CS 1400 The vi Text Editor Slide 12 of 18 Searching In vi vi Multiple Line Commands Continued Searching, replacing, and deleting over multiple lines PCommands work over an optional range where ni is line i PSearching metacharacters (magic characters) < . (dot) current line $ last line in file < ^ beginning of line < < $ end of line P:n1,n2s/pat1/pat2/[gc]5 (substitute pat2 for pat1) < . (dot) any character < g global: replace all occurrences on the line < [xyz] any one of x, y, or z < c confirm: y5 or n5 can be interrupted with control-C < [a-z] any character in the range a to z < :1,$s/counter/COUNTER/g < [^xyz] any character except x, y, or z < :.,$s/\/tmp/\/home/ < * repeat preceding pattern 0 or more times < :1,.s/counter//g < \c treat c as non-magic :n ,n d (delete lines) < \<, \> beginning, end of word P 1 2 < :1,.d < :.,$d PSUGGESTION: save before using these commands CS 1400 The vi Text Editor Slide 13 of 18 CS 1400 The vi Text Editor Slide 14 of 18 Cut, Copy and Paste In vi Cut and Paste Yank and put Continued Pvi has 35 “text buffers” named 1-9 and a-z PDelete commands (x, and dd) place text in a delete buffer PCopy or yank text lines to a buffer: "bnyy (text buffer 1) < " Double-quote character < b buffer name (1-9 and a-z) PText in a delete buffer can be “put” with p or P < n optional number of lines (1 is the default) < p to the right of or below the cursor < P to the left of or above the cursor PDelete text to a buffer: "bndd PRead an existing file into the current file at the current cursor PText in buffers persists between file changes (:n or :e) position with :r filename PText is put at the current cursor position with: "bp PThere are many other versions of these commands < for example: "nyw yanks n words < "ny is followed by a cursor movement command CS 1400 The vi Text Editor Slide 15 of 18 CS 1400 The vi Text Editor Slide 16 of 18 Configuring vi UNIX Shell Commands Settings Accessing Unix from within vi P:set all show current settings (and what can be set) P :sh5 run shell (©D kills shell and returns to vi) P:set ai set (turn on) autoindent mode P :!cmd5 run cmd, then return < :set noai turn off autoindent mode < use % as the current file name for cmd arguments P:set nu turn on line numbering (display only) < must save (i.e., :w5) before running command < :set nonuturn off line numbering < :!spell % run spell command on current file Pset commands (without the colon) can be placed in the P !!cmd5 replace current line with output of cmd configuration file $HOME/.exrc and read each time vi starts < The last line should not be followed by a new-line Pset commands can be placed in the environment variable EXINIT (e.g., setenv EXINIT "set ai" ) CS 1400 The vi Text Editor Slide 17 of 18 CS 1400 The vi Text Editor Slide 18 of 18 .