gvim.pdf

(66 KB) Pobierz
Editing with gVim
Ashley J.S Mills
<ug55axm@cs.bham.ac.uk>
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:
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.
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:
set nocompatible " Use gVim defaults
" set tw=80
set fo=tcrq
tw to specify a default text width
" 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
set
set
set
tabstop=1
shiftwidth=2
nobackup
expandtab
" Each Tab has 1_spaces equivalent width
" Indentation width when using >> and << re-indentation
" 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 ma-
chines and should be removed for other systems.
2.2. gVim - Easy
gVim-Easy, which is installed with gVim, has all the functionality of normal gVim but lacks modes. This is especially useful for
begginers and people who do not want to, or do not have the time to, learn how to use gVim. Users can benefit from gVim's supe-
rior syntax highlighting and auto-indentation while not having to have to learn the, often deemed complex, command set of gVim
in order to edit a simple document. It is recommended that readers of this tutorial at least try to learn how to use gVim in normal
mode, the learning curve is steep, but, the benefits in speed and usability this confers is worth the investment.
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-:.
3.2. Text Navigation
One of the most important skills needed to use gVim efficiently is the ability to navigate a document being edited quickly and pro-
ficiently. 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:
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.
Table 1. gVim Navigation
Key sequence
k
j
h
l
b
e
Shift-h
Shift-l
Shift-]
Shift-[
Effect
move cursor up
move cursor down
move cursor left
move cursor right
move cursor to beggining of current word
move cursor to end of current word
move cursor to top of current screen
move cursor to bottom of current screen
move cursor to beggining of next paragraph
move cursor to beggining of previous paragraph
3.3. Inserting text
Besides basic navigation, there are other important concepts; entering insert mode from command mode can be accomplished in a
number of different ways, depending on where one wants to begin inserting characters. The table below shows some of the ways
to enter insert mode, the key sequences specified must be performed from
command mode.
Table 2. Entering insert mode
Key sequence
i
a
Shift-i
Shift-a
o
Shift-o
g-i
Effect
insert text before cursor position
insert text after cursor position
insert text at beginning of line (before first blank)
insert text at end of line
insert text after current line (creates new line)
insert text before current line (creates new line)
insert text at last insert position
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:
Table 3. Entering insert mode
Key sequence
r-AnyCharacter
x
s
Shift-r
Effect
replace character at cursor position (stays in command mode)
deletes character at cursor position (also copies it to clipboard)
deletes character at cursor postion then enters insert mode at
that position
enters
replace
mode where every character typed overwrites the
character under the current cursor position
3.4. Cutting and pasting text
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.
Table 4. Cutting and pasting in gVim
key sequence
Shift-Y
d-d
p
Shift-p
y-w
d-w
p
Shift-p
Effect
'yank' current line (copies current line)
deletes current line (and also copies it to clipboard)
paste yanked line(s) after current line
paste yanked line(s) before current line
'yank' from cursor position to beggining of next word
delete from cursor position to beggining of next word
paste yanked word(s) after current cursor position
paste yanked word(s) before current cursor position
3.5. Performing the same operation more than once
Most of the gVim commands can be prefixed with an integer to specify that the command should be performed the number of
times indicated by the integer, some examples are shown below.
Table 5. Examples of [count] prefixed command usage
Key sequence
5-j
3-d-w
5-Shift-y
5-p
3-s
Effect
move cursor down 5 lines
delete from cursor position to beggining of third word after cur-
rent word
'yank' 5 lines from current line (current line counts as one of the
5)
paste the yanked text 5 times (in manner depending on whether
lines or words were yanked)
delete the three characters from current cursor position and en-
ter insert mode (substitute)
3.6. System Commands
gVim has menus for performing system operations such as opening files and saving files and so on, however, the system com-
mands are usually quicker. Opening a file from the menu is preferable when one finds it quicker to find the file through the edit-
file dialog box than to specify it at the command line. The commands below are all entered in last line mode hence the prefixed ':'.
Table 6. System commands
Key sequence
:w
:w
filename
:e
filename
Effect
write the current file
write the current file as
filename
edit
filename
, file does not need to exist
4. Searching
1.
2.
Enter search mode by typing
/
from command mode. '/' will appear at the bottom of the screen.
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
Zgłoś jeśli naruszono regulamin