Swaroop C H

blog books about contact subscribe

My Bash and Vim setups

06 Dec 2010

I find it surprising on how little time coders spend on their development environment (the "dev env"). And especially, I find it amusing that I can find, refactor and test code way faster than the Netbeans IDE users in my office, with just a shell and Vim setup.

So why is a good dev env necessary? Because we do searching, refactoring, editing and updating of code much more than appending fresh code, and this also applies to code that you wrote half an hour ago, because you will want to quickly refactor it when a new constraint, a new requirement, a new design or a new idea comes to your mind.

I learned this lesson while I was writing my Vim book. Since then, I have been investing quite a bit of time on my vimrc file, heavily customized to my liking.

To give one very quick example - I like the cursor to always be at the middle of the window (as opposed to at the bottom of the screen when you're scrolling down), so that I can see the lines of code before and after the current line. To achieve this, you simply set scrolloff=999 and you're done. A one-line setting, but it makes a world of difference in usage.

Extrapolate this to dozens of customizations and you have just optimized your environment for lesser time at the keyboard, lesser time fighting the editor, and more time on the actual code. You do not want to break your flow of thought because you're unable to quickly switch between the right files (say, between the controller and the view files), and so on. [1]

These customizations are stored in .vim and .bashrc files, collectively referred to as "dotfiles". I have been asked quite a few times by readers of my book to share my dotfiles, but I was not comfortable to share it because I felt it was too hacked up and did not have a good "base".

So when I came across bash-it and vim-addon-manager, I knew they were good foundations and a good excuse for me to overhaul my bash and vim setups.

And lo, behold, my dotbash and dotvim repos (on GitHub).

These are my actual working environments at office and on my personal laptop, so if you don't agree with some of my defaults, fork away.

Now, on to what is interesting about my setup...

What bash-it provides

bash-it provides great defaults and aliases, right from .. as a shortcut for cd .. and ... for cd ../.. to bash completion setups for git, rake, etc., and of course, a gorgeous theme to use:

dotbash

The best part though is it's neat organization into aliases, completion, custom, lib, plugins, themes and template folders. That makes a big difference in the long run, for the same reasons why a cleanly modularized codebase is better than one giant script.

And it will get various new features over time contributed by the community, example, completion of server names for ssh.

What vim-addon-manager provides

vim-addon-manager provides a super-simple way to install plugins into separate folders and then use them all, instead of lumping all of them together into a .vim folder. Just add the name of a new Vim plugin to the list of plugins you want to load, and it will automatically fetch the plugin and install it for you! This makes it easy to play around with new plugins as well as a simple way of having the latest version of the Vim plugins.

I use vim-addon-manager, but there are alternatives - others prefer pathogen.vim and there is also Vimana which gives you an apt-get-like command to search for Vim plugins. Choose your weapon.

There are caveats to vim-addon-manager, mainly that all plugins don't seamlessly work with it. For example, I couldn't get pyflakes.vim to work with it, so I had to unzip pyflakes into my regular .vim directory to use it.

My Bash customizations

Ever since I saw gemedit, I wanted something like that for Python eggs, and I realized that it was easy because Python modules have a __file__ attribute that I can use, and I created my own egg_edit command to do the same trick. Similarly, I have my own sync command with the settings that I prefer, etc.

tmux setup

tmux is a modern alternative to GNU Screen which is a common arsenal in any Unix coder's war-chest. I prefer to use tmux because it makes it easy to script sessions which makes it super-easy to start a new session working on a project. For example:

Put this as flask-boilerplate-tmux.bash into the custom folder of your bash-it/dotbash folder, and then you can run flask-boilerplate-tmux any time to start a new session to work on the flask-boilerplate codebase! ( Update : Check out Teamocil which will allow you to do the same with a simple yaml config file )

I have a highly customized tmux configuration which sets many good default settings, especially starting the numbering of the windows from 1 (switching between 0 and 1 is painfully because they are at the opposite ends of the keyboard).

autojump

A good Bash plugin to use is autojump which monitors which directories you spend most of your time in, and then makes it easy to jump to your most favorite directories using a simple j command.

Don't forget to use jumpstat to see which are those directories. Also, bash-it/dotbash provides a command called rh that does something similar.

Note that I have started using autojump only recently, so I don't have as much experience using it as the other tools.

My Vim customizations

I have a fairly customized vimrc, right from changing the status line to providing shortcuts like :A to copy the full buffer to the clipboard and :B for vice-versa, which I find it very useful in situations such as I am writing this article in Vim and can quickly copy/paste into Wordpress when I am done with the draft. I also have a o shortcut to open a URL that the cursor is on, and many other goodies.

Vim plugins I use

There are three Vim plugins which are must-have for me:

First is command-t.vim which is a "fuzzy finder" to quickly jump to another file under the current directory.

command-t.vim

Second is ack.vim which allows you to intelligently grep your source code for patterns and then jump to each occurrence in a split-window fashion.

My favorite feature is that I can search my code with :Ack --python or :Ack --ruby and it will ignore all the JavaScript libraries which can have the same variable or function names.

ack.vim

Third is conque shell which allows you to to create a split window which can be any interpreter prompt.

The best part is that you can use a shortcut e (assuming the default mapleader) to send the visually selected text to the interpreter prompt as if it was copy-pasted and it will execute it! This is incredibly helpful when you want to iterate the development of a multi-line function or fragment of code and you find extracting that code to a separate file and editing that file to be a pain (The interpreter prompts are designed for playing around with one-liners, they are simply not built for multi-line code).

conque

I have the following lines in my vimrc to quickly create ConqueShell sessions:


command Shell :set nolist | ConqueTermSplit bash
command PythonShell :set nolist | ConqueTermSplit python

So, I quickly run PythonShell to create a new session.

Setups by others

For other Vim setups, see the Janus repo on GitHub which is used by Yehuda Katz himself. You'll find many more on the dotfiles website.

Summary

I'm hoping this article will help Vimmers and Bash users to broaden their usage and help them be more productive, the same intention behind my Vim book:

"I used to play with vim for years. I think this book would have to save me much time if it was written 10 years ago! Anyways, thank you for this amazing work you are doing."

-- turky_samy (at) hotmail (dot) fr

If you like what you have read so far, then go ahead and install dotbash and install dotvim.

Bottom line:

"Give me six hours to chop down a tree and I will spend the first four sharpening the axe."

-- Abraham Lincoln



[1] The flip side though is that I have been told "You're the most mysterious guy in this office. No one understands what you're doing when we see you coding." Heh. Hopefully, it is not as mysterious after this article.

Comments

Anirudh says:

I'm surprised you don't use zsh actually. I found that zsh and it's capabilities (especially with the completion and globbing, etc) offer much more control. On second thoughts, maybe I never really spent that much time with bash to get it done. There's a lovely little project called "oh my zsh" if you're interested.

Also, how do you like vim-addon-manager. I found it kindof flaky and have found true joy with pathogen.vim In fact, a week ago, I tore apart my entire .vim and re-built it from scratch. There's also a great new github account (github.com/vim-scripts) and a companion site (vim-scripts.org) (I won't link to them lest I get caught by spam filters).

