/code - a blog


The State Of My Vim Configuration

May 30, 2011 at 12:14 AM | categories: Vim | View Comments

I've grown to become a huge Vim fan over my years of programming, probably beyond what most people would call normal. I think my interest in Vim stems from my interest in the tools that I use to code; I appreciate the manner in which I write code almost as much as the code I write. In the craftsman philosophy, I'm a big fan of functional tools that serve their purpose well, CoolTools for example. In the hacker philosophy, I'm a big fan of constantly improving and hacking everything to achieve that, my terminal and Vim for example. Vim is a great tool that continues to show me new tricks every day but I think that with my understanding of Vim, it's time to give back and advocate for Vim's awesomeness.

Vimrc

The .vimrc is where most tweaking begins. Mine is currently up 675 lines, half comments, half code. Latest version is always available on Github.

Vim has a deep learning curve, with nearly everything being customizable. After severals years of tweaking, my .vimrc has turned into a mix of aliases and settings, letting me code the way I want. Here are some basic key remappings for some alternate ideas:

" Professor VIM says '87% of users prefer jj over esc', jj abrams strongly disagrees
imap jj <Esc>

" Save even more keystrokes!
nnoremap ; :

" Move around split windows with ctrl key! map <C-H> <C-W>h map <C-J> <C-W>j map <C-K> <C-W>k map <C-L> <C-W>l

" have Q reformat the current paragraph (or selected text if there is any): nnoremap Q gqap vnoremap Q gq

" Go to the middle of the line nmap gm :exe 'normal '.(virtcol('$')/2).'\|'<CR>

"Easy edit of vimrc nmap <Leader>ev :e $MYVIMRC<CR> nmap <Leader>sv :source $MYVIMRC<CR>

"Clear all spaces at the end of the line nmap <Leader>s :%s/\s\+$//<CR>

Current Plugins

Plugins are the other powerful component of Vim. Vim is a very powerful text editor, but through its many plugins, it can be extended to be a 'build your own IDE'. This might seem a little over the top, but I've found it has been a great learning experience, to help better understand my code and what information I want out of it.

Pathogen has been the most recent plugin that has definitely made it far easier to install and try out new plugins: instead of install a Vimball and tracking what files you installed in your ~/.vim directory, it creates a ~/.vim/bundle/ directory where you can install plugins into separate folders and it keeps everything much more tidy. After you have Pathogen installed, all other plugins are just new folders (or git clones).

  • fuzzyfinder_textmate is a fork of the fuzzyfinder plugin for Vim, which allows for "fuzzy" searching of all files in your current directory; case and place insensitive filename search. Also for fuzzy opening of buffers. FuzzyFinder This really is crucial and what I use constantly. I've been working to make better use of Vim's internal cscope system for jumping around files, but the power this provides for source and non-source files really is great.

    One of the other interesting things about this specific fork is that it was abandoned several years ago, but kept functioning in the latest Vim through patches on Github. You can view the network graph here to find the "latest" version.

  • taglist is a plugin that generates an index of the current file you are editing. Having a high level view of all classes and functions really is great.

  • NERDtree is a great sidebar file explorer and manager. Easy to browse around, easy to create new files, rename, and delete.

To Be Continued

I wanted to get this initial post out as a base, to start the story of my Vim configuration files. Subscribe for future Vim posts!

Read and Post Comments

Simple Sidebar Navigation with Sausage.js

May 29, 2011 at 11:41 AM | categories: Life | View Comments

Several weeks ago, a friend linked me to Sausage.js, "a jQuery UI widget for contextual pagination." I would call it more of a sidebar navigation widget. The example page was intriguing and prompted me to add it to this blog.

Requirements

My inital goal was based off the CouchDB example linked on the Sausage homepage. I was slowed down by trying to figure out the exact JS and CSS requirements, which aren't specifically listed out, and some conflicts with my local CSS for this blog, the main Sausage div requires height 100% and I had something else set to 100% height which didn't work well...

The main requirements:

Here is the main code to setup Sausage for my blog:

$(window).sausage({
    page: '.post',
    content: function (i, $page) {
        return '<span class="sausage-span">' + $page.find('h2').first().text() + '</span>';
    }
});

Bugfixing

The main thing Sausage.js provides is a sidebar that updates as you scroll, showing your current position among the main elements of the page. Great for blogs or documentation and no need to modify your current docs, since it's dynamically created on the client.

I wanted to add the ability to jump between elements, which when I looked, was broken in Sausage and I re-implemented poorly.

$.each($('.sausage'), function (i, e) {
    $(e).bind('click', function () {
        var curTop = $('h2').eq(i).offset().top;
        $(window).scrollTop(curTop - 20);
    });
});

Thanks to GitHub, I was able to easily fix the broken scrolling code, offer a fix, and get some simple navigation on the blog.

Read and Post Comments

Air Speakers For Everyone

April 11, 2011 at 09:44 PM | categories: Life | View Comments

Earlier today, the private key from Apple's Airport Express was found and released, along with some Perl code, finally allowing for anyone to create their own "Air Speaker" without using the Airport Express. With this code, any OSX or Linux computer can now be turned into an AirSpeaker, that any iPhone or iPod Touch, or iTunes, can now stream music to seamlessly. This really is fantastic because it allows a great deal of flexibility with how you play music, which has been limited for years now.

I found the hack via HackerNews and combined with some code improvments from GitHub, managed to get it working on both OSX 10.6 and Ubuntu 9.10. Here are the steps I followed:

OSX 10.6

Although the Perl code was originally designed for Linux, the Internet managed to get things running on OSX quite quickly. I found this fork of the Linux code to be easiest to install on OSX: https://github.com/albertz/shairport @ 2aace590d2099fcf83d8b94b350bcc67d8d58788

Assuming you already have XCode and Homebrew installed, the only steps you should need are:

  • brew install pkg-config libao
  • perl -MCPAN -e 'install Crypt::OpenSSL::RSA'
  • git clone https://github.com/albertz/shairport
  • cd shairport
  • make
  • perl shairport.pl should have you running!

Ubuntu 9.10

For my Ubuntu Boxee system, there were more packages to install. I used the original Linux code version, which the OSX version is forked from, at https://github.com/bbhoss/shairport @ 6e1f784b41629e88ab441f500538d45ec807e2e2. You might not need all these steps if you already have Perl installed.

  • sudo apt-get install libcrypt-openssl-rsa-perl libao2 libao-dev (Program dependencies)
  • sudo apt-get install avahi-daemon avahi-discover avahi-utils libnss-mdns service-discovery-applet mdns-scan (Avahi dependancies)
  • sudo service avahi-daemon start (you need Avahi running)
  • perl -MCPAN -e 'install Crypt::OpenSSL::RSA'
  • perl -MCPAN -e 'install HTTP::Message'
  • perl -MCPAN -e 'install IO::Socket::INET6'
  • git clone https://github.com/bbhoss/shairport
  • cd shairport
  • make
  • perl shairport.pl should have you running!

Assuming everything is working, the Air Speaker is running, and both iPhone and computer are on the same network, you should be able to select the Air Speaker like below. For me atleast, this is great because I have all my podcasts on my iPhone and really love the iPhone app Podcaster for listening and organizing my podcasts. Now I don't have to sync anything on my computer, I can keep it all on the iPhone and just play my podcasts to what ever computer I want wirelessly!

AirSpeakers

Read and Post Comments

On Easily Replacing Text In Vim

April 02, 2011 at 03:43 PM | categories: Life | View Comments

Or more specifically: search and replace using the current visual selection.

