Vim functions to change tab treatment

January 22, 2012 at 04:00 PM | Vim | View Comments

I've gotten tired of doing the :setlocal tabstop=... dance every time I start editing a file from someone who has their own unique feelings about how wide tabs should be... So I've written two little functions which make it easy to change a buffer's tab mode:

For example, when I start editing something written by someone who believes spaces are evil and tabs should be five spaces wide, I just need to :call HardTabs(5).

Permalink + Comments

You and Your Editor: Vim normal mode commands g, : and d (3 of N)

December 05, 2009 at 08:22 PM | Vim | View Comments

In my last post, I showed off some normal-mode data from vim-logging. In this (and the next few) posts, I'll go though my most-used commands and describe how I use them.

(Don't use Vim? This post won't be too interesting… Although you may pick up something useful)


My logs contain more than 60,000 references to the 'g' command (eight times more than ':', the next most frequently used command). This may seem surprising at first, but stick with me and let me explain: the 'g' command is something of a 'gateway' command - a prefix for a number of common actions which don't have their own single-character command (for example, gd, goto definition and g?, rot13 encode).

Here is a breakdown of the 'g' suffixes I often use:

  • gj and gk: cursor to next row, cursor to previous row ("make j and k do the right thing"). The j and k commands are defined as "move to next line" and "move to previous line", not "move to next screen row" and "move to previous screen row". This is an important distinction to make when long lines are wrapped, and I find "move to next/previous screen row" is a more reasonable default. Protip: use noremap j gj and noremap k gk in your vimrc.

  • gg: cursor to top of file. After using gg (for example, to edit some import statements or add a shebang line), I often use '' (tick-tick) to jump back to the line I was editing at the time (for example, ggofrom foo import bar<esc>''). Also, I often use ggVG (go to top, enter line-visual-mode, go to bottom) to select the entire file. From there, I can pipe it to something (for example, :!sort), format it (gw) or copy it to the clipboard.

  • gt and gT: go tab - move to next/previous tab.

  • gf: goto file - edit the file under the cursor. For example, if the cursor is over the 'e' in import "../", the equivalent of :e ../ will be executed.

  • gw: go wrap. Wrap the selected text (that is, text selected in visual mode). Try selecting a long line (pressing V), then using gw to wrap it.

That's all the 'g' commands I can think of for now... The next version of vim-logging will log the sub-command used, so I will be able to provide better data in the future.


The colon command is fairly obvious: enter command-line mode to run commands like :w or :help :.

I'll write more about this later, when I analyze my most-used ex commands.


Ah, d - delete - the programmers best friend.

As with g, I do not have detailed information about how I use d, but I'll list the first few combinations that come to mind when I move my left index finger over the d key:

  • dd to delete whole lines
  • di( to delete everything inside a pair of parenthesise
  • dfx to delete everything between the current position and the next occurrence of 'x'
  • dj and dk to delete this and the next/previous line
  • d$ to delete from the current position to the end of the line
  • d in visual mode to delete the selected text.
Permalink + Comments

You and Your Editor: Data from vim-logging (2 of N)

December 05, 2009 at 07:19 PM | Vim | View Comments

Remember when I blogged about vim-logging? Well, I've finally gotten around to interpreting the results.

First, I will cover what I believe to be the most important thing to know about vim: the normal mode commands.

Below is a chart of my 39 (why 39? I can't remember - when I created the graph, that's the number I chose) most-frequently-used normal-mode commands:

(many - see discussion)g
(ex mode command):
(find in line)f
(move forward word)w
(insert mode at beginning of line)I
(move to beginning of line)^
(insert mode at end of line)A
(move back word)b
(move to next search match)n
(move left)h
(z[nm]): close/open folds, z[tbz]: scroll window)z
(insert mode before character)i
(move to end of line)$
(c_w-c_[wljh_] - window commands)Ctrl_W
(insert mode after character)a
(find backwards in line)F
(insert node in next line)o
(move cursor half screen down)Ctrl_D
(move line down)j
(move line up)k
(enter normal mode)ESC
(repeat previous find in line);
(substitute character)s
(delete character)x
(put from register)p
(delete line)D
(}} - move to next blank line)}
({{ - move to previous blank line){
(enter visual-line-mode)V
(go to bottom of file)G
(visual mode)v
(yank text)y
(change line)C
(insert mode on previous line)O
Permalink + Comments

vim-logging: taking the superstition out of "most used command"

October 17, 2009 at 03:33 PM | Vim | View Comments

After writing the last entry, You and Your Editor, I got wondering… What commands do I really use most often?

A few hours of hacking (and a bit of help from Blake Winton) later, I'd like to present: vim-logging.

You can expect a blog post in a week or two, detailing what I've discovered.

Permalink + Comments

You and Your Editor: The Bare Minimum You Should Be Doing (1 of N)

October 15, 2009 at 12:26 AM | Vim | View Comments

It's come up a few times, so I'll just go and post it here: the things, in no particular order, I believe to be the bare minimum you should be able to do while you're editing source code (and how to do them in Vim):

Easily move between files

All but the simplest scripts require more than one file, so you'll often need to switch between them.

Eclipse has two very useful features here: Quick Access (command+3) and Open Resource (command+shift+r):

quick accessopen resource

And in Vim, I make heavy use of multiple windows (:new and :vnew, using custom shortcuts, c-h, c-j, c-k and c-l to move between them) and multiple buffers, using :b. For example, below, I have typed :b url<tab>, then I use c-p and c-n to cycle through the list of matching buffers (I also have <right> and <left> bound to :bn and :bp, making it easy to flip through buffers if I'm not sure what I want).

windows and buffers in Vim


When you're working with a large project, especially one for which you aren't the only author, it can become nontrivial to figure out where things (classes, functions, etc) are defined. I've found that it's very hard to read source code without a jump-to-definition function because it forces a mental context switch from "figuring out what this code does" to "figuring out where this method is defined", then once I've found where it's defined, "ok, now what was I looking for again?", and once I've finished reading the definition "why am I looking here again?" and finally to "what was the point of this sentence?"

With Vim, I use exuberant ctags to do this.

First, I run ctags -R . to build a tags file, then I access it using :tag, c-] and c-w ] from within Vim. Also useful is c-t: after c-] is used to examine a definition, c-t will take you back to the place you had originally found that definition. Very, very useful.

tags in Vim

(note, for this to work properly, make sure :set tags=tags;/ is set in Vim)

Search for/highlight the current word

Often, when I'm reading code, I'll want to quickly look through all the references to some word in the current file (for example, to see where a variable is referenced or how a function is called). This sounds trivial, but I find myself doing it often. Very often. So give it a try before immediately disregarding it.

I do this in Vim using * and # and :set hlsearch (as a side note, I've also got \\ bound to :set nohlsearch).

For example, if my cursor is over load_BufferedImage, then I hit *, I can quickly jump through all the tests which call it.

Using '*' to search

That's all… For now

That's all for now, but stay tuned – I do love editing text, so I'll probably come back and write some more on this later.

PS: Just to make very sure it's clear, the only editors I mean to bash here are the ones which make it very hard to do the things I've talked about (nano, notepad, etc)… Every decent editor can do these things, you've just got to learn how.

PPS: If you don't already use it, Vim has a great built-in help system. Access it with :help {subject}. For example, :help vnew or :help <c-w>.

PPPS: Todd has posted a followup, Worthwhile vim tips – they are a bit more basic, but even more important than the things I've talked about here.

Permalink + Comments