Using Vim Scripts to determine whether to set 80 chars word wrap

Here’s a little quick tip for those of you out there that use the ViM text editor. The following is from my personal ~/.vimrc file, it is used to set 80 column word wrap. In other words when the cursor gets to the end of a standard 80 column display, it wraps onto a new line. This is great if you use Vi to post emails or to newsgroups. Bad if you write code in it.

So I thought wouldn’t it be great if it could detect whether I’m writing an email (I use the alpine email program, which launches a program called pico, which then hands off to vim for editing my email). If it doesn’t detect the word pico in the command line argument (the command line for alpine is vim /tmp/.pico02323 or something like that), then it uses the default of no word wrap. Here’s the excerpt from the .vimrc that does this (oh, and also switches nifty colour syntax highlighting, set for a dark background terminal):

"my .vimrc
 :set syntax=on
 :set background=dark

” If I’m writing an email with alpine, I want 80 chars wrapping terminal ” test for the existance of the word pico in each of the args in argv()

:let i = 0
 :while i < argc()
 : let d = escape(fnameescape(argv(i)), '.')
 : if d =~ "pico"
 : set textwidth=80
 : endif
 : let i=i+1

If you are using mutt, the elegant way to do it is to set up the editor within your .muttrc. The following sets the wrap length to 74 chars, but you can set whatever you like.

set editor = "vim -c 'set tw=74' -c 'set wrap'"



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.