Current Articles | RSS Feed
Vim isn't just a great editor; it's also a hugely flexible, configurable, and extendable application. You can even set up Vim as an integrated development environment (IDE), giving you all the advantages of a dedicated text editor together with the ability to check syntax, jump between tags, autocomplete, and do all the other useful things you get from a modern IDE. These Vim tips and tricks will get you editing like a pro.
By the way, I'm assuming you're running Vim under Linux, but you can find versions of Vim that run under Windows, Mac OS X, the BSDs, Solaris, and most other operating systems.
It's pleasingly straightforward to set up the code editing basics of autoindenting and syntax highlighting in Vim. Just put these two lines in your ~/.vimrc file:
filetype indent onsyntax on
These two commands use built-in Vim features to identify the filetype of the loaded file and set the appropriate indenting and syntax highlighting. A standard Vim install comes with syntax highlighting rules for most of the languages you're likely to use, but you can create your own rules if you need them. For instance, to change the indent spacing from the default value of 8 to, say, 2, use this line:
set shiftwidth=2
You can also prevent spaces from being changed automatically into tabs:
set expandtab
Another standard feature in a lot of IDEs is search-as-you-type. To turn this on in Vim, put this line in your ~/.vimrc:
set incsearch
You can also use * to search for the word currently under your cursor.
Plugins are Vim scripts that load automatically when Vim is started, or when certain filetypes are opened. Filetype plugins set up various options specific to certain filetypes, such as correct comment handling and keyword matching. You can enable filetype plugins by adding the line filetype plugin on to ~/.vimrc.
filetype plugin on
Ctags (Exuberant Ctags is the Vim version) generates an index file of names found in a source tree: functions, variables, class members, class methods, and so on, with the exact list depending on which language you're using. Install and run Ctags and suddenly you can jump around between tags within your Vim session.
On Linux, you should be able to install ctags as a package (called exuberant-ctags for Debian/Ubuntu, and just ctags for Red Hat/CentOS). On a Mac it's best installed via MacPorts, and if you use Windows you can download the appropriate code from the project's SourceForge page.
Once installed, navigate to the top of your code project and type ctags -R at a command prompt to generate your tagfile. Open one of the source files in Vim, put the cursor on a method name or object type, and press Ctrl-] to jump to its location. Ctrl-T takes you back to where you were before.
ctags -R
To make Ctags even more useful, install the taglist Vim plugin. Download and unzip the zip archive, and move taglist.vim into ~/.vim/plugins/ and taglist.txt into ~/.vim/doc/. To generate the help files for taglist (and any other plugins whose documentation is in ~/.vim/doc), cd into ~/.vim/doc, start Vim, and run :helptags ..
:helptags .
To use taglist, open up one of your source files in a project you've run Ctags on, then type :TlistToggle to open a tag window. Double-click on a tag (or press Enter) to navigate to that tag in the source code window. As you open new files, their tags are added to the list window. You can also click on a tag, then type 't' to open that tag in a new Vim tab. If you edit a file, then save it, you can use :TlistUpdate to update the taglist to include the new tags.
:TlistToggle
:TlistUpdate
Check out the manual for more information, including how to map a single key to toggle the taglist window.
When you're coding, autocompletion helps you avoid mistyping long method names, and speeds things up a bit. To set up built-in omnicompletion in Vim, put these lines in your .vimrc file:
filetype plugin on set ofu=syntaxcomplete#Complete
Start typing a word or method name, then press Ctrl-N to see a list of completion suggestions. Hit Ctrl-N again to go forward through the list, or Ctrl-P to go backwards. To get full method name completion, you need to have run ctags first or Vim will be limited to whatever it can find in the current file and the dictionary.
Vim also offers a bunch of other completion types, such as filename completion (Ctrl-X Ctrl-F), whole line completion (Ctrl-X Ctrl-L), and completion using keywords in the current file. Type :help ins-completion for more information.
:help ins-completion
If you prefer tab completion, try the Vim script SuperTab.
The NERDtree plugin gives you a tree-like filesystem browser in your Vim window, which can be great for navigating source code structures. Unzip the NERD_tree.zip file in your ~/.vim directory to get the files in the right place, and run :helptags . as for taglist.
Open a file and type :NERDTreeToggle to open up the file browser. Press Enter on a filename (or double-click) to open it in the file window. Type :help NERDTree for more information, including info on its bookmark function.
:NERDTreeToggle
:help NERDTree
You can open two files in the same Vim window by using :new secondfile.txt. Navigate between the windows using Ctrl-W J (down) and Ctrl-W K (up).
:new secondfile.txt
:new splits the window horizontally, but you can also split it vertically, populating it with the same file in each pane, with the command :vsplit (:split does the same thing horizontally). Navigate between vertically split windows using Ctrl-W H and Ctrl-W L. You can open a new file in any window using :n thirdfile.
:new
:vsplit
:split
:n thirdfile
In Vim you can open multiple buffers that contain files that are being edited all as full-screens, but it's not always easy to jump between them or to remember which are open. The MiniBufExplorer plugin shows you the list of open buffers at the top of your Vim window, and allows you to navigate between them.
Download minibufexpl.vim and move it into your ~/.vim/plugin/ directory, then open a couple of files in Vim. To fire up MiniBufExplorer, type :MiniBufExplorer. If you're using a graphical version of Vim, you can double-click on a filename to switch to it; otherwise, navigate to the MiniBufExplorer window using Ctrl-W K, then press Enter when your cursor is on the filename you want.
:MiniBufExplorer
Finally, here's a quick roundup of some other helpful tips in brief:
These scripts, plugins, and techniques should give you an idea of the potential power of Vim as a development environment. Now get started configuring Vim as the IDE of your dreams to meet your exact needs!
Allowed tags: <a> link, <b> bold, <i> italics