This way, you can just create a bundle/ folder and checkout as many addins as you want directly from git (regardless of whether or not they're hosted in github in the first place). You can even write a script to do a git-fetch for all the addins in a cron job and tell you whether there are any updates available.

P.S. My .vim is at http://github.com/ninjagod/etc/vim

Bill Mill says:

1) Why not use ipython instead of python in your PythonShell command? If you don't use ipython yet, you should start now, it's life-changing. First thing I install on any python setup.

2) I love these shortcuts for navigating splits:


map j
map k
map l
map h
map <C->


3) insert pdb with ctrl-d for easy debugging:


imap import pdb; pdb.set_trace()

Bill Mill says:

Well, sorry, your comment form ate all my <'s because I didn't escape them

Swaroop says:

@Anirudh I have somewhat control over installing Vim on remote servers, but it's a pain to expect zsh on remote servers, plus many a time I would not be allowed to install it, so I'll be sticking with bash. Plus, I never felt something was missing for me to try something else. Btw, I had read that the bash-it project was inspired by oh-my-zsh :)

pathogen is good, but installing new plugins is still manual, I cannot say "go download and install this plugin for me" which is what vim-addon-manager or Vimana provides. I had not seen vim-scripts.org, that changes the scene for me, and I might swing over to pathogen because of that :) Thanks for that!

@Bill I prefer bpython instead of ipython. Regardless, using those inside the Vim buffer does not work well, so I prefer the simple python interpreter prompt within the conqueshell. In normal usage, I use bpython.

Linux says:

Hey! I love and use gVim for almost everything related to text editing. But the problem I do text edition but not English but Hindi, Marathi and Sanskrit so I am compelled to use gEdit. I am not a developer myself, so I did some google search and found out that vim indeed does support complex text (indian scripts and arabic). Many programmer have with native arabic knowledge have contributed well to make vim support arabic but there is no one to do for Indian languages. I see you are a developer why don't you try making vim for Indian Languages ? many would love and thank you.

alex says:

Smth to start with: sharpening an axe can be a pain. If you ever find that you don't have tools that you used to, you get problems. An example can be moving to different machine, to say nothing about OS. It's much simpler with linux due the way it keeps user settings (and github for dotfiles), but stuck on windows.

Interesting post. Enjoying your blog and your books, really.

Had no luck with bash-it under cygwin - it's terribly slow for whatever reasons, but it's all b/c of novice approach to it. Missing even very basic knowledge of bash/*nix like environment to get it right. :-(

Yoav Aner says:

Nice writeup. Need to try some of those tricks. I noticed however that you mention that you copy&paste between Vim and Wordpress. Have you tried the vimrepress plugin? http://www.vim.org/scripts/script.php?script_id=3510

I now only write posts using it. You just use vim, write stuff using simple markdown, and with a few commands can publish it (or save as draft). No more copy&pasting and no more need to write HTML tags either! It even keeps the markdown format for you, so if you ever go back to edit a post, you still have the markdown stored and ready for edits. You should definitely give this one a spin.

Swaroop says:

Hey Yoav,

Thanks for the suggestion, I didn't know about vimrepress! My workflow has changed to write in pure HTML these days, so I don't know if I'll be using that plugin, I prefer to type in the browser anyway because of the media that gets added (images, video, links, etc.)

Feedback

There's no comment box, but please do email me or tweet me your thoughts and criticisms, and I will publish the relevant ones here.