All things Vi(m)

This is my first post on my own blog, website and domain. I did the famous 5 minutes WordPress install, which in my case took a bit longer. Of course, these are ideal minutes, but still it was pretty easy. As this is my first try – I am expecting that this will be my best or possibly my worst post ever. Anyway I am planning for it to be the longest entry. I hope that this at least succeeds.

A little tidbit of background information about myself. I was born in Bulgaria. My parents are from Indonesia and genetically speaking I am Indonesian. We moved to the Netherlands when I was thirteen years old. I have a Dutch passport went to a Dutch highschool and university. The point I want to make is, that my English to put it mildly is far from perfect. Please keep that in mind, while you are reading.

There are many topics worthy of a post, but it was Jamis Buck’s announcement that he is switching back from Textmate to Vim, that helped me make up my mind. I have been using vi(m) for longer than 10 years, first reluctantly, than with hesitation and now … I don’t know exactly. I would say something resembling love.

The funny thing is, that once you learn the system and have a decent typing speed you can become addicted to vi. Even stranger is the fact, that nobody uses more than a subset of the complete functionality available. The main reason being that it takes time to get used to this type of editing. If I am allowed to do a comparison with other editors – still vi(m) will be #1 for me. For instance, the other “old” Unix editor. You know starts with E and rhymes with wax. Just for fun let’s compare some of the commands, not including the obvious winner commands on the home row.

Yes, the famous ViEmacsTable. So you have 3 options basically, get RSI, use another editor or use viper mode. Just kidding. I tried using viper mode, just in Terminal on my Mac laptop, but I could not figure out, where the Meta key is. Probably have to hold the fn key.

Back to the famous switcher. His claim is that you can use vim as an IDE or at least replace Textmate. I don’t know anything about Textmate, except that it is apparently really cool. Cool with at least 5 o’s. I do know a bit about Eclipse, less about Netbeans and almost nothing about IntelliJ. It is very likely, that I have played around with other IDE’s, but I just can’t remember. Oh yeah, I forgot Visual Studio. Used it for my last project in university. In retrospect, should have used a plain text editor with makefiles.

Would a Java programmer use vi(m) instead of Eclipse? No. Why? Lack of code completion, refactoring and all the other options. Well, there is eclim that should help with a lot of these shortcomings. Unfortunately, I did not have time to try it out, but still I haven’t heard of anybody actually using eclim and I am pretty sure I would not even be allowed at work. Still I think that with the proper setup vim is the better choice for other languages, mostly dynamic languages. Myself, I cheat a bit, I use a special Eclipse plugin (not free but cheap, don’t want to give you the details but it is easy enough to google). It supports a large portion of the subset of commands I use. Right from Eclipse, which is a bit of a hassle especially with pair programming, because it involves clicking the “vi disable button” and apologizing after unsuccessful editing attempts.
According to my analysis blogging about vim requires the following ingredients: favorite commands, plugins, settings in .vimrc and less familiar commands, which now prove useful. First things, first, favorite commands. Obviously, hjkl basic movement, dd and D deleting lines, dw and cw words editing, f jump to character, the dot command. I have the tendency to delete more than change, even if changing is the better option. Also tend to use dot instead of a number in a command, mostly because I cannot be bothered to count the number of lines in a dd command. It is easier to just hold down, the dot key for a while. gg for going to the first line, G to the last. yy for line yanking, p and P for pasting. q for macros. I don’t use macros that often, but sometimes it seems easier to use a macro, instead of writing a script.

Vim can be extended using plugins. Some plugins can really help you to replace IDE features. This command

:helptags ~/.vim/doc

updates the documentation to include information about your installed plugin.

The NERDTreePlugin is the one that I use the most. It is a file system browser in vi style, which means that you can navigate through directories without using a mouse. I hope that somebody makes something like that for Eclipse.

The installation is easy and similar to other vim plugins. Just download and unzip in your ~/.vim directory.

I made a special mapping for the F3 key in ~./vimrc to toggle the navigator on the left.

map  :execute 'NERDTreeToggle ' . getcwd()

NERDTreePlugin comes with its own commands. Here is a list of the ones I use most of the time.

  • H shows/hides hidden files
  • t opens in new tab
  • o opens node

The cycle windows command Ctrl W Ctrl W switches between editor and navigator

TagList is a handy plugin for coding. It shows tags as the eclipse outline. TagList requires ExuberantCtags. ExuberantCtags warrants a more detailed explanation. Although my Mac comes with ctags, I could not use it, which is annoying, but on the other hand it cannot even tell you the version number. The generic way to install ExuberantCtags is , downloading it and compiling:

1
2
3
4
5
6
curl -O http://superb-west.dl.sourceforge.net/sourceforge/ctags/ctags-5.7.tar.gz
tar xzvf ctags-5.7.tar.gz
cd ctags-5.7
./configure
make
make install

The binary is then placed in /usr/local/bin/ctags. If you have MacPorts installed (which I have) do:

sudo port install ctags

the binary is then placed in /opt/local/bin/ctags. From vim you can then type

1
2
3
:cd project_root
:!ctags -R .
:set tags=tags

Back to TagList. Again just unzip in your ~/.vim directory. These are my settings in .vimrc for TagList. They point to my ctags binary from MacPorts, mapped to the T key. Window is on the right. By default – the tag list is shown on the left, but I prefer to have only the NERDTree there. Anyway the window can be rotated to another spot by Ctrl – W r.

