TextMate and Vim

Tagged: Programming, Something Else, Thinking out loud Date: 29th, January 2007

Without doubt, they’re both great editors. Which one should you be using? That’s a personal decision.

Look and feel

This is a personal decision. But most people seem to prefer how TextMate looks on Mac OSX over how Vim looks. I agree with them. TextMate looks like an actual Cocoa app (because it is!).

Want to compare? Check out the picture at the top.

I feel like the tabs for GVim is tacked on (for OS X) and the overall layout isn’t nice as TextMate’s. But if aesthetics were everything, I wouldn’t be using GVim on an Apple.

Moving the cursor around

Moving around is important while editing. You should be able to move to the beginning/end of a line/sentence/paragraph with one or two keystrokes.

Basic navigation (up/down/left/right – by units/words/sentences/paragraphs)

TextMate uses Emacs keybindings for navigation, much like other Cocoa applications. ^N, ^P moves to the next and previous line. ^F, ^B moves forwards and backwards. Alt-F, Alt-B moves forwards and backwards by words. There’s also Alt-Up/Down/Left/Right. This defaults to movement by paragraphs/words. Lastly, there’s Command-Up/Down/Left/Right, which moves to the beginning/end of the document or line (^A and ^E also work for the beginning/end of the line).

You can extend TextMate’s navigation either by setting your keybindings or by using the TextMate bundle editor.

What about Vim? Navigation is already there. In command mode, h,j,k,l moves in all four directions. 0, ^, $ moves the cursor to the beginning/first char/end of the line. (,),{,} moves to beginning/end of sentence/paragraph. And to move by units of words, you have b,B,w,W,e,E.

Movement by search

My favorite part is navigation by searching for both editors. For TextMate, you can search by hitting ^S. For Vim, /regexp or fx, Fx, tx, and Tx (search by character).

TextMate has something awesome, Shift-Apple-T – search by symbols. This searches special keywords within a file. So for Ruby, it searches class/method definitions only.

Shift-Apple-T and enter “initialize” will look for the line “def initialize”.

What’s great about it though, it’s for all languages, no customization needed. For Vim, you could easily extend the editor to have ^T to look for a method definition, but you would have to define it for each language.

nmap  /^ *def

Movement by line numbers

To go to line 24 in Vim, there’s 24G. For TextMate, Apple-L 24. Not much difference.

Navigating through files

A new Rails application generates over 40 files in many different directories. You need good file navigation. You don’t want to be typing the same directory path over and over again, and you don’t want to have to use a mouse to open up a file for editing.

This is where TextMate is awesome. When you open TextMate, you can specify a directory. This directory becomes your project. Any files underneath this directory, any level deep (think **), can be quickly accessed within TextMate. You just hit Apple-T and type in the filename. You don’t even need the entire filename, just bits and pieces of it.

Here’s an example:

  1. You open the directory ‘root/’.
  2. You want to open the file ‘root/app/controllers/wiki_controller.rb’.
  3. Hit Apple-T and type wiki. This matches wiki_controller.rb.
  4. Hit enter, and you have the new file.

Really eloquent.

For Vim, there are multiple ways to navigate through files. Most of them, though, you’ll have to specify the entire path. After using TextMate, I tried to emulate Apple-T in Vim.


nmap  :e **/

This sort of comes close, although not as clean. Basically, just hit Apple-T and you’ll get ’:e **/’ on the ex command. Now you can enter in a filename within any directory underneath the current directory.

Another way would be by setting up your path. Inside your .vimrc, just use:

set path+=**

Now inside your editor, use:

:find filename

Both :e **/ and :find work, but it’s not as short and sweet as TextMate.

Tabs vs Buffers vs Windows

When you open multiple files for editing, you get tabs in TextMate. For Vim, you can have tabs and/or buffers.

Buffers are pretty handy in Vim. Say you open up a file and close it. When you want to re-open it. Just do this:

:b part-of-file-name

And that will re-open it for you. You can even check what files are currently open:

:buffers

What I find really useful though, is Vim’s window system. You can open up multiple editing windows within the same tab. Pretty neat, especially if you want to view one file while editing another (I use it to look at my unit tests while editing library files). To use the window system,

:split
:vsplit

split will split horizontally, vsplit will split vertically. To switch between windows, use ^W^W.

Highlighting text

Is highlighting text important? Yeah! You highlight text to perform a command on it or to move it around.

