Monday, September 19, 2011

Vim configurations: Part 2

Continuing from my last post on vim configurations, in this post I will discuss some vim plugins that I have written, along with couple of third-party plugins which I find very useful. If you already haven't installed the full vim (with gui support), then do it. For ubuntu the package is called vim-gtk. Also if you don't have the following directory structure, create them before we start.
mkdir -p ~/.vim/plugin
mkdir -p ~/.vim/ftplugin

Scripts I have written

There are a couple of vim scripts that I have written over the years. I will discuss two of them now.

CommentLines

This script was written to (un)comment multiple lines simultaneously. It was inspired by vim tip #271 from the vim tips website, now appropiately called the vim tips wiki. You can download the plugin from here.
After downloading the file, rename it CommentLines.vim and copy the file to $HOME/.vim/plugin/. Now open any programming file (c, perl, tex, python, html, shellscript etc) in gvim. To comment lines select multiple lines and press ctrl+k. To uncomment select multiple commented out lines and press ctrl+l.
The script already has support for various types of programming language as mentioned. Support for other languages can be easily added.

ColorComment

Commented out text in gvim by default appears as blue, which IMHO grabs too much attention. So I wrote the ColorComment plugin. Grab it from here. After downloading it rename and copy it in the $HOME/.vim/plugin/ directory. Now if you open any programming file the commented out text appears in gray color. If you want to (un)highlight your comments, press Esc and then press ctrl+g. Ctrl+g is a toggle switch via which you can toggle between highlighted and un-highlighted comments.

Other useful scripts

There are couple of other scripts, written by other vim users, which I not only find very useful, but also use them on a daily basis.

editexisting

Ever tried to re-open the same file twice in gvim and seen this message?

What gvim should should have done is jump to the already opened file. With gvim version 7 one can do just that with the plugin ``editexisting.vim". Copy the script ``editexisting.vim" to your local plugin directory.
cp /usr/share/vim/addons/plugin/editexisting.vim "$HOME"/.vim/plugin/
Now if you try to re-open a file which is already open in gvim, that particular gvim window will come into focus without showing you the above message. This however only works with gvim, but not console based vim.

snipMate

Often while programming or say writing in latex we reuse the same commands. If we could write these commands via a few keyboard shortcuts then, we could type a bit faster. This is possible via the plugin snipMate. Download the snipMate plugin and follow the instructions given in the website to install it. This plugin has support for lot of programming languages. Now say we want to add support for latex. Open the file $HOME/.vim/snippets/tex.snippets. This file already has a couple of snippets, but adding custom code snippets is quite easy. An example will make this clear. Let' say we type the following code in the tex.snippets file.
# Begin equation
snippet beq
 %
 \begin{equation}
 ${1}
 \end{equation}
 %
Now open any tex file and type ``beq" and press ``tab". ``beq" will get expanded to
%
\begin{equation}

\end{equation}
%
with the cursor blinking in-between \begin{equation} and \end{equation}. This is but a small example of what you can do with snipMate. Use the command
:help snipmate
in gvim to learn more.

LatexParFormat

Before I started using this script, I used to often find myself trying to manually resize paragraphs in latex after adding some text in-between paragraphs, so that the tex file would look nicely formatted. Meaning the lines would break off at column 72. Now with LatexParFormat, I can do just that with a shortcut key.
Follow the install instructions in the above website. Open any tex file in gvim and scroll to any paragraph. Press ``Esc" and then ``ctrl+j". The paragraph becomes nicely formatted.

That's all for now. If I write any more vim plugins or find other useful scripts, then I will definitely share them with you.

 In my next post I plan to talk about synctex using GVim and Okular. If you are wondering what synctex is all about, then a video in this blog post makes it clear.

No comments:

Post a Comment