Tag Archives: Bash

Fun With Vim

I freaking love Vim. And yes, I’ve used Emacs in the past (mostly as the recommended editor for a class I took last year – did that for a few days and then switched back to Vim while in terminal or opened up the SSH directory locally and used Gedit. (Also, Gedit is almost certainly the best and most powerful built-in GUI editor you will ever come across. With plugin support, it’s incredibly extensible. It was my favorite editor for a very, very long time too.)

Obviously, Vim’s power comes in its insane customization options. Basically every conceivable thing you’d ever want to do on a text file can be done with a collection of cryptic characters which become not-so-cryptic as you begin to understand the language of Vim. Seriously, hacking and playing with this editor is also as fun as actually using it to code.

But of course, as with all good things in the world, there are some annoyances. Not so much in Vim as in Ubuntu. Hey I love Ubuntu and use it almost exclusively, but it’s kinda boring that it’s always so late to get updates into the main packages. Of course, that’s the reason behind PPAs in the first place, but still.. Anyway, as you may have heard, Vim 7.3 was finally released a few weeks back and of course I wanted to play with it. Basically the thing I wanted most was CursorColumn, which introduced a vertical column at whatever character width(s) you wanted. This is really useful at times when you have code formatting rules such as an 80-character limit on lines. RelativeNumber, which displays how many lines away other lines are from your current line instead of a fixed numbering system starting at the beginning of the file, is also a cool feature sometimes on certain motions (e.g.: copy next N lines – which was a pain before). And file encryption was the last big feature in 7.3, but that’s something I’ll likely never use (at least from within Vim).

So I decided I couldn’t wait until Ubuntu brought 7.3 into it’s main packages (apparently it’s not happening until 11.04 😦 ), so I figured I’d compile it myself. This I merrily did and modified /usr/local/bin/vim to point to the newly compiled executable. And this worked great for a while.. until I realized I was rather naive when I did that and didn’t include all the nice flags that would have enabled such things as Python support in use with certain plugins. So I tried to include stuff that I wanted and recompiled except I kept realizing there was more stuff I wanted. This went on for a while until one day I discovered a fully functional PPA for 7.3 😀 (note, I really wanted the latest release on the day it was announced, so there weren’t any PPA or straight up debs that I could use on the first day that I came across). Win! For the record, here’s the PPA in question. Okay so after finding Software Sources in Ubuntu 10.10 (wow, in Software Center until Edit and inaccessible through menus? grr..), I updated and languished in Vim glory. Very gratifying indeed.

In any case, I just got that done and now I am happy. Also, the past few days of looking through Vim help and learning random stuff from the Internet (did you know that “K” over a standard C function will open up the man-page for it?!), I arrived at my .vimrc file below:

"""  Basic Configurations

set autochdir
set autoindent
set autowrite
set bs=2
set colorcolumn=80
set cursorline
set expandtab
set foldmethod=syntax
set foldminlines=3
set hlsearch
set laststatus=2
set mouse=a
set nocompatible
set number
set ruler
set shiftwidth=4
set showcmd
set showfulltag
set showmode
set smartindent
set tabstop=4
set wildmenu
set wildmode=longest,full
set wrap
set whichwrap+=h,l,<,>

""" Options

filetype on
syn on
au FocusLost * :wa

""" Python options
autocmd BufRead *.py cinwords=if,elif,else,for,while,with,try,except,finally,def,class

""" Hard tabs in Makefiles
autocmd FileType make setlocal noexpandtab

""" Theme options

set background=dark
set t_Co=256
colors mustang

""" Misc mappings and configs

cmap w!! %!sudo tee > /dev/null %
imap <c-c> <Esc>
imap <F1> <Esc>
imap jj <Esc>
nmap <leader> <space>
nnoremap <silent> <C-Left> :tabprevious<CR>
nnoremap <silent> <C-Right> :tabnext<CR>
nnoremap <silent> <C-t> :tabnew<CR>
nnoremap ' `
vmap < <gv
vmap > >gv

Some notes here:

  • Note that the first several lines under Basic Configuration are sorted alphabetically. There’s really no reason whatsoever for this except that I was far too amused that I could do this automatically. Basically you just do :set rnu for relative numbers. Then point your cursor to the first line, do N Shift-V, where N-1 is how many lines you want to select (after getting into Linewise Visual Mode), then :!sort which takes in the selected lines as input, uses the :! to execute shell commands, and of course the shell program sort to actually do the sort. I also realized this is possible by issuing N!!sort where N-1 is again how many lines I want to sort. The !! feeds in selected lines into the shell program and then the result is read back. Oh yeah and you can also do this with N Shift-V v_!sort. Basically, the point is that Vim is powerful.
  • Be sure to do set mouse=a if you want to enjoy the ability to switch tabs using a freaking mouse inside the terminal. Cool, though I rarely use it. Note that if you do this, it will override the built-in Copy/Paste functionality and I can’t seem to figure out how to directly copy text from within the terminal to the OS (or application, if on Ubuntu) copy buffer.
  • The options for set wildmenu and set wildmode=longest,full are the coolest things ever. So basically when you are entering commands, it will suggest completions on and then will iterate through the completions with each subsequent . Also, reverses through the list. This mimics the behavior of standard Bash’s completions and is thus extremely intuitive for me.
  • Another cool command is au FocusLost * :wa. As described in the Vim wiki, this basically will Write All files when the focus is lost. Very useful in my opinion.
  • The option set t_Co=256 enables colorful magic on XTerm terminals (and others). So instead of the usual 8 colors for syntax highlighting, you now get 256. Color-schemes look better, roses smell fresher, etc.
  • The line cmap w!! %!sudo tee > /dev/null % is an interesting hack that allows you to write files for which you don’t have permission when opened. It’s a pain to open a file, edit it, and then realize you don’t have permissions to write it. Sloppy approaches include writing the file to a temporary location and then copying back with permission, but that’s not nice. (One pre-emptive indicator would be to have a [readonly] flag in the statusline.) A solution is the above hack, which allows you to gain temporary super-user access and then write the file to it. Handy. This is a pretty popular hack found all over the Internet, so I’m not sure who to credit it too. I think the first place I saw it was on Command-Fu.
  • Yeah, so map jj to Esc. Just do it. You’ll see how much time you save and it will shock you incredibly causing unbounded, unexplainable glee. YMMV.
  • Finally, the last two lines which map < and > to <gv and >gv. This restores your selection after indenting or un-indenting. In my opinion, this should be the default behavior.
  • Finally, my Color Scheme is Mustang, slightly modified. I may post that up later. It’s an extremely awesome theme and my second most favorite one is Vitamin, also from the same guy. Both very cool.

I think I’ll just leave a screenshot of my Vim configuration as a final note:


Posted by on November 2, 2010 in Uncategorized


Tags: , , , , , ,

Quick Update on Summer

I’ll write more later on but I just wanted to give a quick update on how my summer has been going. So I’ve started my research at Princeton about three and a half weeks ago and so far it’s going pretty well. Mostly it’s a lot of data analysis work and a fair bit of programming. That’s a very good thing. Honestly the more I think about it the more I realize that programming is what I love more than anything else. I’m effectively thinking about programming all the time anyway, always trying to look at the world through the lens of computer science (of what little I know of it) and so to have the opportunity to actually do it for the summer is quite cool. I’m working on servers running a variety of 64-bit Linux flavors, most of which have at least four cores and 8 gigs of RAM. The computational capacity of them is really amazing and the ability to run in parallel at acceptable speeds makes life easier. I’m extremely happy with the tools I’m using – straight manual Makefiles, gcc, gdb, and emacs/vim. As far as code is concerned, it’s mostly C, some Java, and very often Bash scripts with occasional AWK or Perl for quick things. For some reason I’ve found this setup more easy to work with than an actual high-powered IDE. Weird, perhaps, but I sure hope wherever I work in the future I will be afforded this same luxury.

The more I work with Linux, the more advantages I see. I was comfortable with Bash coming in this summer and I’ve learned even more after working exclusively with it. Linux feels like it was designed solely for the purpose of making a programmer’s easier and its done a great job at that. I’ve learned a lot more of Bash scripting now and I’m always amazed at how powerful it is. It’s an extremely useful language to know. For source control, I pretty much had the choice of deciding what I wanted to use and I chose SVN although I think I should’ve perhaps gone with Git. It’s been many months since I’ve used SVN but it was very easy to catch on again. It also forces me to be organized with my code and be more standardized so I’m not going to complain :).

For collaboration, I suggested we use Google Wave to keep a running to-do list and so far it’s working beautifully. It really is the best way to keep track of ideas (although I still maintain an ongoing to-do list on the whiteboard and on my Google Tasks because I’m weird like that). Checking off completed items is pretty damn thrilling – I love the feeling of making progress.

Apart from working on my current research, I’m also doing some light QA/Testing for Mozilla. I’m on the nightly builds (Minefield) on my Linux and Windows 7 partitions (though I use (Linux almost always). But I think that’s helpful here because there are far fewer Linux testers anyway. Mostly it’s just running Litmus tests, benchmarks, and giving feedback on UI/UX changes as they appear and/or break :). It’s a cool feeling to get daily updates and check out what new patches were made and to see the design process through the eyes of the developers. I’m also running on the dev branch of Google Chrome and there too it is interesting to witness the evolution of the interface and behavior. One day I imagine myself actually a developer at one of these companies, pushing out updates to millions of users… that’s something I like to think about everyday. I know it’s silly but I get a kick out of pretending that every commit I make has some tremendous influence. Maybe one day it will be so and all this is good preparation for that I guess :).

This summer my main goals are to of course continue my research but also to learn Python and master C++ properly. I have a somewhat adequate knowledge of C++ right now but I never learned it formally – I want to take this summer to actually learning the finer details of the language. Being that many, many applications are written in that language and that it’s widely the most popular language for programming contests too, it is certainly worth learning. As far as Python, I know that I won’t regret learning that. Seeing how much I’ve come to respect the power of quick and dirty Bash scripts, I know I’ll love the even greater power, flexibility, and ease of use of Python. A few of my friends are learning it too this summer, some for whom it is their first language and this further motivates me to learn it.

I’m going to be taking next week off from research for precisely these reasons among others. Princeton has really tired me out a great deal and I need some time to rest and regroup. I hope to take it a lot easier and to get started on learning those languages. I’ve also got to continue work on the Princeton Math Club blog. For that, I’m thinking of implementing the new Post Type feature that WordPress 3.0 brings. And for that, I need to polish up some PHP skills and get to work hacking away at the current theme. Looks rather straightforward so it shouldn’t take too long to do.

Okay so this post is freaking long and not quick at all. It also took quite a while to type out since I wrote it from the WordPress app on my Droid. More on that in an upcoming post. But for now, that’s about it.

1 Comment

Posted by on July 7, 2010 in Uncategorized


Tags: , , , , , , , , ,