Tuesday, January 27, 2009

Ignore the Modified Flag

There are times when I'm working on a temporary buffer, and I want to do something like a `gf' (go to file), but Vim insists that the buffer is modified and refuses to abandon the file. You can get around this by telling Vim the file isn't modified.

:set hidden

This effectively makes Vim treat the file like it's already saved. Please avoid using this for any production files as it can make Vim think that the file on disk is different than it actually is.

Make Windows Equal Height

If you have a bunch of split windows of disproportionate sizes, you can make them all equal by doing a `ctrl-w =' in normal mode.

Tuesday, January 20, 2009

Converting Tabs to Spaces

Here's a quick way to convert a tab-stopped file to a space-formatted file.

:set expandtab
:retab!

Thursday, January 15, 2009

Last Changed Text

Here are two helpful commands to get to the last changed or yanked text.

`[ - First character of previously changed or yanked text
`] - Last character of previously changed or yanked text

Wednesday, January 14, 2009

Extending History

You can allow Vim to store more history than the default (last 20 commands) by setting history as follows.

:set history=N (where N is how much history you want to keep)

Tuesday, January 13, 2009

Delete to End of Page

Typing `dL' in normal mode will delete to the end of the current page. In other words, everything visible to the bottom of the window.

Monday, January 12, 2009

Remap Backtick

I find it convenient to remap the backtick key "`" to single-quote "'". Functionally, they are very similar. Backtick takes you to an exact mark location while single-quote simply takes you to the beginning of the marked line. I don't use the single-quote functionality much, and it's much easier to reach than backtick, so the following mapping saves some finger stretching.

nnoremap ' `
nnoremap ` '

Saturday, January 10, 2009

Reformat a Paragraph

Say you have the following text:

> someone said
> > someone else said
> > > long quoted message
> > blah blah
> blah

Place the cursor on the first line and do `gqip'. I often use this combo to conveniently reformat paragraphs.

Friday, January 9, 2009

Modelines

Vim allows you to insert modelines to specify options you'd like for a specific file. For example, you could have a perl script as follows:

#!/usr/bin/perl
# vim: set expandtab:
use strict;

This would tell Vim to expand tabs in the current file. See :help modeline for more info.

Thursday, January 8, 2009

Removing Ctrl-M

There are quite a few ways of doing this, but a quick way to remove ^M scattered around your file is as follows:

:%s/C-vC-m//g

Where C-v is Ctrl-v and C-m is Ctrl-m.

Wednesday, January 7, 2009

Command on All Windows

Here's a neat trick. If you want to execute an ex command on all windows, you can do the following:

:windo command

so...

:windo %s/foo/bar/g would find and replace foo with bar in all windows.

Monday, January 5, 2009

Only RSS Vim Tips

For those of you that would prefer to exclude any tips from this blog that aren't directly related to Vim from your RSS, I have an easy solution. Blogger allows feed URLs to be filtered based on tags. I only use the vim tag on posts directly related to Vim, so the following RSS URL should be a convenient solution.

http://dailyvim.blogspot.com/feeds/posts/default/-/vim