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:
- You open the directory ‘root/’.
- You want to open the file ‘root/app/controllers/wiki_controller.rb’.
- Hit Apple-T and type wiki. This matches wiki_controller.rb.
- 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.