• About

    Swaroop C H is 29 years of age. He is a coder and startupper. He has previously worked at Yahoo!, Adobe, his own startup and Infibeam.


    Read more about him


    Email: swaroop (at) swaroopch.com

  • Subscription

    If you want to know when new stories and articles appear on this website, you can receive them via:

  • I'm a Wannabe Hacker

    The Glider: A Universal Hacker Emblem

Archive for the ‘Vim’ Category

My Bash and Vim setups

Monday, December 6th, 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:

Bash prompt

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).

conqueshell.vim

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.

Great response to ‘A Byte of Vim’

Saturday, November 29th, 2008

It has been three days since I released my Vim book. I’m very happy with the response.

For starters, there has been 5003 PDF downloads, 14,715 unique visitors and 35,129 page views. That’s in just three days!

Second, I’m glad to see the kind of responses that I was hoping for:

[@raseel](http://twitter.com/raseel/status/1024291090) says “Great Book !! Although I use vim everyday as an editor as well as an ide, the book makes u realise how much more it can do.”
[@techpickles](http://twitter.com/techpickles/status/1025775542) says “have been thumbing through ‘byte of vim’. learning a ton even having used vim for years.”

I’m happy to see people discovering that Vim can do more, way more than people know about. There is a lot of power underneath the hood and it is a tragedy that it goes unnoticed even by long-time Vim users. Years ago, I started to wonder if I could change that situation and that’s when I started writing the book.

Regarding the responses via Twitter, it was interesting to see how fast the information was spreading. I could see retweets (linking to the book) being passed on from someone in Switzerland to someone in Ireland to someone in USA and so on, in quick succession.

Most of the traffic came from Reddit and Hacker News, so many thanks to those readers who submitted the news to these discussion sites. Seeing ‘A Byte of Vim’ as the top link on Hacker News for more than a day put me on a geek high.

Most of the feedback was from the vim.org mailing list where people were really happy to see the news. I’ve added some of these feedback to the What Readers Say section of the book’s front page.

There has been a lot of contributions to the wiki in terms of “bug fixes” i.e. typo corrections, grammar corrections and procedure corrections. It feels like a ton of editors are holding a magnifying glass to the book :)

One of the more exciting mails I received was from Yeh, Shih-You who wanted to start a Chinese translation! He said:

My name’s Yeh, Shih-You, a programmer and a Linux-lover from Taiwan. Having been using Vim for about 3 years, I found out that getting the most out of Vim suddenly becomes a necessity, in order to improve productivity as well as efficiency. Your book came out at the perfect time.

I’m interested in contributing translations in Traditional Chinese. Thanks for all the effort you’ve put into this book.

Now that is awesome.

On a side note, it’s amazing to note that even for my previous book, the number of mails from Chinese readers have increased dramatically in the past couple of years. So, in a way, I shouldn’t have been surprised that the Chinese translation was the first one to be started for the new book as well. The Chinese guys are already on the forefront of hardware manufacturing, but now seeing firsthand that they are so hungry to learn and devouring information online, it is hard not to imagine them at the forefront of software in a few years as well.

To sum it up, I’m happy with the response, although I would’ve been happier if a good number of print copies were sold, as well as more community contributions in terms of content such as new topics and chapters.

Many people are surprised and curious on why I choose to release my book under a Creative Commons license, I shall explain that in a different post later.

Announcing my free book on Vim

Tuesday, November 25th, 2008

Today is the first day of foss.in/2008, and on this occasion, I’m happy to announce the first public release of my Creative-Commons licensed book on the Vim 7 editor.

This book is meant for both beginners and advanced users.

For beginners, it walks you through the first steps to learning about modes, discusses about typing skills to be effective and moves on to the editing basics.

This book will definitely appeal more to people who are Vim users already because it helps add a huge number of tricks to their arsenal, whether it is more efficient editing, personal information management, coding your own plugins or making Vim a programmers’ editor.

I hope that fellow Vimmers will find these notes useful. Even though it is in a book format, the writing style is more like a tutorial and is informal, which should be familiar to readers of my Python book.

Both books are under the Creative Commons Attribution-ShareAlike 3.0 license, so you’re free to download it, email it, share it and improve it. In fact, the book is on a wiki, so you can just click on ‘Edit’ in the left sidebar of any chapter to improve the book in a matter of seconds. When in doubt, please use the ‘Discussion’ link to add your suggestions and comments.

For those who prefer reading books they can hold in their hand, please consider purchasing a printed copy of the book. This will also help support the continued development of the book.

For those PHP gurus familiar with GeSHi syntax highlighting, I would greatly appreciate any help in improving my vim syntax highlighting source, especially in handling Vim-style comments, etc. Please mail me if you can help.

This book has been in the works for several years, so I’m glad to see it finally in good enough shape for releasing it. Although I haven’t done as many rewrites as I would have been satisfied with, I decided it was better to <insert cliché of “Release Early, Release Often.”>

I dedicate this release to foss.in and GTD principles.

Effective Vim

Friday, November 16th, 2007

Being a fan of Steve Yegge, I was randomly reading some of his older writings and eventually chanced upon his post on “Effective Emacs”. Being a Vim guy, I wondered whether some of the tips he presents are useful for the Vim world as well.

Note: This is not a Vim vs Emacs thing, it is simply a porting of tips for Emacs to see whether the tips are useful for Vim users as well.

So here goes:

10 Specific Ways to Improve Your Productivity With Emacs, ported to Vim:

Item 1: Swap Caps-Lock and Control

This is a desktop-specific customization. However, I don’t think it is required for Vim users, but it can be useful.

Update: After using for a couple of days, I’m really starting to like this!

Item 2: Invoke M-x without the Alt key

Not relevant for Vim. Not a good start, first two tips are out…

Item 3: Prefer backward-kill-word over Backspace

This is a good tip. Normally, I would use bdw to achieve the same. To map backspace to this command in normal mode, put this in your vimrc: :map <bs> bdw.

To make it work in insert mode you can put :imap <bs> <esc>bdwa. I’m sure there’s a better way to use just one command to do this, please leave a comment if you know of a better way.

Update: You can also use ctrl-w in insert mode (see :help i_CTRL-W), thanks to pimaniac.

Item 4: Use incremental search for Navigation

Use :set incsearch.
Press n to search forward and N to search backward.

Item 5: Use Temp Buffers

Run :new to get a new buffer (or alternately ctrl-w n).
To switch between buffers, use ctrl-w ctrl-w (yes, twice).
Use :q as usual to close the buffer (or alternately, ctrl-w q).

Item 6: Master the buffer and window commands

  • To split window vertically, run :vsp
  • To split window horizontally, run :sp
  • To make all visible windows approximately equal height, run ctrl-w =
  • To switch to other window, run ctrl-w ctrl-w or use the directional keys ctrl-w h/j/k/l
  • To delete other windows, use ctrl-w o or run :o nly
  • To list-buffers, run :ls (or even :files or :buffers)
  • Dialog Boxes: The Root of All Evil – agree, Vim doesn’t need dialog boxes as well (at least in the non-gui mode)
  • Buffers to the Rescue – Same thing for Vim, I think.

Item 7: Lose the UI

  • Remove the menubar using :set guioptions-=m.
  • Remove the toolbar using :set guioptions-=T.
  • Similar options exist for the scrollbar, see :help guioptions
  • Region selection can be easier in Vim using the visual mode, just press v, use the normal keys to move around, such as 10j to move down by 10 lines, and then a command to work on that visual selection, such as d to delete it.

Item 8: Learn the most important help functions

The help in Vim is vast, see :help usr_toc to see the chapters of the awesome reference manual.

Item 9: Master Emacs’s regular expressions

I agree, Friedl’s book is the authority on this. However, there are some good introductions to Vim regular expressions available.

Item 10: Master the fine-grained text manipulation commands

  • Creating macros are easy in Vim. Press qa to start recording a macro called ‘a’, do all the commands you want to run, pres q to stop recording. Then, run @a to repeat the recorded commands i.e. a macro.
  • Swapping two adjacent words, yeah, this can be better. I use xp to swap characters and dwwP to swap words, but it doesn’t do fancy stuff like the transpose-* functions. This can be an interesting plugin to write.

Tune in next time…

  1. Filling paragraphs can be done by setting :set textwidth=80 and running gqap command to format ‘a’ ‘p’aragraph, or like me you can map the ‘Q’ key to run it : :nmap Q gwap. To make this work inside comments, make sure you :set formatoptions+=c.
  2. gnuserv : I use It’s All Text! Firefox extension.
  3. Dired : There are plugins available with similar functionality
  4. Whitespace manipulation – plenty of ways such as :set expandtab, :retab!, :help fo-table, etc.
  5. nxml-mode : I haven’t used nxml-mode but I’m still looking for something like Emacs’ SGML-mode that works for Vim. I miss you, SGML-mode.
  6. picture-mode : Dr. Chip to the rescue with DrawIt.vim
  7. minibuffer management : Not sure what this is.
  8. effortless navigation : I think Vim has enough keys for this by default. See :help navigation.
  9. region management : We can always choose the color scheme of choice for the highlighted region, or change it ourselves, see `:help
    :highlight`.
    
  10. rectangle commands : Use ctrl-v
  11. emacs shells : We have :sh but don’t know if Emacs does something more
  12. align-regexp : Not sure what this is.
  13. frame initialization : I set Vim to always opens in full screen, see :help win16-maximized. Not sure how to do it in Linux yet, but in Gnome, I just press Alt-F10.
  14. using the goal column : No idea…
  15. setting the fill column : Nada…
  16. OS settings and font : I like to customize Vim’s font and keep trying different fonts, currently I’m using :set guifont=Consolas:h14:cANSI
  17. browsing and editing archives : I think Vim does this by default, see :help netrw.
  18. advanced keybinding : see :help :map and :help keycodes
  19. mastering the kill ring : I guess you can simulate this with :echo @a, etc.
  20. mastering Info : Not sure if this would be useful in Vim.
  21. using M-x customize : Not sure what this does.
  22. utility apps : It’s all in the plugins.

Summary: Porting good ideas is a good idea :)



