Vim as KMail’s External Editor
Posted on In Linux, SoftwareVim is my favourite text editor and I also prefer to use Vim to compose Email. I ever used Vim as Thunderbird’s External editor with the help of plugin. I started to use KMail as my email client on KDE and I find it is not hard to configure KMail to use Vim as email editor with a little help from Konsole and shell.
KMail has a nice feature that can use external editor to compose email. In “Settings->Configure Kmail->Composor->General”, there is a option to “Use external editor instead of composer”, and we can specify the editor we like to use. The %f will be replaced with the file name of the email to edit which is copied to a temporary location.
It is possible to use KWrite as the editor and configure KWrite to use “Vi input mode”, but that is not convenient enough for geeks like me—I prefer the Vim in a shell/terminal!
Now, let’s see how I use Vim in the shell as KMail’s external editor.
A small script as the wrapper to call Vim
Vim is a command line tool and we can not directly run it. Instead, we should run Vim in a shell. In KDE, let’s just use Konsole. The external editor is actually a instance of Konsole with Vim as its command. Below is the script I wrote to invoke Konsole and Vim. Let’s call it kcallvim:
#!/bin/bash email=$1.eml cp $1 $email # --nofork is needed, otherwise the content is not updated # no other konsole instance running konsole --nofork --geometry 1000x600 -e vim $email cp $email $1 && rm -f $email
One tricky thing here is the “–nofork” option when invoking konsole. This option make the konsole command not return until Vim exits.
Put this script to a directory in the $PATH, or use the full path when invoking this script in Kmail.
Configure KMail to use kcallvim as the external editor
In KMail’s configuration tool, select the option of “Use external editor instead of composer”, and, in the “external editor” field, fill
kcallvim %f
as shown in the figure.
That’s it. When composing or editing email, hit Enter or any key, the Vim will start in a Konsole. Then you can edit the email in Vim as editing any other text files. After you save the email and exit Vim, the Konsole will close automatically and the email will appear in KMail.