Make Life Easier!

vincent wang's thoughts about ruby, vim, cli etc

Debugger(rdb, Gdb Etc) With Source View(vim) Using Tmux

I am a big CLI(Command Line Interface) fan, for example, I like vim for editing, mutt for email, irssi for irc, w3m for web surfing sometimes, mpd and ncmpcpp for music, mplayer for movie, lftp for ftp access etc. I am pretty happy with CLI tools, not only because it is efficient(considering you can almost automating all functionalities), but also that is real cross-platform interface for me. I can use these tools anywhere, local or remote, without knowing any difference. That is a big win for anyone who may need to work on several hosts at the same time. To make things more easier, I put all my config files to a github repository, so at the first hand, I can keep tweek all these configurations, on the other hand, whenever I start to work on a new environment, the first thing is checkout my config files, then I feel at home now(this is true for Linux, Mac OS X and even Windows by cygwin). Hmm, I think I wrote too much about CLI here, maybe I should write another post to describe my point of view about CLI later :)

To be honest, sometimes I am jealous of some GUI tools. I was a C++, Python developer before and now a Ruby develoer. I use vim to editing codes and I think vim is the most powerful editor(note, I use editor here, so Emacser, please don’t argue me since I don’t think Emacs is a editor at all); but when I need to fix some bug in my codes, I will use rdb that is a CLI ruby debugger, it inherits almost all the keyboard interface from gdb that is one reason I prefer rdb over pdb, the python debugger. So far, so good. but when the debugging session is long, then rdb is sometimes less efficient compared to other IDE tools like Netbeans or Eclips , because in rdb to get current source context I have to press ‘l’(the source code list key) to show codes around current running line, even so, the codes are not syntax highlighted as it is in vim and only a small section of codes are showed, that is tedious, right ? If the debugger can cooperate with vim, then that will be very useful.

After I kept asking google for this kind of tool for a long time, I only found clewn project that integrate gdb with vim, but rdb is not supported yet. I don’t want to wait anymore, I want hack now.

I like rdb’s gdb like interface, that is efficient. So I will not implement some wrapper above rdb. I want to use rdb interface to drive the debugging session but using vim to view related source codes and with the current line marked or highlighted, that’s all what I want.

Thanks to tmux that makes my dream possible! below is a screenshot how it looks like:

rdb and vim in action

When the current line changes on the left window, the vim buffer will get updated. It turned out to be very simple to accomplish it: just 80 lines of ruby code! Here’s how to do it:

  1. install required tools

    • tmux tmux is very good replacement for gnu screen. Before I switch to tmux, I had been a screen user for 5+ years. I had thought there would not be better similar tool than screen, but tmux proved I was wrong. try it, it definately deserved it. you need to install it after version 1.1
    • vim need to enable “sign” feature, run “vim –version” to check it
    • ruby in fact the small script mentioned later can be written in any language, but I happen to be a ruby developer now.
  2. config tmux create file .tmux.conf in your home directory, put line “bind C-d pipe-pane -o ‘~/.tmux/rdb’” into it. if you want a more advanced configuration, you can refer to my tmux config file.

  3. download this ruby script and save it as ~/.tmux/rdb

That’s it.

Start a tmux session, run you programm, when in debugging session, you can press “Control”+”b”, then “Control”+”d”, then a vim window will show up, after that, whenever current code line changes, the vim window will be updated. If you press “Control”+”b”, then “Control”+”d” again, the vim window will disappear.

At last, this solution should support other debuggers(gdb, pdb etc) without any change(or just change regex on line 76 ) of the ruby script.

Super Finder in Vim

There are many editors(Emacs, TextMate, UltraEdit, EditPlus etc) available, and more are coming. I believe every long-live editor must have some features that suit some people well, so they keep catching people’s eyes. I am a long-time(>5 years) vim user. Most of time, I am pretty happy with it. But I although keep an eye on other editors to see if it is possible to borrow some useful feature into vim, and build my vim configuration to be most productive for myself, a developer.

TextMate is becoming very hot these days, it’s very neat and powerful, just like vim ;)

One killer feature of TextMate, and borrowed by many other IDEs later, is very convenient finder tool for file, class etc. You can input part of the file’s name, and TextMate will provide a list of possible candicates for you to choose. The key point here is that, you don’t need to input the directory name, just part of the file name, and TextMate will collect all files starting with your input across all folders recursively under the project root directory. More details here

I found this feature is pretty useful and then started to think how to clone it within vim. After some experiments, I managed to do it!

Here is how it looks like:

vim super finder in action.

To make this feature happen in vim, you need following stuffs:

Fuzzyfinder.vim is a very powerful vim plugin, in fact, if I only can select one vim plugin that is must-have for me, I will choose this one. There are many features in fuzzyfinder, but largely, it will match user’s input against files, directors, buffers, tags in fuzzy way. ‘Fuzzy’ means if you input ‘abc’ in fuzzyfinder, fuzzyfinder will expand it to ’abc’ and provide candicates list that match the fuzzy pattern ’abc’.

But one problem of fuzzyfinder is: to find a file, for example, app/controllers/foo_controller.rb, you must input ‘app’, then ‘controllers’, before you get chance to find foo_controller.rb, that is not efficient compared to TextMate(in TextMate, you can input the file’s name directly).

Fuzzyfinder provide several modes: buffers, files, directories, bookmarks, tags etc. Tags mode make the TextMate clone possible. After you install it, run :help fuf.txt will give you a very comprehensive documentation.

Ctags is a tool that will parse files based on regular expression, and generate function defination to file path and regex mapping into a plain text file(named tags or TAGS). You can open the generate file and have a look, it is much easier that you think. Other edits can use the tags file to navigate from a place where a function is used, to funciton defination easily.

My solution is: treat file name as kind of tag, generate a tags file that contains the mapping between file name and file path, and be compatible with tags file’s format, then we can use fuzzyfinder to find the file easily.

Here is the bash script to generate tags file for files.

To use it, you need to run this script under your project root directory like this:

ftags.sh .

then a tag file named ftags will be generated under current directory. You need to tell vim about the new tags file, put the following line into your .vimrc:

:set tags+=ftags 

When you enter the tags mode of fuzzyfinder(by default, the shortcut is ) , file’s names show up together with other kind of tags, so you can search files, functions, classes etc in fuzzy way at the same time. That is pretty powerful, right ?

I have used the super fuzzy finder mode for several months and pretty pretty happy with the effect, it makes navigation within vim become a joy.

Now I am not jealousy at TextMate any more :)