For TextMate, you can highlight text by holding down Shift and using any movement keys. To highlight a line, Shift-Apple-L. After you highlight some text, you can perform an action on it, like cut, copy, delete.

Basically though, you can highlight a character, line, or block with v, V, ^V. After you highlight it, use any movements to highlight more. You can use basic movement keys or movement by searches. After you highlight, you can delete, yank, search/replace, or do any commands.

Search and Replace

Speaking of search/replace, let’s see how both editors implement this feature.

TextMate has Apple-F, which has the option to use regular expressions or not. This is simple and very easy to use.

Vim has :%s/search_term/replace_term/g. Pretty confusing for a newcomer, but after you get used to it, it’s pretty simple to use. Except for regular expressions, where you’ll get a lot of \ escape characters. It looks ugly sometimes, but can be done quickly and succinctly.

Both allow for confirmation when replacing – for Vim %s/search/replace/gc, for TextMate it’s an option you set.

I have to hand it to TextMate though, search and replace in the entire project is a great feature. This lets you replace words recursively, throughout the entire project.

Copy/Yanking and Pasting

Why is copying/pasting important? After all there is an anti-pattern in programming called copy/pasting programming. Well, it’s useful for refactoring. There are plenty of times when you want to extract a piece of code. Without any refactoring tools, cutting and pasting is your best friend.

For Vim, there’s a lot. y is for yanking, and you can use it with a movement command to select how much to yank (or with visual mode). Later you can paste it with p/P (before or after current line or character). You can remember up to the last 9 yanks with “1-9 (that’s “1 or “2 or “3…). If you want to paste the latest yank, “1p. For the second latest, “2p. If you’re ever unsure, just use the command:

:reg

TextMate has the basic Apple-C, Apple-X, Apple-V for copying and pasting. There’s also ^-Alt-Apple-V for remember a list of recently copied items for pasting (this is extremely intuitive, you don’t have to remember which register you saved it in like in Vim). Hitting ^-Alt-Apple-V will list out all of your recently copied items that you can choose from (called Paste from History).

Completion, Omnicompletion, and Documentation Lookup

Sometimes, you’ll type a long word like supercalifragiliciousexpialidocious (spelling??). You don’t want to type that over and over again. Completion to the rescue.

For TextMate, just type part of the word – super – and then hit . This will complete the entire word, very simple.

For Vim, ^x^p will look previous to the current cursor position for the complete word. ^x^n will look below the current cursor position for the complete word. So if I’m below the entire word and type – super – I would use ^x^p.

But Vim 7.0 has a new feature, omni-completion. This is like smart completion based on the current language and context. For example, if I’m in a ruby file and I type:

String.

And hit ^x^o, a list of possible method names will pop up that I can choose from. The list will have all valid class methods of String.

TextMate doesn’t have omnicompletion, but there is something just as useful. Internal documentation! After typing String, I can hit ^H, which will look for the documentation on String. This is extremely useful when learning a new language.

Snippets and abbreviations

Snippets in TextMate are great. Basically, you define a snippet like this:

def ${1:method_name}
end

Now if you type ‘def’ and hit tab, it will expand to:

def method_name
end

method_name will be highlighted and ready to replace. If there were more than one items to replace, you could keep on hitting to jump to each item. It’s a great productivity gain.

For Vim, there are a couple of scripts that try to emulate snippets. But natively, there’s abbreviations. For example:

iab def defend-A

Now if you type ‘def’ and hit your spacebar, it’ll become:

def
end

With the cursor right after ‘def’. There’s no jumping around different items like TextMate’s snippets.

Integration with Unit Tests, compilers, etc.

Ever run a compiler that spit out errors and their line numbers? Didn’t you wish that your editor could just jump to those line numbers?

Well with Vim you can! You can compile code within the editor:

:make

Which will run your Makefile. Any errors will go into a ‘quick list’. To use a quicklist:

:clist
:cnext
:cprev

These commands will jump to the next/previous error or list out all of your errors. The shortcut to these commands or :cl, :cn, and :cp.

But what about languages that aren’t C, C++, or Java? Well, you have to customize it. I’m not sure about PHP or Python, but for Ruby there’s the ruby-vim gem. Check out instructions on how to install it here.

Once you install the gem, you have to manually set ‘makeprg’ while you’re editing.

:set makeprg=ruby -w

This will set your compiler to the ruby interpreter with warnings. Now in your editor, run :make , ( means replace with this filename). Any errors will populate your quicklist and you can jump to each error with :cn or :cp.