let Tlist_Ctags_Cmd='/opt/local/bin/ctags'
let Tlist_Use_Right_Window = 1
map T :TlistToggle

Ctrl W h or l,switches to the left or right window. j and k scroll in the taglist window, pressing Enter on a method navigates to its definition.You can browse help with

:help taglist and :help taglist-intro

Vimpress is the plugin that I am now using to write my blog. I am still evaluating it so I cannot say too much about it.

It has the following commands:

  • “:BlogList” Lists all articles in the blog
  • “:BlogNew” Opens page to write new article
  • “:BlogOpen id” Opens the article for edition
  • “:BlogSend” Saves the article to the blog

There is a blog.vim file in the plugin directory, where you need to setup your login details. On the first test run with :BlogSend, vimpress told me that “An error occurred”. So I had to analyse the network log with Wireshark. Wireshar used to be called Ethereal and is a packet sniffer with a nice GUI. The Macports Wireshark I have installed doesn’t look that nice though. What I typically do, is capture some network traffic and then do a filter with “tcp contains [search phrase]”. I will then “Follow the TCP stream” on the filtered packets. Below is the result with some minor edits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0"?>
<methodResponse>
  <fault> <value> <struct> <member> <name>faultCode</name> <value><int>405</int></value>
           </member>
           <member>
             <name>faultString</name>
              <value>
                <string>XML-RPC services are disabled on this blog. An admin user can enable them at ....wordpress/wp-admin/options-writing.php</string>
              </value>
           </member>
        </struct>
     </value>
  </fault>
  </methodResponse>

Apparently you have to enable XML-RPC services to get this to work. Also I noticed that by default vimpress doesn’t give the blog a title. A Meta header is generated where you can specify the title. The other fields in the header should be left blank, I believe.

Blog.vim is written in Python, so I added a bit of error handling and now if I change the url settings for WordPress, and do :BlogList I get:

A protocol error occurred
URL: ivanidris.net/invaliddir/xmlrpc.php
HTTP headers: Date: Mon, 05 Jan 2009 16:38:29 GMT
Server: Power MOD by myhost.net
Connection: close
Content-Type: text/html; charset=iso-8859-1
Error code: 404
Error message: Not Found

What I changed was to add an exception handler in blog_list_posts():

1
2
  except xmlrpclib.ProtocolError, err:
    onProtocolError(err)

Of course you can do this for the other methods as well, this is left as an exercise to the reader :-).

1
2
3
4
5
6
7
def onProtocolError(err):
    print "A protocol error occurred"
    print "URL: %s" % err.url
 
    print "HTTP headers: %s" % err.headers
    print "Error code: %d" % err.errcode
    print "Error message: %s" % err.errmsg

Vim features I do not use that often include folding and markers. As a matter of fact I don’t use folding in Eclipse much either. I had a brief look in :help folding. The usage pattern is: step 1 define a fold method. For instance :set foldmethod=indent. zo opens, zc closes folds and za does both (alternates). Markers are created with m and then a letter in upper or lowercase, for example ma. You can jump back with ‘a or `a. Personally I find this cumbersome, but hey that is what vim does to you. I guess if you execute a command a hundred times, these kind of feelings disappear.

Now onto .vimrc, which if you did not know is the configuration file for vim. It is a very important file, there are even special websites, where people post their .vimrc. I am not going to do that here, but merely show the bits that I think are interesting. First, most importantly indentation, which of course is crucial if you program in Python. The settings shown below are apparently according to the Python style guide. I haven’t checked I am embarrassed to say.

set sts=3
set smarttab
set shiftwidth=3
set autoindent
set expandtab
set backspace=start,indent

Then there are the setting, which further help with coding. I have similar settings in Eclipse.

set number
set ruler
set hlsearch
syntax on

Line numbering, highlighted searching and syntax coloring is what it is supposed to do. Ruler too.

Spellchecking is a new feature in Vim 7, as far as I know. If you have it on, all your errors get underlined. Here for met it is a red dotted line for spelling mistakes and blue for grammar.

map se :setlocal spell spelllang=en_us
map sn :setlocal nospell

If I do backslash se, all my code and the vim commands are underlined. I can turn it off with backslash sn, if I want to, but for blogging it is perfect. One warning, you have to press the s quickly after the backslash, otherwise vim will only register the s command and you will go into insert mode.

My screen resolution is set to 1680 to 1050. For me, small fonts are unreadable so I have set my font on the MacVim to 18 by default.

set guifont=Monaco:h18

Why I use MacVim? Well, it is a “real” Mac application and you can use it in full screen mode (Cmd Shift F), which is awesome. Also I have set it up to be an external editor, so I can edit files from Finder and other applications. In Firefox I am using the It’s All text plugin to edit textareas with MacVim. Vimperator might have been a better choice, but font wise I don’t like it and it does weird things to the toolbars. Firefox and my Mac setup will be the topic of essays to follow. The thirteen thousand character line has been crossed already, so it is better to leave something for next time.

The topics I am planning to blog about include Maven 2, Python, Flex, Eclipse, Java, Groovy, Grails, shell scripts, my Google App Engine experiments and Vim again. Not necessarily in this order and not necessarily this year. Originally the plan was to submit an article each month, but realistically it might turn out to be once every two months. I hope to keep it up for at least one year. Hope to see you next time and please feel free to comment.

By the author of NumPy Beginner's Guide, NumPy Cookbook and Instant Pygame. If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Share
This entry was posted in editors and tagged , . Bookmark the permalink.