Vim Day One

vim screen shot

I spent most of the day today getting comfortable using Vim. If you are not familiar with it or some of the other high octane text editors available and you spend most of your time at the computer writing text files, you might want to check them out. Vim and it’s ilk are highly configurable text editing programs made for productivity and extensibility. The pertinent information in this post falls into the “write this down for next time” category, but I thought I’d take a moment to also share why I am jumping into Vim after 6 years of writing code in IDEs.

Over the years I’ve taught myself several programming languages and the programs that are most commonly used to write them. I’ve used Eclipse for Android and Processing, Flash/Flex Builder for ActionScript, XCode for Obj-C and C++ and Visual Studio when I’m on the Windows side of things. Add a handful of other programs like TextMate, SublimeText, Komodo Edit and TextEdit to the list of editors I’ve used for Python and JavaScript and it all stacks up to quite a lot to think about. Each one of these programs has it’s own paradigms and user interfaces that at the end of the day just abstract many interesting things that I feel I should know how to do on my own. This brings me to one of the major reasons for learning Vim. I want to move away from relying on IDEs for compilation and packaging and learn to handroll these these tasks. I’d like to have a better handle on writing shell scripts to automate builds and deployment and for packaging distributable software.

Additionally, I’ve found that the text editor is the most important part of my toolchain. A good editor that doesn’t distract yet is responsive is essential. I like TextMate quite a lot. However, I have reached a point where I want to fine tune my environment beyond what TextMate offers and Vim seems to be the tool for that.

Being able to use Vim from the command line is a major plus for me. If you have Mac, it is most likely already installed on your computer. Just open the Terminal and type vim. If you immediately attempt to enter text you might be a little shocked. Instead, re-open Terminal and type vimtutor. I am actually using a program called iTerm2 which is a more advanced version of the Terminal.

Here are the first things I set out to do on my first day using Vim and some impressions or tips on each.

Complete the vimtutor
A quick intro that focusses on learning through doing and teaches all of the basics you need to know in order to get going. This is definitely the place to start out.

Set up Solarized color scheme
This proved to be pretty simple and required me to learn a little about Vim’s plugin structure and configurability. You can get the Solarized theme for Vim complete with installation instructions here: https://github.com/altercation/vim-colors-solarized

Customize behavior for certain languages
One thing that I really like about Vim is how the plugin and syntax highlighting system works. All configuration settings and plugins are contained within the root directory of your computer in a folder called .vim. Within this directory you can add sub-dirs that contain .vim files that define indentation, syntax and color schemes. There is also a file at the root called .vimrc. This file defines global settings for Vim.

So far my .vimrc file looks like this:

syntax on
colorscheme solarized
filetype indent on
filetype plugin on
set background=dark
set nu

The last line from that snippet turns on line numbers in Vim.

Another helpful trick I learned when trying to load plugins was to list all of the loaded scripts. Keep in mind that scripts are loaded as they are needed. So if you are trying to load a python script the syntax file for python may not have loaded yet and won’t appear in the list. To view loaded scripts type :scriptnames from within Vim.

I also need to update the indentation behavior for Python code. To do this I added a file called python.vim to the ~/.vim/indent. The contents of that file look like this:

set expandtab
set textwidth=0
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set backspace=indent,eol,start
set ignorecase
set smarttab

This seems to work pretty well for all of my existing Python code. It sets indentation at 4 spaces which is compliant with the PEP 8 style guide.

Posted in Coding | 4 Comments

4 Responses to Vim Day One

  1. 28inch says:

    Have you tried sublime text 2 yet? A bunch of people I know switched recently, including myself. I`ve been using it for almost a year now and it is just awesome. And its all python!

  2. Jeremy says:

    Cool, I’ll have to give it another look. I really like that I can open Vim directly in the terminal. Can you access the command line from Sublime Text?

  3. Per says:

    I started with vim about a year ago, and I have fallen more in love with it. I like the customization and also the control of using short commands like 4dd, remove 4 rows and store data in vims buffer.

  4. 28inch says:

    @jeremy Sure you can. I`m using it on windows, so is just adding path. It even works when I alias(overwrite) the .exe to a shortcut. It look like this on the cmd:
    ‘sbl somefile.py’ .

    The python based plugin structure could be an other benefit for you. The build scripts are also cool. For example, I managed to build your previous tutorial in ten minutes with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>