IT 244 – Chapter 6 The vim Editor: vi was the Visual Interface to a very cryptic one-line-oriented editor called ex. vim is a sophisticated ascii text editor, which like vi, is part of the ex editor. o Meant for writing code, short notes, and some types of scripting. o Uses the same keystrokes as vi for moving around and moving between edit and command modes. Vim Modes: Ex Command/Input mode Vim Command/Input/Last Line mode o Command mode is where you give a command that doesn’t start with a “:”. o Last Line mode is where you give a command starting with a “:” o Move into Command mode with the <Esc> key. Getting help: :h[elp] – Shows a help screen. Read the initial screen so that you know how to move around and how to get exit (with :q) :h[help] <command> - Shows help for the given command Editing a file: vim <filelist> - Opens an edit session for the first file given in <filelist> whether it exists as a file or not. vim +[num] <file> - Opens an edit session at the line in <file> denoted by num vim +/{pattern} – Opens at the first occurrence of the given pattern o “:help search-pattern” gives available patterns Ending an edit session – vim must first be in Command mode o ZZ or :q[!] – quit without saving changes o wq! or x! – quit after saving changes vim –R <file> – open in read-only mode (same as view <file>) Editing multiple files: vim [<file-list> | <pattern-that-results-in-a-file-list>] :n – goes to the next file :e# - goes back to the previous file. o If used consecutively, it will toggle between the last two files. o Ctrl-Shift-^ (or on some systems, Ctrl-Shift-~) will also toggle. :e#<num> - goes to the file with position <num> in the list. <shift><ctrl>~ - toggles between the last two opened files :rew – rewinds the file pointer the first file in the list :ar – list all file names to be edited :la – goes to the last file in the file edit list Command Mode Commands: <Esc> goes to Command mode Ctrl-g – Shows the current status: o Name of the file being edited o Status {modified, readonly, or not shown) o Current line number and total number of lines o Percentage of the file that this line number represents o Column on this line that the cursor is currently at Moving around: o [num]Shift-G – goes to the last line of the file. If num is provided, this will go to the line in the file denoted by num. If num is greater than the number of lines in the file, it just goes to the end of the file. o w – moves forward to the start of the next word (including punctuation as words) o W – moves forward to the start of the next word (excluding punctuation) o e – same as w but to the end of the next word o E – same as W but to the end of the next word o b – same as w but backwards o B – same as W but backwards o h – moves left – same as left arrow o l – moves right – same as right arrow o j – moves down – same as down arrow o k – moves up – same as up arrow o H – moves to the first character at the top of the viewable text o M – moves to the first character at the middle of the viewable text o L – moves to the first character at the bottom of the viewable text o J – NOTE: not a move: concatenates the next line to the end of the current line o K – NOTE: not a move: when the cursor is positioned over a word will give context sensitive help for that word – useful for programming – must be a word known in the context of the vim session. o f/F[char] – find the next/previous occurrence of char on the same line. o ( | ) – move back to the beginning of this sentence or forward to the beginning of the next sentence. Grammatical on period (no matter how many spaces or tabs separate. o { | } – move back to the empty line before this paragraph (beginning if it is the first paragraph with no empty line before) or forward to the empty line after this paragraph (end of the paragraph if this is the last with no empty line after it). Grammatical on double newline (no matter how many empty lines separate paragraphs). o [num]Ctrl-D – half screen of new text down preceded by num, scrolls that number of lines down o [num]Ctrl-U – half screen of new text up preceded by num, scrolls that number of lines up o Ctrl-F – full screen of new text down (forward) o Ctrl-B – full screen of new text up (backward) Changing text: o r | R – Replaces the current character with the next character | from the current character, each subsequent character replacing the next until <Esc> is hit. o u | U – Undo for the last change | for all the last number of changes to the last line changed. Repeated use of u will undo the change previous to the last undo Repeated use of U toggles between the undo and a full redo o x | X – Delete the current character | character to the left of the cursor o [num]dd – Deletes the current line. If num is provided, it deletes num lines started with the current line. o Deleting with d commands: D – deletes from the current character to the end of the line [num]dw – Deletes the current word. If num is provided, it deletes num words starting with the current word. (same as d[num]w) d0 – Deletes from the beginning of line to just before the current position d^ - Deletes from the first non-whitespace character on the line to just before the current position d$ - Deletes from the current character to the end of the line NOTE: Many more deletions using the d command are given on pp. 170-1, but these that I have given are very commonly used. o Changing with c commands – allows for putting new text in place of the text chosen for change: [num]cw – Changes the current word from the current position in the word to the end (same as c[num]w) c0 – Changes from the beginning of the line to just before the current position c^ - Changes from the first non-whitespace character in the line to just before the current position c$ - Changes from the current position to the end of the line cc – Changes the current line cb – Changes from the beginning of the word to just before the current position NOTE: Many more changes using the c command are given on pp. 172, but these that I have given are very commonly used. o Yank and Put [num]yw – Yanks the current word, or from the current position in the word to the end of the word, and puts that into a buffer for later use. If num is provided, it will yank num words starting from the current position and going forward, no matter if it had to continue on multiple lines. [num]yy – Yanks the current line and puts that into a buffer for later use. If num is provided, it will yank num lines starting from the current line and going down. p – Puts the current content of the buffer into the next position on the line after the cursor if the last yank was by word, or as the next line if the last yank was by line, pushing the current text forward to make room for the full content of the buffer. <shift>p – Same as p except that it puts to the position or line before rather than after the cursor. o Repeating the last change: “.” – Hitting the period key will repeat exactly the last change made at the current position in the file. Pairing this with search functionality or forward/backward movements allows for very quick changes to be made to a file. Substitutions o Substituting with the s command – allows for putting new text in place of the text chosen for change: [num]s – Substitutes for the current character. If num is provided, this substitutes for num characters starting with the current character S – Substitutes for the current line o Searching for strings: /<pattern> - Searches forward in the file for the given pattern. This search will wrap around to continue. ?<pattern> - Same as /, but searches backwards. n | N – Repeats the last search forward | backwards. With normal searches, you must type the whole pattern you want matched and then hit <Enter> before you know if you have found anything. If you use “:set incsearch”, then you can type and as you type the next word that has the current pattern will be highlighted. When you find the word you want, you hit enter to go to the highlighted word. This is incremental searching. Search patterns – characters you can add to your search string: ^ - means beginning of line $ - means end of line . – means any one character \> - means end of word \< - means beginning of word * - zero or more occurrences of the preceding character [] – a list of characters inside brackets means any single character can be matched from that list Commands that move into Edit mode (return with <Esc>): Insert: “i | I” – inserts before the cursor | at the start of the line Append: “a | A” – appends after the cursor | at the end of the line Open: “o | O” – opens a new blank line after | before the current line Insert mode is straightforward use of the keyboard. Last Line mode Commands: :set <> – Sets vim settings for this session o :set all – lists all settings that can be set along with their current value o Some useful settings: :set [ignorecase/noignorecase | ic/noic] – case sensitivity during search :set [number/nonumber | nu/nonu] – line number listing :set [list/nolist] - display tabs and end of lines Substituting a new string for an existing string: o :[address]s/search-string/replacement[/option] address – if % is used, the whole buffer is searched. Otherwise the current line is searched. See p. 177 for more addresses. s – the substitute command search-string – the string to be changed replacement-string – the string to be used as a replacement option – g as an option causes any line with more than one match to have substitution applied to every match (otherwise only the first match in a given line will be substituted for). Recovering after a crash (abnormal termination): When you re-open a file that you were editing in vim when the edit session was abnormally terminated, vim will warn you that the file could be recovered. Exit vim and do the following: o vim –r - causes vim to reread the swap files it created during your previous session. The fact those swap files existed is what caused vim to notify you about a problem. Other considerations: Editing multiple files (there are other commands, but these are commonly used): o vim [<file-list> | <pattern-that-results-in-a-file-list>] :n – goes to the next file :e# - goes back to the previous file <shift><ctrl>~ - toggles between the last two opened files :rew – rewinds the file pointer the first file in the list :ar – list file names