I've been using Notepad++ for a while; in fact, I've even started using Launchy for that "load this resource into the editor right now" functionality that many fuller IDE's like Eclipse has. It has syntax highlighting, split window view, code collapsing, parentheses (and other delimiter) paring, automatic indent, block commenting.
However, it seems that everywhere I go, people are using vim for their programming needs.
I know the basics of vim; it's my main "basic text editor" when I'm on a linux machine; I use it like I'd use the basic Notepad on Windows. I get the controls, most of the shortcuts, the repetition eliminators, etc.
What I'm not quite understanding are these killer apps that people are espousing about. How "debugging Ruby in vim was a life-changing experience", or how, when using it, magic occurs.
Am I missing some essential plug-ins? Is there a dimension I'm not seeing? Should I just shut up and start using it for a bit, to see? How do I get syntax highlighting?
I'm specifying Ruby specifically because I'd like to find some plugins for it. Note that I am not using Rails, and answers should be rails-independent of possible. But I would appreciate some general vim-ness koans about programming in other languages, as well.
Thank you, and I hope my question isn't too vague or inspires any nasty editor wars.
I use vim for all my Ruby programming, and I think its customizability is its killer feature. With vim you can do just about everything to text you can imagine, if you're willing to invest the time to hunt down the plugins that do what you want or write a few scripts yourself.
I'll just list a few things I like about vim for programming (in no particular order):
Syntax Highlighting
The ruby syntax highlighting is very nice. One thing in particular that seems to be somewhat unique is that keywords can be colored differently depending on context. This isn't used as much as I would like, but you can easily see whether that
end
statement closes, say, an if-statement or a function definition.Also nice is that, since vim knows which parts of the text are comments, you get spell checking for these only.
Automatic indenting.
When you're writing code, vim will automatically place the cursor at the right indentation level, so you don't have to worry about that. I also find myself invoking this functionality manually by selecting a block and pressing
=
to automatically (re-)indent everything I highlighted.Autocompletion
I use a plugin that automatically pops up completions.These are very versatile. They know the methods of classes from the standard library, look at other files you have open (good for variable names and class methods), recognize when you're typing a filename, etc.
Snippets
There is a plugin called snipMate that provides shortcuts for often used text snippets. It's a big help with writing tests and the like.
Code folding
Scriptability
As I said, vim's scripting is very powerful. Want extraneous whitespace at the end of the line deleted automatically? Just write a one-line script.
Plugins, Plugins, Plugins!
There are a ton of plugins that help you with all kinds of things. Git integration, Rails integration, Rspec integration, autoclosing parentheses, matching keywords that open a block {
def
,do
,if
,while
, etc.} to theirend
... the list is practically endless.
It's an exceedingly powerful editor out of the box, it integrates well with version control, and there are bucketloads of good add-ons available. (See the scripts page as well as the tips wiki.) Those are good reasons to consider Vim, but there are plenty of other good editors available for various platforms. (Look, Ma, no religious editor wars!)
In terms of very Ruby-specific add-ons, check out endwise by Tim Pope, as an example. (It automatically inserts end
after do
, if
, etc.) Actually nearly all of Tim Pope's scripts are potentially useful for Rubyists.
How do I get syntax highlighting?
You need at least a minimal .vimrc
or .gvimrc
to get syntax highlighting and automatic indentation (assuming you want that). Vim ships with examples that can get you started, and if you search for 'vimrc' or 'gvimrc', you will get plenty of hits. That said, here's some of mine to get you started:
" Most general settings first
set nocompatible " Set Vim rather than Vi settings; must go first
set noeb " Set no audio or visual error beep
set bs=indent,eol,start " Backspace over everything in insert mode
set history=500 " Keep 50 lines of command line history
" Set items for view @ bottom of windows
set ruler " Show the cursor position all the time
set showcmd " Display incomplete commands
set showmode " Display current mode
set ls=2 " Always show status bar
" Syntax basics
syntax on
filetype indent on
set autoindent
set smartindent
filetype plugin on
" Text basics
set textwidth=80 " Set text to wrap at 80 columns
set expandtab " Convert tabs to spaces
set tabstop=4 " Tabs = 4 spaces
set shiftwidth=4 " Indent/outdent 4 spaces
set softtabstop=4 " Tab key indents
set shiftround " Indent/outdent to nearest tabstop
set smarttab " Uses shiftwidth @ start of lines
set fo=trcn
" An exception for Ruby files
autocmd FileType ruby set tabstop=2
autocmd FileType ruby set shiftwidth=2
autocmd FileType ruby set softtabstop=2
autocmd FileType ruby set number
" Search basics
set incsearch " Do incremental searching
set showmatch " Show matching brackets
set hlsearch " Highlight all matches in a search
" Don't use Ex mode, use Q for formatting
map Q gq
" Pick a colorscheme
colorscheme Dim
My personal killer feature of vim is the humble .
command. This command repeats the last edit at the current cursor position. This can save oodles of time.
For me, one of the "killer features" of vim is it's ad-hoc macros.
Press q
then a key name to store the macro in (I often use m
for a macro mnemonic, but any letter is fine) and you'll notice the recording
status at the bottom. Now, any key strokes you press will be recorded until you press q
again, and you've recorded a macro in the letter m
. Now type @m
and your keystrokes will be played back, with all their implications, starting from the current cursor position. Press 20@m
and you'll replay the macro 20 times, and now you've got a powerful tool for programmatically editing text without the overhead of writing a larger program (or configuration file).
For Ruby specifically, the syntax highlighting (:syn on
), automatic indent (:set cindent
), and paren/bracket paring (%
to move the cursor to the matching brace) and other features can be found in other editors, as you mention. But really, the general text processing macros in vim are a big advantage for any text file.
I think the main advantage VIM has is the fact it's cross platform. Now you're using notepad++, that is a great editor(even if it hasn't good macros capabilities). Tomorrow, you could be obliged to use another OS, and you should learn to use another text editor.
I know where you are coming from. I used IDEs and even notepad++ for longer than I care to remember. If all you use VIM for is basic navigation, you aren't going to be using it to its full potential. while there are several very powerful plugins, I don't believe that finding the right plugin is what makes VIM so powerful.
What does it for me is that my hands do not move from the keyboard and it allows me to stay in the zone. Every time I go to the mouse, it invariably leads to a concentration break - which as a programmer is the unforgivable sin.
The other killer feature for me is searching and replacing using regex. I highly recommend learning more about it. Or in the words of Jamis Buck, "Know Thy Tools".
ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
is a great way to get started, but I think I would recommend the O-Reilly book over this one if you want to spend the money. It is easy to get the basics in VIM, but mastering it takes time - but they payoff is immense.
精彩评论