Gvim
Gvim
<[email protected]>
Copyright © 2002 The University Of Birmingham
Table of Contents
1. Introduction ................................................................................................................................................. 1
2. Installation .................................................................................................................................................. 2
2.1. .vimrc .............................................................................................................................................. 2
2.2. gVim - Easy ...................................................................................................................................... 2
3. Core Commands ........................................................................................................................................... 2
3.1. Modes .............................................................................................................................................. 2
3.2. Text Navigation ................................................................................................................................. 3
3.3. Inserting text ..................................................................................................................................... 4
3.4. Cutting and pasting text ....................................................................................................................... 4
3.5. Performing the same operation more than once ........................................................................................ 5
3.6. System Commands ............................................................................................................................. 5
4. Searching .................................................................................................................................................... 5
5. gVim regular expressions - Search and Replace .................................................................................................. 6
6. gVim and DocBook ...................................................................................................................................... 7
6.1. Useful key mappings ........................................................................................................................... 7
7. gVim and Java ............................................................................................................................................. 7
7.1. Automatically generate getter and setter methods ..................................................................................... 7
7.1.1. Install .................................................................................................................................... 7
7.1.2. Use ....................................................................................................................................... 7
8. References (and links you may find useful) ........................................................................................................ 8
1. Introduction
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, distri-
butions 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/.
1
Editing with gVim
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:
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.
2. Installation
Installation of gVim is as easy as following the instructions on the download page: http://www.vim.org/download.php.
2.1. .vimrc
gVim is configured via a file called .vimrc on Unix machines and a file called _vimrc on Windows machines. Here is an exam-
ple .vimrc which provides a basic working environment with syntax highlighting:
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 ma-
chines and should be removed for other systems.
3. Core Commands
3.1. Modes
gVim is a modal editor. There is some discrepancy over how many modes gVim has, some consider gVim to have two modes;
command mode, and input mode whereas some consider vi to have three modes; command mode, input mode, and 'last line' mode.
2
Editing with gVim
This tutorial will assume the latter.
• Input mode is used to input text into the editing "buffer". This means that whatever is typed appears on the screen, verbatim, as
it were. To get into input mode from command mode, press escape then press 'i'. To get into input mode from last line mode,
press escape to enter command mode then follow the instructions for entering input mode from command mode. There are
some other ways of entering input mode from command mode, these will be discussed later.
• Command mode is used to perform operations on text and to navigate quickly around the document being edited. To enter
command mode from input mode or last line mode, press the escape key.
• Last line mode is used to toggle gVim options, perform system operations such as saving, and to perform abstract operations
on text such as regular expression search-and-replace. To enter last line mode from input mode, first, press escape to enter
command mode, then, press Shift-:. To enter last line mode from command mode, just press Shift-:.
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:
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
3
Editing with gVim
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.
There are also a couple of special ways to insert text which don't really fit into the table above, these are shown in the table below:
4
Editing with gVim
gVim has support for native cut and paste where applicable. For instance, on Windows, one may use CTRL-X, CTRL-C, and
CTRL-V to cut, copy and paste text respectively. gVim also provides it's own commands for cutting and pasting areas of text.
4. Searching
1. Enter search mode by typing / from command mode. '/' will appear at the bottom of the screen.
2. Enter a regular expression to search for. Any gVim supported regular expression may be entered, this includes normal words
like "dog". Each time the user adds an extra character to the regular expression, gVim will highlight the first match it finds in
the document from the character after the cursor position from which search mode was entered. Backspacing is allowed.
5
Editing with gVim
3. Press Enter to confirm expression to search for. All instances of the expression in the document will be highlighted or if no
expression is found "Pattern not found: pattern" will be displayed.
4. Use n and Shift-n to cycle forward backwards through all matches found respectively.
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.
: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. Com-
mands 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.
• The character '%' can be used to indicate global replacement, it replaces "start_point,end_point".
• Special characters must be escaped with a backslash if their literal meaning is desired. For example, if one wants to replace "/"
one must use "\/".
6
Editing with gVim
For more complex regular expression examples, and mode general information about regular expressions, see: Regular Expres-
sions - Ashley J.S Mills [../regexp/regexphome.html].
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 [files/dbhelper.vim]. It would be nice if somebody created a comprehen-
sive 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 al-
ways 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 lan-
guage 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 in-
serted:
<para>
</para>
2. Place in ftplugins directory of gVim installation. If there are already files that begins with java, create a directory called
java in ftplugins and put all the files that begin with java in that directory.
7.1.2. Use
2. Press \-p
The getter and setter methods for the selected variables will be generated. For example, selection of the following:
private int age;
7
Editing with gVim
Followed by executing the keystroke sequence \-p, brings up the dialog box shown below:
/**
* Get age.
*
* @return age as int.
*/
public int getAge() {
return age;
}
/**
* Set age.
*
* @param age the value to set.
*/
public void setAge(int age) {
this.age = age;
}
One can modify the templates in the script to produce custom output, for example, the above output shows an indentation of 3 and
has the opening bracket of a method on the same line as the method name.
• http://octopus.cdut.edu.cn/~yf17/unix/unixcd/vi/index.htm
Online version of Learning the vi Editor by Linda Lamb and Arnold Robbins.
• http://www.thomer.com/vi/vi.html
• http://www.moesworld.com/vim/usr_toc.html