Lately with my job, I've been spending more time refactoring old code than writing new code. Refactoring and manipulating code (and text) is something that I think Vim is great at, once you really understand the different modes and motions that Vim has, see Stack Overflow for a good summary. Vim's Search and Replace mode is what I've primarily been using and while refactoring, I found some Vim tricks I wanted to share.

Let's look at normal search and replace usage:

:%s/foo/bar/gc
Find each occurrence of 'foo', and replace it with 'bar'.

When dealing with complex functions or tricky variable names, I didn't want to type out my function name again into Vim's command line. I wanted a way to visually select my search text and just type my replacement text. I found some help on Stack Overflow again, finding out about the <C-R><C-W> shortcut to insert the current word under under cursor. The Vim Wikia explained this shortcut a bit further and I had enough ideas to write something out:

vmap <Leader>r "sy:%s/<C-R>"s/

So, visual map leader key + r to the command: yank the current visual selection to the s register, then go into command line mode, setup a string replace on the current buffer and use to load text from the s register. Then you can finish the replace command!

My final version include a tip from the Wikia article to run the search string through substitute() to replace newline characters, allowing for multi line/end of line searching. I had to switch to using the @ register format instead of the quote register format for vim to complain though.

vmap <Leader>r "sy:%s/<C-R>=substitute(@s,"\n",'\\n','g')<CR>/

To expand on this command, to find the number of occurrences of the visually selected word, before you run a replace on it:

vmap <Leader>c "sy:%s/<C-R>=substitute(@s,"\n",'\\n','g')<CR>//n<CR>

Other cool search aliases I found in vim:

  • [I shows all instances of the word under the cursor in the document
  • ]I shows all instances of the word under the cursor after your current location

One final tip, regex can be a dangerous tool kids, so always use the confirm flag when replacing!

Read and Post Comments

Toggle OSX Audio Output From The Command Line

March 31, 2011 at 10:51 PM | categories: Life | View Comments

I spend alot of time at my computer and with that much usage, have both speakers and headphones for different hours of the day. I was initially physically swapping 3.5MM audio output plugs on my speakers, lotsa actual effort! I then figured out I could plug my headphones into the "rear" speaker output and switch output through the audio control in OSX, much easier! But that was even work, to open the audio panel and chose the correct source, so today I figured out how to toggle sources in Applescript and the command line. I then bound that script to a keypress and now I can toggle audio sources much easier!

Since I run a Hackintosh, I am using my motherboard's internal audio chipset with 8 channel output, although I'm assuming any Mac Pro or MacBook system with multiple audio output sources could use this. Internal speakers is regular out, Line Out is my headphones. Here is my audio panel:

OSX Audio Panel

At the beginning I found SoundSource and a few other menubar options for easily switching audio sources, but they still required work. I searched on Google and StackOverflow, but only found 30-50 line Applescript scripts which just controlled the Audio Panel, which seems klunky to me. The first piece of the puzzle was "switchaudio-osx" on Google Code which allowed me to control audio sources from the command line. Download the source, which contains a binary SwitchAudioSource.

Basic usage from a command line is to get a list of audio sources, shows your current audio source, and chose a new audio source:

ruin:~/$ SwitchAudioSource -a
Built-in Microphone (input)
Built-in Line Input (input)
Built-in Digital Input (input)
Built-in Output (output)
Built-in Line Output (output)
Built-in Line Output (output)
Built-in Digital Output (output)

ruin:~/$ SwitchAudioSource -c Built-in Line Output

ruin:~/$ ./SwitchAudioSource -s "Built-in Line Output" output audio device set to "Built-in Line Output"

That was enough to do what I wanted from the command line, but to control SwitchAudioSource from a keypress, I used Spark, an open source "shortcut manager", which I've written about previously. Spark allows to bind anything to custom keypresses. From here, it was figuring out enough Applescript to use SwitchAudioSource to toggle audio sources properly. Simple!

Spark

Code is here to copy, suit to fit your audio sources and and enjoy!

Read and Post Comments

« Previous Page -- Next Page »