Cristian Cortez Presentation Site

@cortezcristian

Unleash the Super Vimmer inside you

This article is a compilation of tips dedicated to all the people who wants to start improving their performance using Vim. If you are going to start with Vim, just beware there’s no return.

Benefits I found using Vim

Vim helps me to move fast and found effective solutions to my daily tasks. Every time I try to improve my way of working, I find mysef learning new solutions with this tool, and that’s why Vim has been turned into my favorite IDE. If you want to achieve your goals quick with a minimal effort and reuse solutions you explored in the past Vim is the tool for you. One of the things I love it’s that Vim is almost everywhere, it works in several platforms and operative systems, you can find Vi pre installed in your android mobile phone. Recently a web coding IDE (Cloud 9) added a vim mode to their Ace Editor, this fact in my opinion make their platform the coolest.

Another greatness is that Vim is highly customizable. At the beggining this can take some of your time but believe me it’s actually an investment. So take your time to create your own shortcuts and macros, abbreviate terms and find the plugins that feed your needs before start coding.

Re-thinking the concept of DRY (Don’t Repeat Yourself) with Vim. As a common example: how many times did you find yourself in your editor saving an html file and then switching windows going directly to the browser and refreshing it to see the result of your changes. If you find yourself always doing the same steps… Why don’t you automate that? This is where Vim Scripting come to action and you will learn how to automate that later in this article.

At this point we can agree that Time is gold, and we don’t want to waste it. If you are like me, and stay the day running from a project to another you can ask yourself: Which is my set up cost? In other words, how much time you take to prepare your enviroment to get your hands on the real work. You can save multiple sessions with Vim, don’t waste your time opening files again or trying to remember where you were the last time you touch it. I know that the mayor part of the IDE’s let you do the same, but this is even faster with Vim. When IDE’s are hardly leveraged in GUI this can be very tedious, I’ve found myself with various versions of Eclipse, consuming all my machine resources and my patience.

As programmers being lazy it’s a crucial aspect of our lifes, we are always trying to write less and do more. With Vim you always have the oportunity to look for plugins, that helps you to be faster and also be creative, when you don’t find a generic plugin to solve your problems you have an oportunity to create new solutions with Vim Scripting.

Start considering that Vim can be one of your best allies on your journey in search of continuous Vimprovement (it’s that a word?). But don’t forget to be social, on your daily fight against your working enviroment I’m sure you will create innovative hacks so please share your solutions with your co-workers and the world.

Basic Stuff & CheatSheets

Vim in all its glory and its minimalist beauty, has basically the following edition modes:

When I started with Vim all the basics thing I’ve needed were knowing that the “i” key switch me to the Insertion Mode and the ESC key will send me to the Normal Mode where I can use basic commands preceeded with a colon, i.e. to save (:w), quit without saving (:q!), going to an specific line (:9) and may be opening other files (:o file.txt). After that I get used to copy an entire line (yy), delete a certain number of lines (3dd) and moving forward (C-f) and backward (C-b) though the document. If you are a begginer don’t rush, you’ll slowly start feeding your own needs. You also might find this cheatsheets useful:

When you are new at Vim, you fell happy with less. If that is your case enjoy that moment you’ll find yourself sooner or later hungrier for Vim plugins and tips.

Regular Expressions

One of the things I enjoy, is that Vim lets you play with file contents and regular expressions so easily. Being able to test until you find the regular expression pattern you need to solve a problem it’s a great advantage. After obtaining a group patterns that fulfill the operations I need to solve my original problem, I can easily write some script that iterates many files and execute the operations reusing those patterns. Do you want to learn more about regular expressions?

Regular Expressions Cheat Sheet

Search and Replace


Solution

Deleting unneeded lines

Someday you will need to remove lines that do not match an specific pattern. Suppose we want to know all the scripts included in a particular page. First we can obtain the entire html of the page like this:
:r !curl http://cortezcristian.com/

And then we can filter only the script doing this:
:g!/^.*<script.*src=.*$/d

And we have succesfully filtered all the scripts inclusions.

Macros, Abbreviations & Keys Mapping

Branching

Sessions

As a multi-tasking programmer you’ll find yourself quickly assuming many different roles, backend, tester and so on… well that’s my case. Switching between this roles can be tedious, wasting your time everyday you open all the files needed when you have to change the front end of your application. You start with a simple task and just go inside the layouts, open the css and the javascript files you will need to work with… and then you suddenly recieve a call to update some test cases made in ruby, so you continue with that to end working in a last minute change that involves a fatal error with the ORM that of course is made in a different language… let’s say php. Guess who is going to fix the new bug? Yes, you! As you change priorities fast and now your editor looks like a mess full of tabs you don’t need, so you finally decide closing all front end related stuff. It’s in that precisely momment that your boss gives you a quick call again just to tell you that the last minute change can wait until next week and also begs you to please focus again in the front end task… It’s time for you to start opening all over again…

If you are smart enaught may be you’ll start considering saving sessions according to your roles

:mksession ./frontend.vim

And getting back to your front end work with:

:source ./frontend.vim

Obviously this feature can help you in several cases, for instance if you work in different projects or the most important if you are working in a remote server, what if you lose connection? are you going to start all over again?

Comparing Files

Moving through the document

Highlight & Folding

Configure your .vimrc

Search & Install Plugins

Basic Vim Scripting

Scripting to Improve Performance

The main advice here is Don’t Reinvent the Wheel, try to search over the web first and look for a solution. If the solution is not suitable for you or you wasn’t able to find a good one then you might consider start coding it. I remember being reading an article about programming where the author wrote something like “if someone give me a penny for every time that i reload the page to test what i just did, i’ll be reach”, I thought wait a second I already resolved that, so this is the script that will prevent you from Alt+Tab follow by Ctrl-R for the rest of your life:

" reloadBrowser.vim : Refresh the Browser on Save
" Dependency : xdotool
" Installing xdotool: sudo apt-get install xdotool

function! Refresh_browser()
    if &modified
                write
                        silent !xdotool search --class google-chrome key ctrl+r
                                "silent !CUR_WID=$(xdotool getwindowfocus);BRS=$(xdotool search --onlyvisible --class "chrome");xdotool windowactivate $BRS; xdotool key 'ctrl+r';xdotool windowactivate $CUR_WID
                                    endif
                                    endfunction
                                    autocmd BufWriteCmd *.html,*.css,*.js :call Refresh_browser()
                                    

So now you can write your code, and take a look at your second monitor everytime you save a file. And of course you can modify the script to add the browsers you use and the file extensions you want.
Working Area

Coolest Plugins

Certanly the Plugins you’ll use depend on what you are working on. Anyways this is a brief list of plugins you might consider:

Nerd Tree Plugin

Obtain your “Youtube Degree”

This is a list of motivational videos not necessary from youtube you might consider watching.

(Still don’t get it… What the heck is a Youtube degree? Well find out yourself at urban doctionary)

Moving Forward

A Vimmer’s life is plenty of fun Vim scripts, not all of them are production busters. It’s a fact once you get used to it, you just don’t want to go outside of Vim. I found myself listening to music with the XMMS and editing WordPress with VimPress. Go ahead and try Tweeting about this article with TwitVim:
TwitVim in Action

Posted in Vim on Nov 24, 2011 Comments Off

Comments are closed.

Blogroll

Pages

Categories

Recent Posts