Archive for the ‘Vim’ Category

Counting occurrence of a particular word in Vim

Wednesday, January 28th, 2009

There is no built in word count feature in Vim, but a simple use of the substitute command can be used for the same purpose.

:%s/word//gn

Vim returns something along the lines of:

14 matches on 47 lines

Basically we are performing a search and replace but telling Vim not to take any action. If we break down the command we can see how it works.

:s – is the substitute command (find and replace)

% – putting the ‘%’ in front tells Vim to substitute across the whole file (not just this line)

<word> – this is the word we are counting (it could also be a regex expression, see below)

g – the ‘g’ switch tells Vim to search the whole line (globally) and not stop after the first match

n – the ‘n’ switch tells Vim to take ‘N’o action. This is what we want as we are only counting.

Using  regex:

Usually this command counts all occurrences of the word, but we can make it more useful with a sprinkling of regex. Say you want to count all lines that begin with comments (shell style):

:%s/^#//gn

Or all lines that are empty or only contain whitespace:

:%s/^\s*$//gn

The source of this tip is from the tips built into the Vim online help. If you enter the following command, you will get the help page version of what I have documented here (took me a while to track this down inside Vim).

:h count-items

Using Vi commands to control your shell (ksh, Bash)

Saturday, September 13th, 2008

Recently I was forced to use the Korn Shell (or ksh) when I had to perform some minor tasks on an AIX server. So of course I just jumped in and started using the shell. A few seconds later I was thoroughly frustrated as I discovered I had no command history and couldn’t use backspace or command completion (as you can probably guess I’m a Bash user).

The Vi mindset

So begrudgingly I resorted to a few web pages to work out what was going on. To my pleasant surprise, all I had to do was switch my thinking from Bash to Vi. I’m a big Vi(m) fan and feel quite at home using Vi like commands so I was quite happy to discover this was all I had to do. Command history: just hit ESC and use the Vi “up” which is “k” to go back in history, or Vi “down” which is “j” to go forward in history – easy.

Keeping in this mindset I began investigating other commands. Jumping back in history, then a quick “cw” to change a word, “A” to append to the end of that command. I wont bore all the people that don’t use Vi(m) with a list of commands, because if you don’t, then all this probably sounds like a pretty painful way of using a shell. But if you do use Vi(m) regularly, you quickly learn that a few carefully selected keystrokes can save you many repetitive backspaces or left arrows etc.

Escape from home

The other great thing about using Vi(m), or a Vi like interface, is that your fingers never need to leave the home keys. The only exception to this is that pesky ESC key that has been banished to the far corner of the keyboard. But never fear, there are some fixes to this.

The answer is to remap your CapsLock key to be another Ctrl key. Not only does this open up a wealth of shortcuts that can be performed without taking you fingers away from the home keys, but it also allows you to type an ESC equivalent, which is Ctrl-[.

If you are running X11 you can do it in the (re)configuration. Running Gnome you can do it through keyboard preferences, I’m guessing there is something similar in KDE and other window managers. You can even do it in Windows with a simple registry hack.

More information for Linux, Mac and Windows at the below links:

http://www.manicai.net/comp/swap-caps-ctrl.html

http://johnhaller.com/jh/useful_stuff/disable_caps_lock/

Dessert time

Anyway, whilst I was pretty impressed with the interface that ksh had on offer (once I understood it), I wasn’t totally sold. Command line completion was there (ESC+\) but it was nowhere near the level of Bash’s usefulness.

But…..inspired by this discovery, it didn’t take long to work out that I could have my cake and eat it too! A simple command in Bash allows these Vi like commands to be used:

set -o vi

This way you can keep on using Bash as you would, but hit the ESC key (or Ctrl-[) and you have the power of Vi at your fingertips!

Color coded man pages with Vim

Sunday, March 23rd, 2008

Want a color coded man page viewer with hyperlink support? Well look no further. This simple script is a wrapper around the built in :Man function in Vim. Basically it allows Vim to be started up in man mode and adds some key shortcuts like the default man page viewer.

Have a look at this screenshot, it shows the color coding of various elements, hyperlinks for other commands and also shows the folding feature.

Using vim for man page viewing

Enabling the man plugin

Before we begin, we need to make sure the filetype plugin for man is enabled in Vim. To do this we need to source $VIMRUNTIME/ftplugin/man.vim from our .vimrc file.

I have the following line in mine:

source /usr/share/vim/vimcurrent/ftplugin/man.vim

Your location may vary depending on versions/distributions etc.

Using the :Man function

Once we have enabled the plugin, we can view man pages in Vim using the :Man command (i.e. :Man vim). We can now also use the below command in place of our standard man pager.

vim -R -c “:set foldmethod=indent” -c “:set foldnestmax=2″ -c “:set foldenable” -c “:Man $1″ -c “:normal ” -c “:q” -c “:set nomodifiable” -c “:map q :q<CR>” -c “:map <space> “

Place this command inside a shell script, put it on your path and/or set up an alias to it like follows:

alias man=”/home/user/scripts/vimman.sh”

Note: the above command has some non-printing characters in it, such as “Control-W” and “Page-down”. As such, just copying the text from a web browser will probably not work. You can checkout the full script vimman.sh at omobos.com

Please explain…..

The above command is pretty simple if we break it down a little. To start with the ‘-R’ option puts us in read-only mode. The rest of the arguments are actually Vim commands, this is indicated with the “-c” option. The first three relate to folding and can be removed if you prefer not to have folding enabled. Following that we have our :Man command which opens the man page in a new split window. To remove the split window, we jump to the other window (“Control-W. Control-W” – not printed correctly above) and then execute “:q” to close it. The last two commands set up some keyboard shortcuts. Pressing “q” quits immediately and pressing “Space” gives us a “Page-Down”.

Hyperlinks

When viewing man pages in Vim, other referenced commands are highlighted (in blue usually). By placing the cursor on the command name and pressing Ctrl-] you will jump to the man page of that command. Ctrl-T will jump back to the original page.