What’s amazing though is the vim-ruby’s integration with unit tests. Instead of running the file through the ruby interpreter, you can run your unit tests. Any errors will populate your quicklist (this assumes you have a Rakefile). To do this:

:source /path/to/vim/config/compiler/rubyunit.vim
:set makeprg=rake test

Now run:

:make

Vim will jump to the first error produced by your unit tests. To go to the next error, just use :cn or :cp.

Unfortunately, there are still some bugs with it. The first error is usually fine to jump to but next errors sometimes end up jumping to a blank file. Either that, or I’m using it wrong…

As for TextMate, hitting Apple-R will run the current file. So if the file is a unit test, that unit test will be executed. To run a focused unit test for Ruby, just put the cursor in a test definition and use Shift-Apple-R.

Update:
I just found out that TextMate has great integration with Ruby’s unit testing facility. If you’re currently editing a unit test file, you can hit Apple-R to run it in RubyMate. A new window will pop up, and if there are any errors, the stack traces will have links to the file/position. So you can just click on it and TextMate will open that file to the exact position of the error, pretty awesome.

What I missed

What was mentioned in this article was just some features I could think of off the top of my head. I missed a lot of features for both editors.

For TextMate, there’s macros, bookmarking, folding. For Vim, there’s recording, marks, and more folding. There’s a lot more too, like extending the editors for new languages – syntax highlighting, colorschemes, etc.

Try them out for yourself. Get a TextMate trial at macromates. Compare the two editors to see which you like best. After all, choosing an editor is a personal decision, everyone’s favorite editor is going to be different.

Don’t forget to learn more about your editor everyday. Try learning new ways to search/replace or better ways for movement. Try extending your editor with macros or scripts. Since it is a personal decision, it’s a good idea to customize your editor.

Update: Sorry for any errors in regards to keyboard instructions. I wrote this post with Textile and forgot that ^carets^ handles superscript.

