Thursday, January 24, 2013

Command line and Gnome Desktop integration tips for Vim

Here are a couple of ideas that I find handy when working with Vim.The first tip is how to check what the last search term was in Vim from the command line. A silly idea that I've found more useful than I thought it would be.

For this to work, you need set nocompatible so that Vim writes out its state information to a viminfo file on exit. The line in the viminfo file that stores the last search looks like this:

 ~Msle0~/search_term

The m, s, l and e characters describe which magic, smartcase, line/char offset, or from the end or start flags Vim had set when you searched. For example, the third character is an 's' or an 'S' depending on whether smart case is on (S) or off (s).

Here is a script, which I call last_vim_search, that shows you what the last search term stored in the viminfo file was:

#!/bin/bash
lvs=$(awk -F/ '/^~[Mm][Ss][Ll][eE]0~\// { print $2; }' ~/.viminfo)
if test $# -eq 0 ; then
    echo $lvs
    exit
fi
grep "$lvs" "$@"

The snippet of awk searches for the matching pattern, splits on the "/" character, and spits out the last used pattern.
$ last_vim_search
\<pattern\>

As an added bonus, if I pass arguments to the script then it searches whatever arguments I passed in using grep.
$ last_vim_search *c
file.c:123: ....
file.c:456: ....

The second tip is how to integrate Vim with the "Recent Documents" feature from the Gnome Desktop. Frequently I find myself editing a text file in Vim, and then having to attach it to an email or upload it to a web form. Having the file you want to upload in the recent documents list helps a lot in these situations, as it is quicker to get to than having to navigate the file chooser to the correct location and selecting the file there. Unfortunately only "Gnome-ish" applications update the recent documents list, and terminal Vim is not one of these. This tip fixes that situation, and lets Vim update the recent list too.

For this to work, you will need the python-gtk bindings. Debian and Ubuntu at least installs python-gtk by default with Gnome. Also your version of Vim needs to have Python support compiled in. If not, you could adapt the python snippet to run in its own script.

"
" add text files that I edit to Gnome's recent file list
"

functions:RecentAddFile()
    python <<EOF
import vim
import gtk
n = vim.current.buffer.name
f = 'file://' + n
gtk.recent_manager_get_default().add_full(f,
    {'mime_type''text/plain''app_name''vim''app_exec':'vim'})
EOF
endfunc

autocmd BufReadPost *.txt call <SID>RecentAddFile()

This makes use of the "recent documents" GTK API. It's only short and doesn't really need much explanation. The python <<EOF marks the start of the embedded snippet of python code, the rest is all vimscript. The autocmd BufReadPost bit means that when Vim reads in a txt file it will call the function to add the file to the recent list.



The python code uses Vim's own API to get the current file name, which it turns into a URL by prepending "file://". The gtk call adds the URL to the recent document list. There's a simpler "add_item" method too that doesn't need the options passed in via the dictionary argument, but it doesn't seem to work in this context.

You could tweak the code to only add files in your home directory - as is it will add help pages as recent docs too. With different autocmd lines you could also add other file types to the recent history.

And now for something completely different... check out this great Super Meat Boy plushy that my better half made for eldest son. That's love :-)



PS. This weekend I'm hoping to have time to put the final touches on a new android-ndk-profiler release. The current beta-ish version seems to be working OK if you want to try it out.

No comments:

Post a comment

Note: only a member of this blog may post a comment.