Editing with gVim


Table of Contents

1. Introduction
2. Installation
2.1. .vimrc
2.2. gVim - Easy
3. Core Commands
3.1. Modes
3.2. Text Navigation
3.3. Inserting text
3.4. Cutting and pasting text
3.5. Performing the same operation more than once
3.6. System Commands
4. Searching
5. gVim regular expressions - Search and Replace
6. gVim and DocBook
6.1. Useful key mappings
7. gVim and Java
7.1. Automatically generate getter and setter methods
7.1.1. Install
7.1.2. Use
8. References (and links you may find useful)
gVim introduction screen

Vi pronounced vee-eye was written by William Joy in around 1976 at The University of California, Berkeley. Vi was created by hacking various other editors around at the time and writing new code. Here is an interview with the creator: http://www.cs.pdx.edu/~kirkenda/joy84.html. Here is the original documenation for Vi: http://docs.freebsd.org/44doc/usd/12.vi/paper.html.

Vi is available on VMS and Unix systems, it is a small, fast, full-screen editor for terminals. Vi comes with most, if not all, distributions of Unix, Linux, and BSD. Vi is also present on other Unix-like operating systems.

VIM (VI IMproved) is a Vi clone created mainly by Bram Moolenaar, who owns the copyright. VIM is charityware, in that, it is freely distributable but the user is asked to make a donation to charity. VIM can operate in a text based environment but there is a version known as gVim which operates in a GUI environment. The latest version of gVim is available for many operating systems and has full mouse support, clipboard support is available where applicable. Here is the gVim homepage: http://www.vim.org/.

This tutorial will focus on using gVim because it is thought that the target audience will get most utility out of it compared to VIM and Vi. The basic commands are the same with VIM and Vi but Vi lacks certain functionality such as multiple levels of undo. Here are a few benefits of using gVim over Vim and Vi:

  • It has menu support which is useful for begginers.

  • It directly competes with xemacs.

  • It has an "easy" mode for begginers.

Within the file editing community there is somewhat of a feud between users of gVim and users of (ughh!) Emacs. This rivalry is not to be taken too seriously since none of the editors are THAT good, see http://www.tuxedo.org/~esr/jargon/html/entry/holy-wars.html.

This document does not intend to cover the subject matter to any significant degree. Its intent is to provide a simple introduction to editing in gVim that is consistent in style with the rest of the tutorials in this tutorial collection. Only a handfull of the most useful commands are introduced. The user is referred to the gVim manual for further information. The gVim manual comes with the gVim installation and is located in the doc directory.

Installation of gVim is as easy as following the instructions on the download page: http://www.vim.org/download.php.

gVim is configured via a file called .vimrc on Unix machines and a file called _vimrc on Windows machines. Here is an example .vimrc which provides a basic working environment with syntax highlighting:

set nocompatible	" Use gVim defaults

" set tw=80		 tw to specify a default text width
set fo=tcrq		" fo to specify default formatoptions
			" t auto-wraps text using textwidth
			" c auto-wraps comments using textwidth
			" r auto-inserts the current comment leader
			" q allows formatting of comments

   " allow backspacing over everything in insert mode
   set backspace=2

set tabstop=1           " Each Tab has 1_spaces equivalent width
set shiftwidth=2        " Indentation width when using >> and << re-indentation
set nobackup
set expandtab           " Tabs are expanded to spaces

source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
      

Notice that the configuration file "sources" an example vimrc file that comes with the gVim distribution called vimrc_example.vim; this file defines the syntax highlighting colours. The last two lines are specific to Microsoft Windows machines and should be removed for other systems.

One of the most important skills needed to use gVim efficiently is the ability to navigate a document being edited quickly and proficiently. In insert mode, "standard" navigation applies, one may use backspace to delete characters, use the mouse to move the cursor position, and use the cursor keys to move through sentences. One may also use the cursor keys to position the cursor in command mode too.

The cursor keys are intuitive and familiar but their use requires the movement of the users hands away from the 'home' keys of the keyboard thus slowing the user down. It is thus preferable to use the command mode navigation keys:

Navigating in gVim

It can be quite frustrating getting used to the keys but it is well worth the effort. There is a Unix game called hack where the aim of the game is to navigate ones way through some dungeons down to below level 20 where it is rumored that the Amulet of Yendor lies. The aim of the game is to find the Amulet and get it back out. The keys used to navigate ones character in "hack", in the four primary directions, are the same as those used by gVim hence one could use this game as a fun way to learn the navigational keys of gVim. "hack" comes with the games package of the FreeBSD operating system and is present on decent Unix systems. There is a more recent version called Nethack, which is available for many operating systems, but it does not appear to support the same navigational methods, if I am wrong about this please inform me.

Searching using gVim

[Note]Note

Thespian correctly begins with a capital 'T'.

In the image above, the first row shows gVim matching the first occurance of the expression as more characters are added. The first image of the second row shows the string "the" has been highlighted (backspace was pressed twice from the last image of the first row). The second image of the second row shows the effect of pressing enter to confirm the expression to search for; all the instances of the expression have been highlighted. The last image of the second row illustrates the pressing of n to cycle through the instances of the expression found.

Regular expressions are one of the most powerful tools available to the gVim user; they can be used to search and replace on sections of text, saving the user much time. The general form of a gVim Search and Replace expression is shown below:

:start_point,end_points/search_pattern/replacement_pattern/g

The start point can either be an absolute line number, or be relative to the current line. The same applies for the end point. Commands are entered from last line mode. The 'g' at the end is optional, if it is ommited only the first occurance of the pattern will be replaced.

For more complex regular expression examples, and mode general information about regular expressions, see: Regular Expressions - Ashley J.S Mills.

One can write DocBook documents at an incredably faster rate if one maps element entry to key bindings. A directory called xml was created in the ftplugins directory of the gVim installation. Into this was placed a vim file that contained macros to map key combinations to element insertions. Comma preceeds each mapping, this is convenient because if the user types comma followed by space, nothing happens, but if the user types comma followed by one of the mapped key combinations an element is inserted. Most of the mappings are intuitive, for example, ulink is mapped onto ,-u-l. The mappings are very easy to customise and the improvement in document creation speed is amazing.

I wanted to go through the file and add mappings for every single element, and make the mappings more intuitive since I felt that the previous mappings, on occasion, deviated from intution somewhat. I have not done this yet.

I am not the creator of the file, this information is specified in the file header. I modified some of the mappings from the original and added some new ones, here is the file: dbhelper.vim. It would be nice if somebody created a comprehensive version of this file with mappings for every tag installed, let me know if you do.

Download the file (on Windows) to where/you/installed/gvim/vimXX/ftplugin/xml, where XX is the version number with a dot removed, for example 61. On Unix variants, save the file to your home directory and add something like the following to your .vimrc file:

augroup BEGIN
   au! BufRead,BufNewFile *.xml source ~/dbhelper.vim 
augroup END

Next time you use gvim to edit an XML file dbhelper.vim will be sourced and the mappings enabled. Of course, it may not always be desireable to have such mappings enabled for every XML file but different mappings for different types of XML could be sourced manually from gvim by going into lastline mode and typeing "source fileName". Or one could use the gvim macro language to write something which scans the XML doctype and loads the appropriate helper file.

The mappings are used in insert mode by typing ',' followed by the mapping chars, typing ",pp" causes the following to be inserted:

<para>
</para>
      

Usually the cursor will be appropriately positioned.