FileType-Specific Settings

Vim can run commands when reading or writing files, and this is known as an autocommand. Automatic commands can be combined with FileType and set to apply settings that only apply to a certain type of file. This takes the form au FileType type set commands. For example, to ensure Ruby files wrap at 80 characters and use a tabstop of two the following line could be added to your ~/.vimrc:

au FileType ruby set tw=80 ts=2

The {options} argument to :set can be repeated, and here I've supplied two arguments. This pattern is really great for those of us who work with multiple languages with different styles.

I like this example taken from Ryan Tomayko's dotfiles/.vimrc:

Read More →

Script Roundup: Vitra, Cowsay

Send in your Vim scripts for review through our contact form or @vimnews.

Vitra

Vitra (GitHub: nsmgr8 / vitra, License: MIT) by M. Nasimul Haque is a Trac interface for Vim. It communicates with a Trac server using XML-RPC, and displays wiki pages and tickets in an elaborate Vim-based interface.

Typing :TTOpen will open the ticket UI, which displays a list of tickets and full details on the selected ticket. If your project has a lot of tickets, the list can be paginated through, sorted, ordered, and filtered. Tickets can also be created and updated. The wiki interface has similar options.

cowsay.vim

cowsay.vim (License: Vim) is a wrapper for cowsay that can be combined with TComment to litter your code with comical cows.

Read More →

Vim 101: Repeating Commands

Hint: To make your Vim look the same as my screenshots, download usevim-vimrc and run Vim with vim -u usevim-vimrc.

Most types of commands can be repeated in Vim. Mastering the basic ways to repeat commands is a good way of building up your skills to prepare for another killer feature: macros.

First, the most commonly used repeat command is probably n. In Normal mode search for text with /, then press n to repeat the search. N will do this in reverse.

Search and repeat with n

Read More →

Review: Vim for iOS

Vim for iOS

Vim by Applidium (GitHub: applidium / Vim, License: Vim) is an open source Vim port for iOS. I realise that might sound impractical, but Vim's modal nature means it can actually work fairly well with touchscreen keyboards. It's not the ideal interface, but it's at least possible to reuse some of your hard-won Vim skills in a mobile context.

If you're a heavy Android/iOS user like me, then you'll already have at least one SSH client installed. Therefore you've probably already used Vim with your mobile device. This native port, however, means files can be edited locally.

Read More →

Script Roundup: Buffet, OnSyntaxChange

Send in your Vim scripts for review through our contact form or @vimnews.

Buffet

Buffet

Buffet (GitHub: sandeepcr529 / Buffet.vim) by "Sandeep CR" is an alternative way of viewing buffers. It has commands for opening buffers in split windows, tabs, or closing the window associated with the buffer.

OnSyntaxChange

OnSyntaxChange (GitHub: vim-scripts / OnSyntaxChange, License: Vim) by Ingo Karkat will generate events when the cursor moves into text that matches a particular syntax group. This could be combined with an :autocmd for executing commands that coincide with the syntax group.

Read More →

XVim

I work as a freelance Objective-C developer (amongst other things), so I find myself butting heads with Xcode on a regular basis. Recent releases have integrated several separate applications that betrayed Mac OS's NeXT legacy, which means Xcode now feels almost the exact opposite of Vim.

Despite the frustrations of working with Xcode, it does do some things fairly well, and I find myself switching between my favoured Vim/tmux setup and Xcode.

Read More →

Vim 101: Blockwise Visual Mode

Last week I wrote about the virtues of Visual mode in Vim 101: Visual Mode. This is the gateway to an entire class of powerful text manipulation techniques. One aspect that I find myself using regularly in my daily coding chores is blockwise Visual mode.

Blockwise Visual mode is started by typing CTRL-V when in Normal mode, and is indicated by -- VISUAL BLOCK -- in the status line. This is usually performed at the start of a line, or perhaps somewhere in the middle:

Blockwise selection

Read More →

Using a Mouse with Vim

Hint: To make Vim behave exactly as it does in this post's video, download mouse-vimrc and run Vim with vim -u mouse-vimrc.

Console Vim works shockingly well with the mouse. Selections can be made, windows resized -- all without using gVim or MacVim. I've written about training yourself to use hjkl instead of the arrow keys before, and while I'd encourage people to learn the window management keys, the mouse options are worth trying out.

There are two important options: mouse and ttymouse, and both have associated documentation (:help 'mouse', :help 'ttymouse'). The first option enables the mouse, and I usually set this to a which means the mouse will work in the Normal, Insert, Visual, and Command-line modes.

The other option, ttymouse, changes what "mouse codes" Vim will recognise. I found xterm2 worked well with iTerm2 in Mac OS, but I was unable to get Lion's standard Terminal to work.

Read More →