27 Responses to “TextMate and Vim”

  1. sandro:

    Thanks so much for this comprehensive list, something tells me I will be reading it again at least 2x times as I search for the ideal editor under linux. Vi has always been good for quick edits but coming from windows I’m unfortunately addicted to the mouse and scrolling…vim experts don’t need to use the mouse and SAVE time because of it…maybe I can go this route.

  2. Hello Kitty:

    Your layout doesn’t actually look any good (it’s unreadable actually) when you try to increase the font size in the browser.

  3. Emir:

    Under autocompletion, where you say:

    “For TextMate, just type part of the word – super – and then hit .”

    I think a character is missing. I believe you meant to put escape symbol at the end of the sentence but it got lost in translation :)

  4. Chris:

    Great post! I’ve been using TextMate for months now and still learned something incredibly useful (I had no idea about Control-S) and was reminded of Control-H. Good stuff.

  5. Keith Lancaster:

    @sandro: I tried Emacs for a while under Linux, but ended up with VI (actually, VIM). Fewer keystrokes than Emacs, however you do have to get used to the modal nature of the editor. Very different (at least for me).

    I’ve been using TM for well over a year and love it. The only thing I miss sometimes is being able to use regexes in ^S searches. I’d also like to be able to have all the matches in the file highlighted.

  6. Janus’ Weblog » Blog Archive » Why is TextMate only for Mac?:

    [...] TextMate Wikipedia page | Comparison with VIm [...]

  7. Oskar L-B:

    “For TextMate, just type part of the word – super – and then hit .”

    The key you want is Esc.

  8. JMC:

    The textmate fanboys at their finest…

  9. meek:

    for Linux, I highly recommend scribes. It’s TextMate combined with VIM without the hideous learning curve of VIM and much simpler than TM.

    This flash demo got me hooked.

    http://scribes.sourceforge.net/demo.htm

  10. David G. Paul:

    Vim’s a great editor, but going back to a different one (such as TextMate as you menioned) can sometimes leave you trying to do things the vim way – such as :wq to save and quit. Oops!

  11. Sami Samhuri:

    Great write up on my 2 favourite editors. TextMate stole me away from vim for basically all the reasons you mentioned here.

    I despise using lots of \ escapes in my regexes as well and in vim you can turn on so called “very magic” mode using \v in the regex (\V to turn it off, akin to \L, \E, etc). After that ( ), , and all the good things you want will be magic. Think of it as going from grep to egrep.

    Nice to know I’m not the only one who loves navigating in vim but finds the treelist plugins lacking, and realises that the power of TextMate could be mimicked in vim but at a significant investment of time and work that’s already been put into a nice Cocoa app for us. Allan deserves every Euro cent he makes.

    The Rails bundle is really great. Use Cmd-Opt-down to jump from controller/model to tests, and Cmd-Opt-Shift-down to go from controller to view and vice versa.

  12. Kevin Chiu:

    A couple points:
    1. You can access vim remotely through a terminal.
    2. Vim is cross platform.

  13. Jim Hinds:

    Thanks for adressing the Vi (or VIM) vs Textmate issues. Basically, however, you are just ’selling’ the features of textmate, rather than addressing the adaptation of textmate to allow for VI like behavior.

    I use textmate often, but It’s not my favorite editor. I’d like it to be my favorite editor, but it’s got to be either universally implemented OR more adaptable to my fingers.

    The reasons that VI has been around forever (well 20 years, anyway) is that it is implemented on every platform, from small to large, from X-windows to “whatever” and all these implementations behave in a consistent manner. The learning curve is nasty, (and so is textmate’s) but once you have gotten over the hump, your fingers just do what they do. You don’t even have to think about it. So when I want to move some 5 lines of code up 4 lines, 5ddjjjjjp (or whatever my fingers are doing) just works. And textmate does not do that.

    It would be different if Textmate was universally implemented. I would dump VI in a heartbeat and go through the learning pains to do all the (admittedly) cute tricks that textmate excells at. But when I need to massage that code on a terminal oriented system that is 5000 miles away from me, I am going to use VI. It’s that simple. As a contractor, I’m adaptable to the needs of my customer.

    Textmate is supposedly adaptable. If so, then adapt it to have an easily entered/exited VI compatibility mode. I hear you say that Textmate is modeless. Well, think about it: the mouse movements ‘ARE’ the non text mode. So you are really just using ‘out of band’ communication to do the kind of things that VI does with ‘in-band’ communication.

    Thanks for the opportunity to share.

  14. Stephane Liu:

    I’ve been using VI for over 7 years and recently purchased my first mac. Being a new mac fan boy I decided to switch over from vi to textmate because all the cool rails kids are using it. I gave it an honest effort of a month, finding all the ways to make textmate work for me and make me more productive in my rails programming.

    After a month of solid use I switched back to my beloved VIM with Tim Pope’s plugin script for rails: rails.vim. VIM coupled with this plugin has been the best experience period. I agree with newbies that say VI is hard to learn but give it time and keep learning, you won’t regret it.

  15. Paul Betts:

    Cool article, definitely learned some new tricks

  16. Adam Byrtek:

    Thanks for doing a comprehensive comparison in real-life use cases, this is much more interesting than just a feature table. My main problem with Vim is it’s weak support for quick navigation inside a project (ctags is not enough) and lack of good file outline (no, I haven’t checked plugins yet). Moreover the window/tab/buffer distinction in Vim is confusing, but it might be only my problem. When it comes to Mac, it would use some better OS integration, not to mention having anti-aliased fonts working correctly with Unicode.

  17. adwin:

    I have just learn VIM (before I use notepad++ and emacs) and it is not as hard as people mentioned or think before,… rails.vim made by tpope was very good tools … after I doing research about textmate, i think VIM is wide supported and has many usefull scripts to faster the development, therefore I stick on VIM …

    may be i will give a try on Textmate after it released on linux or windows:)
    I prefer to use multiplatform tools

  18. Adam Byrtek:

    Small update to my previous comment. My complaint about file outline in Vim is no longer valid with taglist plugin installed, but I still haven’t found a good way to handle project with multiple files and easily navigate inside it, which TextMate handles really nice.

    When it comes to finding good Vim port for Mac OS X I could recommend MacVim that I found recently. This is still work in progress, but it is already extremely stable for me, and I use it daily. It is much nicer to use than the old Vim.app port, and it has working Unicode support. Hope it will reach more stable phase soon.

    MacVim can be found here:
    http://code.google.com/p/macvim/

  19. jkyle:

    Very good little rundown. A few notes on vim and project management. There are two related plugins that that facilitate easy management of large projects: Project and Rproject. Rproject is Tim Pope’s adaptation of Project for rails with all the rake and script functions built (such as :Rake or :Rscript).

    They both provide the sort of easy navigation you speak of. They also allow full editing or filtering of the project (settings saved for future sessions). For example, if you don’t want $project/tmp or $project/cache files to show. I’m sure Textmate can do this as well.

    Lastly, for cocoaesque cut/paste abilities, mouse scrolling, etc. Use Gvim, which can be compiled from macports or downloaded as a standalone app.

    I’m not pushing vim here, only reiterating to choose the editor whose editing style feels best for you. I’m sure there are very few if any features of Textmate that can’t be found in vim with a little vim.org plugin search (and the reverse is likely equally true).

  20. Travis J Jeffery:

    Adam, try the VTreeExplorer scripthttp://vim.sourceforge.net/scripts/script.php?script_id=184

  21. Travis J Jeffery:

    Woops: http://vim.sourceforge.net/scripts/script.php?script_id=184

  22. Mark Wilden:

    I also just switched from Windows to Mac, partially (mainly?) so I could use this great editor for Rails – TextMate. After a month or so of use, I would have to say that this is one of the worst programs that people love I’ve ever used. That’s very different from being a bad program, but people -love- this thing, and I don’t get it.

    I tried very hard, but I can’t believe that people would use an editor that doesn’t let you see two files at once. Even if that were acceptable, you can’t switch between the current tab and the previous one you were using with a keystroke. In fact, the tab for the current window can actually be invisible (and you can’t move it by dragging from the dropdown menu at the right edge). The project drawer is very good, but removing items is buggy and doesn’t work. I have other problems, but those are pretty major. So much of what people seem to like about it are available in other editors – sometimes even better. For example, the “Intelligent Go To File” in the Rails bundle is laughably stupid compared to gf in Tim Pope’s vim plugin.

    I wish I liked TextMate, so I could be like Ryan andDHH. Oh well. I’m still glad I switched to a Mac. :)

    ///ark

  23. Sean:

    @Mark,

    You’ll probably never read this, but…

    You are right, there isn’t a good way to open two files in a project side-by-side without opening the project twice. But I’d bet this will be included in a future release.

    Switching files threw me off for awhile too. However, the Apple-T (Project File Search Thing) can be used for that. Apple-T Brings up a list of all files in the project in reverse order of when you last focused them. So Apple-T Enter will take you to the file you were just on.

  24. Walked by:

    Apostrophy in your text is displayed as three characters in my firefox on ubuntu hardy.
    I tried many encodings, but did not find any to eliminate this scramble.
    Anyway good review.

  25. Anmar Oueja:

    I am all for textmate but I still can’t shake the speed I can edit stuff using VIM. Tried emacs but found it too overkill.

    I just wish VIM has something similar to Textmate’s snippets. There are scripts but they are not the same as the ease of use of VIM.

    Also, abbreviations don’t support multiple lines in VIM. I know there are hacks, but come on. This is a simple and easy thing that needs to be rectified sometime in the future.

  26. Mario Grgic:

    I don’t know if this is worth arguing. VIM is more powerful and more customizable (it comes with it’s own Turing complete language, so you can extend it to do what ever you want).

    VIM is free (source code is open) and available for any platform you can think of (including e.g. Amiga).

    MacVim (unlike gVIM) that you can download here:

    http://code.google.com/p/macvim/

    is actually a Cocoa app (and I know that matters to a lot of people).

    By the way VIM can also work on a logical grouping of files on the file system (there is no concept of project but you don’t really need it), e.g.

    vim $(find “some complex criteria”)

    then vim will open all the files that match. Now you can execute a complex editing command on all files with

    :argdo

    e.g. you could record a macro and play it on all files (look up help on it :h :argdo). You could execute normal commands or even a script. That’s pretty powerful.

    But in the end it all comes down to personal preference.

  27. Arthur Debert:

    Hi Vladmir.

    Having moved recently from TextMate, I’ve a couple of plugins that address those issues:

    1. Nerd Commenter (http://github.com/scrooloose/nerdcommenter)
    Inserts / toogles multiline comments in visual or command mode

    2. Fuzzy Finder Textame ( http://github.com/jamis/fuzzyfinder_textmate )
    Just like Command T in textmate, really well polished

    3. DelimitMate (http://www.vim.org/scripts/script.php?script_id=2754)
    Automagically balances (), {}, “” when inserting text

    4. SnipMate (http://www.vim.org/scripts/script.php?script_id=2540)
    Snippets a la textmate

    With those in place, I no longer miss textmate at all.

    Cheers
    Arthur

Leave a Reply