I wonder why a search for Steve Yegge on Wikipedia points to Batman



Update in November end, 2008: I have released a new book on Vim, read the whole thing right here..

Lookup

Saturday, August 25th, 2007

Most of my writing inside the browser is done with the combination of the It’s All Text extension and Vim. During this writing, I use words whose meaning I know vaguely but don’t know the exact usage, and in such cases, I usually open a new browser window and look up the meaning of the word in an online dictionary. This process of looking up the dictionary meaning was getting rather tedious, so I wrote a small Vim plugin today to do this job with a simple command.

To use the plugin, first install BeautifulSoup. Then, get my lookup.vim script and copy it to ~/.vim/plugin/.. See the new version’s installation instructions, it now uses John Goerzen’s dict client implementation in Python.

Next time you use Vim, just place your cursor over any word and run :Lookup, and you’ll see something like this:

lookup.vim screenshot

Since, this is the only command starting with L on my Vim installation, I just end up running :L. Of course, you can always create your own keyboard shortcuts to make it easier.

Update: Based on the comments, I’ve updated the script to now use the DICT protocol and talks to some servers. This avoids screen-scraping, is much faster to use, allows me to now use both a dictionary and a thesaurus which is very very handy because I can see what other words I can use, and of course, doesn’t violate any TOS (which screen-scraping could amount to).

