Vim For Writers
Vim For Writers
Contents
1 Why Vim for Writers? 2 Gvim vs. Vim 3 Line Wrapping and Text Width 4 Files (Opening, Saving, etc.) 5 General Editing of Text 6 Getting Around with the Cursor 7 Scrolling 8 Bookmarks (Marks) 9 Selecting Text, Cutting and Pasting 10 Searching and Replacing 11 Using Ranges 12 Multiple Windows, Buffers, and Tabs 13 Inserting Special Characters 14 Dealing with DOS, Unix conversion problems 1 2 3 3 4 4 5 7 7 8 9 9 10 10 11
15 Spell Checking 16 Macros 17 Learning more about Vim 18 Acknowledgments, License, and Version History
List of Figures
1 2 3 4 5 6 7 8 9 10 11 Files (Opening, Saving, etc.) . . . . . . . . . . . . . . . . . . . . . . . . . . . . General Editing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Advanced Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Simple Search Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Scrolling Relative to Document . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5 6 6 6 7 7 8 9 10 11
Scrolling Relative to Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Visual Mode Selection Commands . . . . . . . . . . . . . . . . . . . . . . . . . Cutting and Pasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Some Common Digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 companion volume to the Woodnotes Guide to Emacs for Writers1, and covers the same information. Your rst task then, if you havent already done so, is to get to know Vim. I recommend the use of a good reference card to learn and remember the commands. There are many available; my personal favorite is the creation of Laurent Grgoire.2 This guide uses the same notation as Laurents reference card, so it would help to have it in front of you as you continue. The graphical vi-vim cheat sheet and tutorial3 is an excellent way to learn Vim and serves as a cheat sheet afterwards (they also have a Dvorak version!). And of course, Vims own tutorial (just type :vimtutor is effective, and should probably be the rst place you begin learning this powerful software.
5 Function Insert before, after cursor Insert at beginning, end of line Change text of motion m Change to end of line Delete text of motion m Delete to end of line Delete letter under, before cursor Change letter under cursor and keep typing Replace letter under cursor Open a new line below, above cursor Join this line to the next Key i, a I, A cm C dm D x,X s r o,O j
Figure 2: General Editing Commands text, and begin editing. A few examples will make the benet clear. As an author I frequently begin a sentence and then decide halfway through that I am not pleased with what I have begun to write. A simple c( instructs Vim to change (erase and prepare for replacement of) everything to the beginning of that sentence. I can just as easily type c7b to change the last 7 words. If Ive edited something mid-paragraph and am ready to now place the cursor at the end of the paragraph and keep writing, its as simple as typing a capital A. Its also quick to select some text using visual mode, press d to delete it, and continue. Note that where delete and insert commands are concerned, pressing the .key (the period) instructs Vim to repeat the last command. If you type dw (delete word) and then 3. the second command will delete 3 more words. Be careful! One last word about editing. Vim remembers up to 100 of your last commands6 This allows you a lot of exibility with the undo command. Press u in command mode to undo your last command, and keep pressing it until you are satised. Control-r works in the opposite direction, redoing what youve just undone.
6 Direction Left, Right Down, Up Screen Line Down, Up Forward, Backward word sentence paragraph Beginning, End of Doc Key h, l j, k gj, gk Key w,b (,) {,} gg, G
Figure 3: Basic Movement Note that if you are using soft wrapping, what Vim considers to be a line is actually what we would call a paragraph since to Vim, a line ends with a new line character. As such, moving up by one line at a time (using j and k) actually moves the cursor up by one paragraph at a time. In that case, use gj and gk to advance the cursor one screen line at a time. You can gain some time with prepending a numerical reference, so typing 3( will take you back 3 sentences, and 5} will advance the cursor 5 paragraphs. Vim is aware of what you have visible on the screen, and allows you to quickly jump around on that basis as follows: Position on Screen Top Line Middle Line Bottom Line line n from top, bottom of window Key H M L nH, nL
7 The f and t commands are most useful for nding characters that appear infrequently in your document, like punctuation marks; as such, I dont use them frequently. But searching using the / and ? commands is a fast way to get around your document. You can type ? Dingleberry to be taken to the most recent use of that word in your document. Continue searching in that direction by pressing n (in this case, since were searching upwards, n will continue searching upwards) or N to search in the opposite direction (in this case, down). Use a prepended numeral to jump that many search results: ? fleabag and then 3n will search for the word eabag and take you to the 4th occurrence.
7 Scrolling
Vim will save you time by eliminating the need to take your hands off the keyboard in order to position the cursor by sliding those little scroll bars up and down with the mouse. Scrolling via the keyboard is immensely efcient.7 At a minimum, get used to the zz command, which will position the cursor and the line youre currently working on, in the center of the screen. Just dont confuse it with ZZ which is a shortcut for exit. Scroll Direction and Amount Line up, down Half page up, down Full page up, down Key control-E, control-Y control-u, control-d control-b, control-f
Figure 6: Scrolling Relative to Document Position Cursor on Screen At the top In the middle At the bottom Key zt zz zb
8 Bookmarks (Marks)
Placing marks through your text is an interesting way of navigating, if you nd yourself navigating back to certain positions frequently. But its more important a tool as part of cutting and pasting. Type m plus a letter from a-z or A-Z to establish a mark, and then (thats the single quote, not the backtick) plus that letter to return the cursor to the position identied by that mark. By distinguishing between upper and lower case identiers, Vim provides you 52 individual marks, more than youll probably need. If you
7 Remember
forget where they are, typing :marks at the Ex prompt will provide you a list of them, plus the rst couple of words at each mark to help you identify them.
Figure 8: Visual Mode Selection Commands The more complicated way is to use the motion commands alone to select areas. For example, if you want to copy the entire sentence you just wrote, you could issue y( (that is, yank to the beginning of the sentence). Youll get no visual feedback for this operation, but the text will be yanked and ready to place elsewhere. As an author, though, this is a fast way to keep your hands on the keyboard. If you dont like what you just wrote, a simple d( will remove your last sentence, d5b will remove the last 5 words, dTZ will delete back until the last occurrence of a capital Z, and so on.
9 the text back to the beginning of the paragraph. If you forget what youve stored in the registers, the Ex command :registers will show them all. Using both marks and registers in a single command means you mustnt mix up your and characters: the former is for marks and the latter for registers. So, ayb means Yank from the cursors position to mark b, and store it in register a. Finally, you can place the contents of any register with the put command (p). For example, hp will put the contents of register h under the cursor, beginning on a new line (a capital P will place it on a new line above the cursor). Command Cut/Delete Yank/Copy Put/Paste below, above cursor Key d y p,P
11 Using Ranges
In the Larry/Moe example above, the % tells the search command to search the entire document. But searching and replacing is one of the few times a writer might be interested in limiting the effect of a command to a certain range of lines rather than to the entire document. Use visual mode to select a certain part of your document, and then limiting the search and replace to that range with * instead of %. Figure 10 shows the other options at your disposal. Here are two examples: .,$s/German/French/g From the cursors current position to the end of the document, replace German by French. *s/Zune/iPod/gc Within the visual area, change all occurrences of Zune with iPod, and conrm each replacement.
10 Range Use , or ; to separate two line positions Current line Last line in le Entire le Visual area Mark t Key ,; . $ % * t
11 Character Name Exclamation mark Apostrophe Greater-Than sign Question mark Hyphen-Minus Left parenthesis Full stop Colon Comma Underline Char ! > ? ( . : , _ Meaning Grave Acute accent Circumex accent Tilde Macron Breve Dot above Diaeresis Cedilla Underline
15 Spell Checking
Vim took a big step forward with version 7, which introduced spell checking and thus made Vim a much more attractive system for writing long text works. To take advantage of the new functionality, begin by typing :set spell, which turns on the spell checker with the default language. Once the spell checker is running, the keystrokes [s and ]s move the cursor from error to error. With the cursor positioned over a misspelled word, the keystroke zg declares that this word is good and should henceforth be ignored; zw identies a good word as one that shouldve been identied as an error. Finally, z= will request a list of suggestions from the ispell program, which allows you to choose what you want. For Vim to be able to add these words to a custom dictionary, you must dene your spellle, and Vims useful approach to doing so is to permit you to dene and use as
12
many spellles as youd like. As such, you can have one spelling list for acronyms you need for one type of le without contaminating another dictionary. For example, :set spellfile=.spellfile.sailing.add creates a custom wordlist where you can put all your arcane sailing acronyms, and then choose not to load that dictionary while working on your medical thesis. This is useful but not perfect I happen to think emacs has the advantage here and consequently prefer to exit vim and use the ispell program to spell check text les already written; the advantage is no highlighted text distracts you. However, as an editing tool, in-program spell checking can be a useful and productive feature.
16 Macros
The easiest way to deal with repetitive text is to assign it to a keyboard macro using the imap command. If you enter :imap br Best regards,, the effect will be to map the expression Best regards, to the key combinaton br. Try a couple of these so you are familiar with the possibilities. Choose something exotic like the double-comma combination I use here to avoid stumbling on those keys in regular text. Note that once youve dened a macro, the cursor will change when typing if you begin to enter those rst keys while it decides whether or not what youre typing triggers a macro. When you quit Vim, the macros will vanish. For a longer term solution, put the macros in a le and source it, as follows. Lets say you create a le called keystrokes.vim Stuff it with lines like imap br Best regards,. Then, from Vim, issue :source keystrokes.vim and the new functionality will be imported. The .vimrc le is sourced automatically upon starting up, so if you have macros (or other custom modications, for that matter) that you would like to have available all the time, edit the .vimrc le (even if you have to create it!) to do so.
13