vimbook-Tutorial.pdf

(3760 KB) Pobierz
vim book
The Tutorial
1
Basic Editing
2
Editing a Little Faster
3
Searching
4
Text Blocks and Multiple Files
5
Windows
6
Basic Visual Mode
7
Commands for Programmers
8
Basic Abbreviations, Keyboard Mapping, and Initialization Files
9
Basic Command-Mode Commands
10
Basic GUI Usage
11
Dealing with Text Files
12
Automatic Completion
13
Autocommands
14
File Recovery and Command-Line Arguments
15
Miscellaneous Commands
16
Cookbook
17
Topics Not Covered
184432580.002.png
184432580.003.png
Basic Editing
T HE V IM EDITOR IS ONE OF THE MOST powerful text editors around. It is also
extremely efficient, enabling the user to edit files with a minimum of keystrokes.This
power and functionality comes at a cost, however:When getting started, users face a
steep learning curve.
This chapter teaches you the basic set of 10 Vim commands you need to get started
editing. In this chapter, you learn the following:
n
The four basic movement commands
n
How to insert and delete text
n
How to get help (very important)
n
Exiting the editor
After you get these commands down pat, you can learn the more advanced editing
commands.
Before You Start
If you have not installed Vim , you need to read Appendix A, “Installing Vim ,” and
install the editor.
184432580.004.png
4
Chapter 1 Basic Editing
If you are running on UNIX, execute the following command:
$ touch ~/.vimrc
By creating a ~/.vimrc , you tell Vim that you want to use it in Vim mode. If this fi le is
not present, Vim runs in Vi -compatibility mode and you lose access to many of the
advanced Vim features. However, you can enable the advanced features from within
Vim at any time with this command: :set nocompatible<Enter> .
If you are running on Microsoft Windows, the installation process creates the
Microsoft Windows version of this file, _vimrc , for you.
Running Vim for the First Time
To start Vim , enter this command:
$ gvim file.txt
Note that the $ is the default UNIX command prompt.Your prompt might differ.
If you are running Microsoft Windows, open an MS-DOS prompt window and
enter this command:
C:> gvim file.txt
(Again, your prompt may differ.)
In either case, Vim starts editing a file called file.txt . Because this is a new fi le, you
get a blank window. Figure 1.1 shows what your screen will look like.
The tilde (~) lines indicate lines not in the file. In other words, when Vim runs out
of file to display, it displays tilde lines. At the bottom of a screen, a message line indi-
cates the file is named file.txt and shows that you are creating a new file.The mes-
sage information is temporary and other information overwrites it when you type the
first character.
~
~
~
~
~
~
~
~
“file.txt” [New File]
Figure 1.1 Initial Vim window.
184432580.005.png
Editing for the First Time
5
The vim Command
The gvim command causes the editor to create a new window for editing. If you use
the command vim , the editing occurs inside your command window. In other words, if
you are running inside an xterm, the editor uses your xterm window. If you are using
an MS-DOS command prompt window under Microsoft Windows, the editing occurs
inside the window. Figure 1.2 shows a typical MS-DOS command prompt window.
A very intelligent turtle
Found programming UNIX a hurdle
The system, you see,
Ran as slow as did he,
And that's not saying much for the turtle.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"turtle.txt" 5L, 158C
1,1
All
Figure 1.2 Editing with the vim command in an MS-DOS window.
Modes
The Vim editor is a modal editor.That means that the editor behaves differently,
depending on which mode you are in. If the bottom of the screen displays the file-
name or is blank, you are in normal mode. If you are in insert mode, the indicator dis-
plays --INSERT-- ; and if you are in visual mode, the indicator shows --VISUAL-- .
Editing for the First Time
The next few sections show you how to edit your first file. During this process, you
learn the basic commands that you have to know to use Vim . At the end of this lesson,
you will know how to edit—not fast, not efficiently, but enough to get the job done.
Inserting Text
To enter text, you need to be in insert mode.Type i , and notice that the lower left of
the screen changes to --INSERT-- (meaning that you are in insert mode).
184432580.001.png
Zgłoś jeśli naruszono regulamin