How to use Vim with Firefox

Wednesday, April 26th, 2006

If you’re a Vim user like me, you would’ve wished many a time that you could use Vim to edit text in Firefox. Too bad, the Mozex extension doesn’t work anymore.

Thanks to Samuel Wright, I have now discovered another way to achieve the same – using the ViewSourceWith extension.

I have put up the set of steps on my wiki on how to use Vim to edit text in Firefox. The wiki page is no longer available. Please refer the Official ViewSourceWith FAQ instead.

Note : This works only for plain text entries. It will not work for rich text formatting options such as GMail or Yahoo! Mail compose page.

GVim

Friday, March 24th, 2006

A message by Bram on vim-announce:

The past few years many people have sponsored my work on Vim. Now version 7 is nearing completion, beta testing will start soon, very soon. A big thanks to all who supported me! I would not have been able to do this without your help.

But things are going to change. I have accepted a job offer and will go back to a full time job in a few months. To avoid speculation and rumours: I am going to work for Google in Zurich. Fortunately I can spend part of my time on Vim. But it will obviously be less than the past few years when I was working 150% of my time on Vim.

I will no longer need sponsorship to survive. Therefore, starting the end of March, all money given for Vim sponsorship and registration will go to the project in Kibaale, Uganda. And no, this is not an April fools joke.

The voting will continue as before. And seeing people give money will motivate me to keep working on Vim. The children need the help much more than me anyway. Thus please keep sending money!

P.S. Yes, I have donated to Vim before.

Connect the text

Monday, May 9th, 2005

A while ago I was thinking about using a wiki to organize my notes, but I don’t know if even that scenario would be useful for quick note taking because of the conversion from wiki syntax to HTML everytime which causes a lag when saving the notes.

I have found a simple and effective solution (next to paper and pen, which I would always prefer) by using VIM with the Universal Text Linking plugin and the Markdown syntax file.

As the name indicates, the Universal Text Linking (or UTL for short) plugin allows you to create URLs that you can access like real hyperlinks (within VIM) and even opens a browser or media player depending on the type of file you link to.

I like using Markdown syntax because I find it to be the most cleanest and simplest of semantic text markup styles that I have used. So, the Markdown syntax highlighting makes the text even more pleasant to read.

Let me give an example on how I created a plain text wiki:

![13103132][Medium][]

I have one central file which acts like an index to the other documents and this is the first file I always open. I also put in any critically urgent, etc. stuff in this file, so I’m always reminded of it.

I have unimaginatively named this file as ‘plan’ and here are the contents of that file:

  • [ProjectA]
  • [MeetOnSaturday]

From here, I just press \gu on [ProjectA] and it takes me to that file. So, I read or make changes to the notes on my top-secret project and then I just hit Ctrl-O and I’m back to my ‘plan’ file which I started with.

If you run set filetype=mkd for the file, you will get Markdown syntax highlighting. Better yet, just put a modeline such as # vim: filetype=mkd as the last line of the file and VIM will automatically do this for you everytime.

In effect, you have a wiki using just plain text!

I think I must be catching the Wikiphilia.

Sidebar: I have let mapleader="," in my vimrc file, so I press ,gu to open links. I find the comma easier to type than the backslash.

Editing Away

Sunday, March 14th, 2004

A text editor is one of the most important tools in the kit of every programmer. It is the starting point for us when we intend to write programs or anything else. It is therefore important to understand the strengths and weaknesses of our editor as well as being able to utilise the editor to its fullest potential. One such good editor is VIM. Bram Moolenaar, the creator of VIM, discusses the Seven habits of effective text editing and uses VIM as an example of showing how to do things.