Ivor O’Connor

March 1, 2009

gvim tips

Filed under: Linux, ubuntu, Uncategorized, vi — Tags: , , — ioconnor @ 4:53 pm

This post will be updated every time I have to do something in vim taking more than a few seconds to get right. Or when I find interesting reference information I don’t know or forget on a regular basis. The idea is to include everything I do and need with vim in this post.

I prefer using gvim for some reason. Don’t really have a reason I can remember. The useless toolbar on top can be turned off with:

set guioptions-=T

The most useful command in all of vi/vim land I have ever used is the following held within the .vimrc or whatever. It allows the line under the cursor to be executed. With clever useage vi becomes very useful. I’ve used it on all versions of vi for decades.

:map ** “zyy:@z

along with the following that needs to sometimes be modified heavily depending on the version of vi. This allows you to bounce between the two buffers with just a keystroke. (Removing the ability to use the standard ‘;’ for what it was intended for.)

:map ; :e#

When viewing a downloaded google spreadsheet in csv format of all deposits the following displays the summary of each deposit


Removes the first 7 words of each line and replaces them with “rm “. Helpful when editing a file made with “ls > file”.

:%s/\(\S\+\s\+\)\{7\}\(.*\)/rm \2 /

Using vim’s internal sort. Start by marking the beginning and ending line with say ‘b’ and ‘e’. (Note the ‘}’ does not help in specifying range unfortunately.) Then type:


I’m always forgetting vim’s regexps metacharacters. The following I stole from this excellent guide http://www.geocities.com/volontir/

. any character except new line
\s whitespace character
\S non whitespace character
\d digit
\D non-digit
\x hex digit
\X non-hex digit
\o octal digit
\O non-octal digit
\h head of word character (a,b,c…z, A,B,C…Z and _)
\H non-head of word character
\p printable character
\P like \p, but excluding digits
\w word character
\W non-word character
\a alphabetic character
\A non-alphabetic character
\l lowercase character
\L non-lowercase character
\u uppercase character
\U non-uppercase character

Non-greedy quantifiers where n and m are positive integers (>0):

\{-} matches 0 or more of the preceding atom, as few as possible
\{-n,m} matches 1 to m of the preceding characters…
\{-n,} matches at least 1 or more of the preceding characters…
\{-,m} matches 1 or more of the preceding characters…

The greedy options have all the obvious non-greedy options plus the semi-obvious extras as follow

\{n} Matches exactly n characters. It is not greedy or non-greedy.
* Greedily matches 0 or more of the preceding whatever. “.*” matches everything including an empty line. Notice there is no backslash before the ‘*’
\+ Matches 1 or more of the preceding whatever.
\= Matches 0 or 1 of the preceding whatever.

There are valuable replacement options:

& The entire part matched by the pattern.
\1 When 1 is used it is the first pattern. A 0 is the same as the & above and probably makes more sense. I don’t know if two digit numbers can be used or if it stops at 9.
~ The previously substituted string.
\L \U \E \e Turns upper or lower case on until either of the end control characters are encountered.
\r Splits the line in two by adding a return character.
\l \u Makes the next character upper or lower case.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: