PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Download Kubuntu Today

   Ubuntu

   The Power Of KDE + Ubuntu






PGTS Humble Blog

Thread: Tips/Tricks For Programming etc

GP JPG
One world --- Or none!

A Couple Of Tips For Vim and Gvim


Chronogical Blog Entries:



Date: Mon, 06 Apr 2009 23:38:50 +1000

Vi is the editor of choice for many programmers. And since Bram Moolenaar's heroic programming effort, and the spread of Linux, the variant known as "Vim" has been widely adopted. As Linux has increased in popularity, vim has been adopted throughout the unix-verse. Like most popular Open Source software, there is a port of vim for just about every known "nix". Syntax highlighting makes it much easier to write and debug code. And an amazing team of volunteers have created extensions for just about every language from XML to COBOL.

However, vim can also be a great tool for writing regular letters, poems, theses and other documents. With the addition of a spell-checker and used in combination with a powerful MUA like mutt, you can answer your emails, spell check them from vim and still have access to the full suite of Unix commands. The most powerful text processing command set in the history of computing.

In the past, I used to just write the email out to a temporary file (e.g. mutt.tmp). And then spell check the file with this command:

!aspell -c mutt.tmp

When the spell check finished, I'd just delete the current buffer and read in the alternate buffer with the command:

r#

However by using the inbuilt spell checker, I could do it all in the current window. Getting the spell check to work is so simple it hardly warrants a blog entry. If you have a standard edition installation of vim, enter this command:

help spell

If you read the excellent help screens, you will see that all you need to get started is this command:

setlocal spell spelllang=en_us

Actually, Australian users may find this more helpful:

setlocal spell spelllang=en_gb

That's because (as with Open Office) additional files are required to get the "en_au" region working. It is supported -- you just don't get the necessary files with the standard install.

Once you turn on spelling, you can start working with the keyboard combinations ]s (to move forwards) and [s to move backwards. To get a list of the suggestion use z= to get a menu and zg to add the word as a "good word". Although, you will need to set the spellfile option if you are going to add and remove words. You have the option of setting the spellfile option in your .vimrc file. While you are about it you might want to map the spelling option to a function key. I use the following in the .vimrc file that I use for emails:
" set the spellfile - folders must exist
set spellfile=~/.vim/spellfile.add

"Map the F5 key to turn on spell-checking
map <F5> :setlocal spell spelllang=en_gb

Of course the folders must exist. Windows XP users will have to modify _vimrc file in the home folder (usually this is in %HOMEDRIVE%\%HOMEPATH%). The folder should be in a more Windozy format such as:

     set spellfile=c:/Vim/vim71/spell/spellfile.add

Provided of course that the you installed Vim in C:\Vim.

Of course, there are whole bunch of other options, which you can learn from the help menu. But these are the main commands you need to get started.

If you are using Gvim, the mistakes get highlighted with wavy red lines (as in Open Office). You also get the option of fancy mouse friendly popup menus. All of which works almost identically for Ubuntu, Mac or Windows. However in Ubuntu, you will need to set the mousemodel option to "popup" or "popup_setpos". Or you can simply add the following line to the .vimrc file:

     set mousemodel=popup_setpos

"The popup_setpos" setting will take the cursor to the selected word and offer a popup menu with a single right click. Where as the "popup" setting requires a left click to position the cursor and right click to offer the menu. (Windows users don't need to bother with the mousemodel option. Note that Ubuntu users must "right-click" the selection to actually replace the word.

One problem which you might encounter is removing a word from the "ok" list after it has been added. If you haven't closed the file, you can undo the previous add with the zug command. More problematic is removing a word after you have committed your changes and exited from vim. If you are a Linux user and you have not altered the default settings you can edit the list by entering this command:

	vi ~/.vim/spellfile.add

Inside this file will be all the words you added to your dictionary, one per line. Find the word that you wish to remove and delete that line. Now save the spellfile.add file and use the :mkspell! ~/.vim/spellfile.add command (inside vim) to rebuild the spell dictionary (which will be called ~/.vim/spellfile.add.spl).

Sometimes, if you are publishing snippits of perl or shell scripts online, you might want to make them look nice and fancy like they do in Vim. You can use a handy feature in Gvim to do this. Just click on the Syntax Menu on the toolbar. You will see a selection labeled "Convert to HTML". This converts the entire file to HTML code. If you only wish to convert a small piece of code copy the small piece of code and either use a shebang (#!) or file extension to let Gvim know what syntax you want (i.e. use #!/bin/bash for shell, #!/usr/bin/perl for perl). This is particularly handy if you want to publish snippets of HTML. Generally speaking Gvim is better suited to this type of work then vim, because you paste text directly into the Gvim application.

For example the above snippet of code which describes the contents of my .vimrc file. If I edit it in a file called "vimrc", Gvim figures out that it is a .vimrc file and highlights the syntax accordingly. By clicking the Syntax->Convert to HTML option I can see a HTML version of the code. I Save this as temporary file, edit it and remove the HTML headers leaving the body text that looks like this:
<body bgcolor="#ffffff" text="#000000"><font face="monospace">
<font color="#0000ff">&quot; set the spellfile - folders must exist</font><br>
<font color="#804040"><b>set</b></font>&nbsp;
<font color="#a020f0">spellfile</font>=~/.vim/spellfile.add<br>
<br>
<font color="#0000ff">&quot;Map the F5 key to turn on spell-checking</font><br>
<font color="#804040"><b>map</b></font>&nbsp;<font color="#6a5acd">&lt;</font>
<font color="#6a5acd">F5</font><font color="#6a5acd">&gt;</font>
&nbsp;:setlocal spell spelllang=en_gb<br>
<br>
</font></body>

Next remove the enclosing body tags and then put it inside a table. I use something like this:
<table bgcolor="#eeeeee" border="1" cellpadding="20" cellspacing="0">
<tbody>
<tr>
<td valign="top">

<!-- The HTML Snippet (from above) goes here -->
<font face="monospace">
<font color="#0000ff">&quot; set the spellfile - folders must exist</font><br>
<font color="#804040"><b>set</b></font>&nbsp;
<font color="#a020f0">spellfile</font>=~/.vim/spellfile.add<br>
<br>
<font color="#0000ff">&quot;Map the F5 key to turn on spell-checking</font><br>
<font color="#804040"><b>map</b></font>&nbsp;<font color="#6a5acd">&lt;</font>
<font color="#6a5acd">F5</font><font color="#6a5acd">&gt;</font>
&nbsp;:setlocal spell spelllang=en_gb<br>
<br>
</font>


</td>
</tr>
</tbody>
</table>

Now the code is ready to publish.


Other Blog Posts In This Thread:

Copyright     2009, Gerry Patterson. All Rights Reserved.