2010/07/19

Tips for editing Plain Text with Vim

Tip 1: Navigation

When writing Plain Text in Vim, navigation can be a bit tougher than it is while writing programming code. If you write in Vim like you would in Microsoft Word or in OpenOffice Writer, you are going to end up with incredibly long lines, since you probably won't be pressing ENTER at the end of every line.

Out of the box, the Vim j and k keys, change between actual lines (as indicated by line numbers), not between visual (wrapped) lines. If you would like to change between lines on the screen, gj and gk are your friend. You can even consider making this behavior the standard behavior of the j and k keys, by adding

map j gj
map k gk
to your .vimrc file (or by typing :map j gj and :map k gk in command mode when you start Vim).

Another possible solution to this problem (and the one I prefer), is to get Vim to place enters at the end of every screen-line. This is simply achieved by entering in command mode :set tw=78 (tw stands for text-width) if you'd like Vim to place a newline when a line of text is wider than 78 characters. 78 is a standard I like to use but you can of course choose your own value. Like always you can also place set tw=78 in your .vimrc or appropriate ftplugin file.

Even with text-width set, it is still very much possible to make a line either wider than you specified it should be or way too short, for example by joining multiple lines (SHIFT+J) or by deleting words. You could make the paragraph look good by manually placing hard returns and deleting them, or you could do it the smart way.

Moving newlines to make text fill the entered text-width is done with the gq command followed by a movement. To do this to your entire file (be careful, this is probably only appropriate if your file contains only text), you can enter the command gggqG. Explained:

gg          Go to the beginning of the file
  gq        Refill text from here to
    G       ... the end of the file
If you only want to refill a paragraph, probably a better idea than refilling an entire file, give the command gqap while anywhere in the paragraph. Once again explained:

gq          Refill text
  ap        in this paragraph (ap stands for "a paragraph")
Suppose your boss comes by after a few days and decides he wants the file you've been working on in Microsoft Word. Don't worry, you don't have to remove all of the newlines manually, there's a much better and faster way to do that. Just enter the following command (it's a bit complex so I won't blame you if you bookmark this page so you can copy paste it when you need it) :g/./,/^$/j then press enter twice. An explanation:
:g               A global command (affects the entire file)
  /./            search for a letter (so no empty lines)
     ,/^$/       and process everything until you find an empty line
          j      by joining them
In order for this to work on the last paragraph, your file has to end with an empty line (or the /^$/ part would not be matched).

Tip 2: Spelling

Everybody makes mistakes (especially me), and sometimes that includes spelling mistakes, especially when writing in a foreign language. Although the Vim spelling correction is by no means perfect, it's pretty good. As you probably know, spelling isn't turned on by default, so you have to turn it on. You can do this by executing the command :setlocal spell spelllang=en_us, of course replacing en_us with the appropriate locale. If you only want Spell checking while editing certain files, no problem. Just add autocmd BufRead *.txt setlocal spell spelllang=en_us to your .vimrc if you'd like to use US English spell checking for all txt files.

Try typing gibberish and see how Vim underlines the incorrect word with a red line. Move the cursor to the underlined word and press z= in command mode to see Vim's alternatives to the incorrect word.

When Vim incorrectly underlines a word, move the cursor over the word and type zg in command mode to permanently add it to the list of correct words for the language you currently have selected. If you prefer to only temporarily mark this word as correct you can give the zG command.

If you see a word that's not underlined but you'd like it to be underlined because it's a wrong (bad) word, you can permanently make it wrong by positioning the cursor over it and giving the zw command (or the zW command to temporarily mark it as wrong).

Tip 3: Word Count

People often like to know how many words or lines they've typed, either because they want to know how much progress they've made or because there is a limit on the amount of text they are allowed to write (e.g. an essay or article).

There are two ways to determine this, the wrong way and the right way. The wrong way would be to pass the file through an external program like wc (UNIX and Linux users can try this by typing :!wc % in Vim command mode). In all honesty, this way isn't so bad, except for the fact that it's not portable and not very Vim-y.

The right way to do this is to type g CTRL+G (press and release g and then press CTRL+G). For the file I'm writing right now this will output

Col 4 of 4; Line 104 of 104; Word 934 of 934; Byte 5350 of 5352 
That's about it for interesting stuff about editing plain text in Vim. There's not much to it really, just remember to save your work often.

No comments:

Post a Comment