annotate runtime/doc/usr_10.txt @ 12681:ebdb4ea1386e

Added tag v8.0.1218 for changeset 429bf1b9292f432f7454fe2d7d444d14292a339e
author Christian Brabandt <cb@256bit.org>
date Thu, 26 Oct 2017 16:45:05 +0200
parents 9f48eab77d62
children 1174611ad715
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10198
9f48eab77d62 commit https://github.com/vim/vim/commit/bb76f24af2010943387ce696a7092175b4ecccf2
Christian Brabandt <cb@256bit.org>
parents: 5294
diff changeset
1 *usr_10.txt* For Vim version 8.0. Last change: 2006 Nov 05
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 VIM USER MANUAL - by Bram Moolenaar
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 Making big changes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 In chapter 4 several ways to make small changes were explained. This chapter
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 goes into making changes that are repeated or can affect a large amount of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 text. The Visual mode allows doing various things with blocks of text. Use
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 an external program to do really complicated things.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 |10.1| Record and playback commands
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 |10.2| Substitution
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 |10.3| Command ranges
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 |10.4| The global command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 |10.5| Visual block mode
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 |10.6| Reading and writing part of a file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 |10.7| Formatting text
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 |10.8| Changing case
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 |10.9| Using an external program
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 Next chapter: |usr_11.txt| Recovering from a crash
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 Previous chapter: |usr_09.txt| Using the GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 Table of contents: |usr_toc.txt|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 *10.1* Record and playback commands
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 The "." command repeats the preceding change. But what if you want to do
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 something more complex than a single change? That's where command recording
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 comes in. There are three steps:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 1. The "q{register}" command starts recording keystrokes into the register
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 named {register}. The register name must be between a and z.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 2. Type your commands.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 3. To finish recording, press q (without any extra character).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 You can now execute the macro by typing the command "@{register}".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 Take a look at how to use these commands in practice. You have a list of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 filenames that look like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 stdio.h ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 fcntl.h ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 unistd.h ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 stdlib.h ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 And what you want is the following:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 #include "stdio.h" ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 #include "fcntl.h" ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 #include "unistd.h" ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 #include "stdlib.h" ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 You start by moving to the first character of the first line. Next you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 execute the following commands:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 qa Start recording a macro in register a.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 ^ Move to the beginning of the line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 i#include "<Esc> Insert the string #include " at the beginning
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 of the line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 $ Move to the end of the line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 a"<Esc> Append the character double quotation mark (")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 to the end of the line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 j Go to the next line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 q Stop recording the macro.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 Now that you have done the work once, you can repeat the change by typing the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 command "@a" three times.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 The "@a" command can be preceded by a count, which will cause the macro to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 be executed that number of times. In this case you would type: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 3@a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 MOVE AND EXECUTE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 You might have the lines you want to change in various places. Just move the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 cursor to each location and use the "@a" command. If you have done that once,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 you can do it again with "@@". That's a bit easier to type. If you now
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 execute register b with "@b", the next "@@" will use register b.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 If you compare the playback method with using ".", there are several
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 differences. First of all, "." can only repeat one change. As seen in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 example above, "@a" can do several changes, and move around as well.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 Secondly, "." can only remember the last change. Executing a register allows
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 you to make any changes and then still use "@a" to replay the recorded
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 commands. Finally, you can use 26 different registers. Thus you can remember
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 26 different command sequences to execute.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 USING REGISTERS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 The registers used for recording are the same ones you used for yank and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 delete commands. This allows you to mix recording with other commands to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 manipulate the registers.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 Suppose you have recorded a few commands in register n. When you execute
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 this with "@n" you notice you did something wrong. You could try recording
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 again, but perhaps you will make another mistake. Instead, use this trick:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 G Go to the end of the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 o<Esc> Create an empty line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 "np Put the text from the n register. You now see
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 the commands you typed as text in the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 {edits} Change the commands that were wrong. This is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 just like editing text.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 0 Go to the start of the line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 "ny$ Yank the corrected commands into the n
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 register.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 dd Delete the scratch line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 Now you can execute the corrected commands with "@n". (If your recorded
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 commands include line breaks, adjust the last two items in the example to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 include all the lines.)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 APPENDING TO A REGISTER
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 So far we have used a lowercase letter for the register name. To append to a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 register, use an uppercase letter.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 Suppose you have recorded a command to change a word to register c. It
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 works properly, but you would like to add a search for the next word to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 change. This can be done with: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 qC/word<Enter>q
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 You start with "qC", which records to the c register and appends. Thus
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 writing to an uppercase register name means to append to the register with
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 the same letter, but lowercase.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 This works both with recording and with yank and delete commands. For
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 example, you want to collect a sequence of lines into the a register. Yank
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 the first line with: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 "aY
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 Now move to the second line, and type: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 "AY
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 Repeat this command for all lines. The a register now contains all those
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 lines, in the order you yanked them.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 *10.2* Substitution *find-replace*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 The ":substitute" command enables you to perform string replacements on a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 whole range of lines. The general form of this command is as follows: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 :[range]substitute/from/to/[flags]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 This command changes the "from" string to the "to" string in the lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 specified with [range]. For example, you can change "Professor" to "Teacher"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 in all lines with the following command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 :%substitute/Professor/Teacher/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157 <
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 Note:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 The ":substitute" command is almost never spelled out completely.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 Most of the time, people use the abbreviated version ":s". From here
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 on the abbreviation will be used.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 The "%" before the command specifies the command works on all lines. Without
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 a range, ":s" only works on the current line. More about ranges in the next
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165 section |10.3|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 By default, the ":substitute" command changes only the first occurrence on
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 each line. For example, the preceding command changes the line:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 Professor Smith criticized Professor Johnson today. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 to:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 Teacher Smith criticized Professor Johnson today. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176 To change every occurrence on the line, you need to add the g (global) flag.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 The command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 :%s/Professor/Teacher/g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 results in (starting with the original line):
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 Teacher Smith criticized Teacher Johnson today. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 Other flags include p (print), which causes the ":substitute" command to print
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 874
diff changeset
186 out the last line it changes. The c (confirm) flag tells ":substitute" to ask
e63691e7c504 updated for version 7.1a
vimboss
parents: 874
diff changeset
187 you for confirmation before it performs each substitution. Enter the
e63691e7c504 updated for version 7.1a
vimboss
parents: 874
diff changeset
188 following: >
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 :%s/Professor/Teacher/c
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 Vim finds the first occurrence of "Professor" and displays the text it is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 about to change. You get the following prompt: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195 replace with Teacher (y/n/a/q/l/^E/^Y)?
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197 At this point, you must enter one of the following answers:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199 y Yes; make this change.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 n No; skip this match.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201 a All; make this change and all remaining ones without
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202 further confirmation.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203 q Quit; don't make any more changes.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204 l Last; make this change and then quit.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205 CTRL-E Scroll the text one line up.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 CTRL-Y Scroll the text one line down.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209 The "from" part of the substitute command is actually a pattern. The same
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 kind as used for the search command. For example, this command only
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211 substitutes "the" when it appears at the start of a line: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 :s/^the/these/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 If you are substituting with a "from" or "to" part that includes a slash, you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 need to put a backslash before it. A simpler way is to use another character
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 instead of the slash. A plus, for example: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 :s+one/two+one or two+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222 *10.3* Command ranges
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 The ":substitute" command, and many other : commands, can be applied to a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 selection of lines. This is called a range.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 The simple form of a range is {number},{number}. For example: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 :1,5s/this/that/g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 Executes the substitute command on the lines 1 to 5. Line 5 is included.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 The range is always placed before the command.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 A single number can be used to address one specific line: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 :54s/President/Fool/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 Some commands work on the whole file when you do not specify a range. To make
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 them work on the current line the "." address is used. The ":write" command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239 works like that. Without a range, it writes the whole file. To make it write
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240 only the current line into a file: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 :.write otherfile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 The first line always has number one. How about the last line? The "$"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 character is used for this. For example, to substitute in the lines from the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 cursor to the end: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248 :.,$s/yes/no/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 The "%" range that we used before, is actually a short way to say "1,$", from
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 the first to the last line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 USING A PATTERN IN A RANGE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256 Suppose you are editing a chapter in a book, and want to replace all
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 occurrences of "grey" with "gray". But only in this chapter, not in the next
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 one. You know that only chapter boundaries have the word "Chapter" in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 first column. This command will work then: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261 :?^Chapter?,/^Chapter/s=grey=gray=g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263 You can see a search pattern is used twice. The first "?^Chapter?" finds the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 line above the current position that matches this pattern. Thus the ?pattern?
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 range is used to search backwards. Similarly, "/^Chapter/" is used to search
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 forward for the start of the next chapter.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267 To avoid confusion with the slashes, the "=" character was used in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 substitute command here. A slash or another character would have worked as
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 well.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 ADD AND SUBTRACT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 There is a slight error in the above command: If the title of the next chapter
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275 had included "grey" it would be replaced as well. Maybe that's what you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 wanted, but what if you didn't? Then you can specify an offset.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277 To search for a pattern and then use the line above it: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 /Chapter/-1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 You can use any number instead of the 1. To address the second line below the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 match: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 /Chapter/+2
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 The offsets can also be used with the other items in a range. Look at this
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 one: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289 :.+3,$-5
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 This specifies the range that starts three lines below the cursor and ends
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 five lines before the last line in the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
293
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 USING MARKS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297 Instead of figuring out the line numbers of certain positions, remembering them
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 and typing them in a range, you can use marks.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 Place the marks as mentioned in chapter 3. For example, use "mt" to mark
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300 the top of an area and "mb" to mark the bottom. Then you can use this range
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 to specify the lines between the marks (including the lines with the marks): >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 :'t,'b
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
305
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 VISUAL MODE AND RANGES
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308 You can select text with Visual mode. If you then press ":" to start a colon
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 command, you will see this: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311 :'<,'>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 Now you can type the command and it will be applied to the range of lines that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 was visually selected.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 Note:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317 When using Visual mode to select part of a line, or using CTRL-V to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318 select a block of text, the colon commands will still apply to whole
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 lines. This might change in a future version of Vim.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 The '< and '> are actually marks, placed at the start and end of the Visual
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 selection. The marks remain at their position until another Visual selection
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 is made. Thus you can use the "'<" command to jump to position where the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 Visual area started. And you can mix the marks with other items: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 :'>,$
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 This addresses the lines from the end of the Visual area to the end of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332 A NUMBER OF LINES
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334 When you know how many lines you want to change, you can type the number and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 then ":". For example, when you type "5:", you will get: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 :.,.+4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 Now you can type the command you want to use. It will use the range "."
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 (current line) until ".+4" (four lines down). Thus it spans five lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 *10.4* The global command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 The ":global" command is one of the more powerful features of Vim. It allows
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 you to find a match for a pattern and execute a command there. The general
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 form is: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 :[range]global/{pattern}/{command}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 This is similar to the ":substitute" command. But, instead of replacing the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 matched text with other text, the command {command} is executed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 Note:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 The command executed for ":global" must be one that starts with a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 colon. Normal mode commands can not be used directly. The |:normal|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357 command can do this for you.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359 Suppose you want to change "foobar" to "barfoo", but only in C++ style
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 comments. These comments start with "//". Use this command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 :g+//+s/foobar/barfoo/g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
363
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364 This starts with ":g". That is short for ":global", just like ":s" is short
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
365 for ":substitute". Then the pattern, enclosed in plus characters. Since the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366 pattern we are looking for contains a slash, this uses the plus character to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367 separate the pattern. Next comes the substitute command that changes "foobar"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368 into "barfoo".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 The default range for the global command is the whole file. Thus no range
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370 was specified in this example. This is different from ":substitute", which
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371 works on one line without a range.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372 The command isn't perfect, since it also matches lines where "//" appears
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373 halfway a line, and the substitution will also take place before the "//".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375 Just like with ":substitute", any pattern can be used. When you learn more
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
376 complicated patterns later, you can use them here.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379 *10.5* Visual block mode
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 With CTRL-V you can start selection of a rectangular area of text. There are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382 a few commands that do something special with the text block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 There is something special about using the "$" command in Visual block mode.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 When the last motion command used was "$", all lines in the Visual selection
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386 will extend until the end of the line, also when the line with the cursor is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 shorter. This remains effective until you use a motion command that moves the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388 cursor horizontally. Thus using "j" keeps it, "h" stops it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
391 INSERTING TEXT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
392
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
393 The command "I{string}<Esc>" inserts the text {string} in each line, just
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
394 left of the visual block. You start by pressing CTRL-V to enter visual block
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
395 mode. Now you move the cursor to define your block. Next you type I to enter
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
396 Insert mode, followed by the text to insert. As you type, the text appears on
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
397 the first line only.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
398 After you press <Esc> to end the insert, the text will magically be
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399 inserted in the rest of the lines contained in the visual selection. Example:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 include one ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402 include two ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
403 include three ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
404 include four ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
406 Move the cursor to the "o" of "one" and press CTRL-V. Move it down with "3j"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
407 to "four". You now have a block selection that spans four lines. Now type: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
408
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409 Imain.<Esc>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
410
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
411 The result:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
412
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413 include main.one ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 include main.two ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415 include main.three ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416 include main.four ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
417
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
418 If the block spans short lines that do not extend into the block, the text is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419 not inserted in that line. For example, make a Visual block selection that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
420 includes the word "long" in the first and last line of this text, and thus has
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
421 no text selected in the second line:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
422
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423 This is a long line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424 short ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
425 Any other long line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427 ^^^^ selected block
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
429 Now use the command "Ivery <Esc>". The result is:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
430
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
431 This is a very long line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
432 short ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
433 Any other very long line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
434
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
435 In the short line no text was inserted.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437 If the string you insert contains a newline, the "I" acts just like a Normal
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438 insert command and affects only the first line of the block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 The "A" command works the same way, except that it appends after the right
205
d292c40ca788 updated for version 7.0060
vimboss
parents: 7
diff changeset
441 side of the block. And it does insert text in a short line. Thus you can
d292c40ca788 updated for version 7.0060
vimboss
parents: 7
diff changeset
442 make a choice whether you do or don't want to append text to a short line.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 There is one special case for "A": Select a Visual block and then use "$"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
444 to make the block extend to the end of each line. Using "A" now will append
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
445 the text to the end of each line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
446 Using the same example from above, and then typing "$A XXX<Esc>, you get
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
447 this result:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
448
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
449 This is a long line XXX ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
450 short XXX ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
451 Any other long line XXX ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
452
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 This really requires using the "$" command. Vim remembers that it was used.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
454 Making the same selection by moving the cursor to the end of the longest line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
455 with other movement commands will not have the same result.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
456
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
457
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
458 CHANGING TEXT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
459
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
460 The Visual block "c" command deletes the block and then throws you into Insert
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
461 mode to enable you to type in a string. The string will be inserted in each
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
462 line in the block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
463 Starting with the same selection of the "long" words as above, then typing
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
464 "c_LONG_<Esc>", you get this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
465
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 This is a _LONG_ line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
467 short ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
468 Any other _LONG_ line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470 Just like with "I" the short line is not changed. Also, you can't enter a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471 newline in the new text.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473 The "C" command deletes text from the left edge of the block to the end of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 line. It then puts you in Insert mode so that you can type in a string,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 which is added to the end of each line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476 Starting with the same text again, and typing "Cnew text<Esc>" you get:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478 This is a new text ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479 short ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 Any other new text ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482 Notice that, even though only the "long" word was selected, the text after it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
483 is deleted as well. Thus only the location of the left edge of the visual
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
484 block really matters.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
485 Again, short lines that do not reach into the block are excluded.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
486
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
487 Other commands that change the characters in the block:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
488
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
489 ~ swap case (a -> A and A -> a)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
490 U make uppercase (a -> A and A -> A)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491 u make lowercase (a -> a and A -> a)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
492
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
493
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
494 FILLING WITH A CHARACTER
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
495
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
496 To fill the whole block with one character, use the "r" command. Again,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 starting with the same example text from above, and then typing "rx":
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
498
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
499 This is a xxxx line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
500 short ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
501 Any other xxxx line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
502
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
503
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504 Note:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
505 If you want to include characters beyond the end of the line in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
506 block, check out the 'virtualedit' feature in chapter 25.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509 SHIFTING
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511 The command ">" shifts the selected text to the right one shift amount,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 inserting whitespace. The starting point for this shift is the left edge of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 the visual block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514 With the same example again, ">" gives this result:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 This is a long line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517 short ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 Any other long line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 The shift amount is specified with the 'shiftwidth' option. To change it to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521 use 4 spaces: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523 :set shiftwidth=4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525 The "<" command removes one shift amount of whitespace at the left
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
526 edge of the block. This command is limited by the amount of text that is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 there; so if there is less than a shift amount of whitespace available, it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528 removes what it can.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
530
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
531 JOINING LINES
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533 The "J" command joins all selected lines together into one line. Thus it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534 removes the line breaks. Actually, the line break, leading white space and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535 trailing white space is replaced by one space. Two spaces are used after a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536 line ending (that can be changed with the 'joinspaces' option).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 Let's use the example that we got so familiar with now. The result of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538 using the "J" command:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
540 This is a long line short Any other long line ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
541
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
542 The "J" command doesn't require a blockwise selection. It works with "v" and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
543 "V" selection in exactly the same way.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
544
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
545 If you don't want the white space to be changed, use the "gJ" command.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
546
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548 *10.6* Reading and writing part of a file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550 When you are writing an e-mail message, you may want to include another file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551 This can be done with the ":read {filename}" command. The text of the file is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 put below the cursor line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553 Starting with this text:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555 Hi John, ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 Here is the diff that fixes the bug: ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557 Bye, Pierre. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559 Move the cursor to the second line and type: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561 :read patch
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563 The file named "patch" will be inserted, with this result:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565 Hi John, ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566 Here is the diff that fixes the bug: ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 2c2 ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568 < for (i = 0; i <= length; ++i) ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569 --- ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
570 > for (i = 0; i < length; ++i) ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571 Bye, Pierre. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 The ":read" command accepts a range. The file will be put below the last line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574 number of this range. Thus ":$r patch" appends the file "patch" at the end of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576 What if you want to read the file above the first line? This can be done
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 with the line number zero. This line doesn't really exist, you will get an
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
578 error message when using it with most commands. But this command is allowed:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
579 >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580 :0read patch
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582 The file "patch" will be put above the first line of the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
585 WRITING A RANGE OF LINES
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
586
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
587 To write a range of lines to a file, the ":write" command can be used.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
588 Without a range it writes the whole file. With a range only the specified
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
589 lines are written: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
590
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
591 :.,$write tempo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
592
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
593 This writes the lines from the cursor until the end of the file into the file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
594 "tempo". If this file already exists you will get an error message. Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
595 protects you from accidentally overwriting an existing file. If you know what
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
596 you are doing and want to overwrite the file, append !: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
597
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
598 :.,$write! tempo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
599
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
600 CAREFUL: The ! must follow the ":write" command immediately, without white
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
601 space. Otherwise it becomes a filter command, which is explained later in
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
602 this chapter.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
603
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
604
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
605 APPENDING TO A FILE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
606
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
607 In the first section of this chapter was explained how to collect a number of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
608 lines into a register. The same can be done to collect lines in a file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
609 Write the first line with this command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
610
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
611 :.write collection
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
612
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
613 Now move the cursor to the second line you want to collect, and type this: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
614
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
615 :.write >>collection
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
616
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
617 The ">>" tells Vim the "collection" file is not to be written as a new file,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
618 but the line must be appended at the end. You can repeat this as many times
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
619 as you like.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
620
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
621 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
622 *10.7* Formatting text
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
623
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
624 When you are typing plain text, it's nice if the length of each line is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
625 automatically trimmed to fit in the window. To make this happen while
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
626 inserting text, set the 'textwidth' option: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
627
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
628 :set textwidth=72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
629
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
630 You might remember that in the example vimrc file this command was used for
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
631 every text file. Thus if you are using that vimrc file, you were already
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
632 using it. To check the current value of 'textwidth': >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
633
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
634 :set textwidth
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
635
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
636 Now lines will be broken to take only up to 72 characters. But when you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
637 insert text halfway a line, or when you delete a few words, the lines will get
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
638 too long or too short. Vim doesn't automatically reformat the text.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
639 To tell Vim to format the current paragraph: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
640
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
641 gqap
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
642
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
643 This starts with the "gq" command, which is an operator. Following is "ap",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
644 the text object that stands for "a paragraph". A paragraph is separated from
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
645 the next paragraph by an empty line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
646
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
647 Note:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
648 A blank line, which contains white space, does NOT separate
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
649 paragraphs. This is hard to notice!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
650
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
651 Instead of "ap" you could use any motion or text object. If your paragraphs
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
652 are properly separated, you can use this command to format the whole file: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
653
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
654 gggqG
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
655
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
656 "gg" takes you to the first line, "gq" is the format operator and "G" the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
657 motion that jumps to the last line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
658
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
659 In case your paragraphs aren't clearly defined, you can format just the lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
660 you manually select. Move the cursor to the first line you want to format.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
661 Start with the command "gqj". This formats the current line and the one below
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
662 it. If the first line was short, words from the next line will be appended.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
663 If it was too long, words will be moved to the next line. The cursor moves to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
664 the second line. Now you can use "." to repeat the command. Keep doing this
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
665 until you are at the end of the text you want to format.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
666
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
667 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
668 *10.8* Changing case
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
669
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
670 You have text with section headers in lowercase. You want to make the word
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
671 "section" all uppercase. Do this with the "gU" operator. Start with the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
672 cursor in the first column: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
673
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
674 gUw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
675 < section header ----> SECTION header
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
676
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
677 The "gu" operator does exactly the opposite: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
678
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
679 guw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
680 < SECTION header ----> section header
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
681
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
682 You can also use "g~" to swap case. All these are operators, thus they work
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
683 with any motion command, with text objects and in Visual mode.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
684 To make an operator work on lines you double it. The delete operator is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
685 "d", thus to delete a line you use "dd". Similarly, "gugu" makes a whole line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
686 lowercase. This can be shortened to "guu". "gUgU" is shortened to "gUU" and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
687 "g~g~" to "g~~". Example: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
688
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
689 g~~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
690 < Some GIRLS have Fun ----> sOME girls HAVE fUN ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
691
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
692 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
693 *10.9* Using an external program
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
694
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
695 Vim has a very powerful set of commands, it can do anything. But there may
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
696 still be something that an external command can do better or faster.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
697 The command "!{motion}{program}" takes a block of text and filters it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
698 through an external program. In other words, it runs the system command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
699 represented by {program}, giving it the block of text represented by {motion}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
700 as input. The output of this command then replaces the selected block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
701 Because this summarizes badly if you are unfamiliar with UNIX filters, take
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
702 a look at an example. The sort command sorts a file. If you execute the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
703 following command, the unsorted file input.txt will be sorted and written to
236
4707450c2b33 updated for version 7.0066
vimboss
parents: 205
diff changeset
704 output.txt. (This works on both UNIX and Microsoft Windows.) >
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
705
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
706 sort <input.txt >output.txt
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
707
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
708 Now do the same thing in Vim. You want to sort lines 1 through 5 of a file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
709 You start by putting the cursor on line 1. Next you execute the following
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
710 command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
711
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
712 !5G
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
713
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
714 The "!" tells Vim that you are performing a filter operation. The Vim editor
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
715 expects a motion command to follow, indicating which part of the file to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
716 filter. The "5G" command tells Vim to go to line 5, so it now knows that it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
717 is to filter lines 1 (the current line) through 5.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
718 In anticipation of the filtering, the cursor drops to the bottom of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
719 screen and a ! prompt displays. You can now type in the name of the filter
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
720 program, in this case "sort". Therefore, your full command is as follows: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
721
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
722 !5Gsort<Enter>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
723
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
724 The result is that the sort program is run on the first 5 lines. The output
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
725 of the program replaces these lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
726
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
727 line 55 line 11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
728 line 33 line 22
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
729 line 11 --> line 33
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
730 line 22 line 44
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
731 line 44 line 55
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
732 last line last line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
733
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
734 The "!!" command filters the current line through a filter. In Unix the "date"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
735 command prints the current time and date. "!!date<Enter>" replaces the current
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
736 line with the output of "date". This is useful to add a timestamp to a file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
737
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
738
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
739 WHEN IT DOESN'T WORK
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
740
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
741 Starting a shell, sending it text and capturing the output requires that Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
742 knows how the shell works exactly. When you have problems with filtering,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
743 check the values of these options:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
744
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
745 'shell' specifies the program that Vim uses to execute
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
746 external programs.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
747 'shellcmdflag' argument to pass a command to the shell
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
748 'shellquote' quote to be used around the command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
749 'shellxquote' quote to be used around the command and redirection
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
750 'shelltype' kind of shell (only for the Amiga)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
751 'shellslash' use forward slashes in the command (only for
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
752 MS-Windows and alikes)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
753 'shellredir' string used to write the command output into a file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
754
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
755 On Unix this is hardly ever a problem, because there are two kinds of shells:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
756 "sh" like and "csh" like. Vim checks the 'shell' option and sets related
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
757 options automatically, depending on whether it sees "csh" somewhere in
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
758 'shell'.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
759 On MS-Windows, however, there are many different shells and you might have
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
760 to tune the options to make filtering work. Check the help for the options
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
761 for more information.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
762
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
763
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
764 READING COMMAND OUTPUT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
765
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
766 To read the contents of the current directory into the file, use this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
767
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
768 on Unix: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
769 :read !ls
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
770 on MS-Windows: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
771 :read !dir
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
772
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
773 The output of the "ls" or "dir" command is captured and inserted in the text,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
774 below the cursor. This is similar to reading a file, except that the "!" is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
775 used to tell Vim that a command follows.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
776 The command may have arguments. And a range can be used to tell where Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
777 should put the lines: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
778
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
779 :0read !date -u
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
780
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
781 This inserts the current time and date in UTC format at the top of the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
782 (Well, if you have a date command that accepts the "-u" argument.) Note the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
783 difference with using "!!date": that replaced a line, while ":read !date" will
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
784 insert a line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
785
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
786
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
787 WRITING TEXT TO A COMMAND
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
788
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
789 The Unix command "wc" counts words. To count the words in the current file: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
790
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
791 :write !wc
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
792
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
793 This is the same write command as before, but instead of a file name the "!"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
794 character is used and the name of an external command. The written text will
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
795 be passed to the specified command as its standard input. The output could
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
796 look like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
797
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
798 4 47 249 ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
799
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
800 The "wc" command isn't verbose. This means you have 4 lines, 47 words and 249
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
801 characters.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
802
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
803 Watch out for this mistake: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
804
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
805 :write! wc
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
806
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
807 This will write the file "wc" in the current directory, with force. White
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
808 space is important here!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
809
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
810
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
811 REDRAWING THE SCREEN
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
812
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
813 If the external command produced an error message, the display may have been
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
814 messed up. Vim is very efficient and only redraws those parts of the screen
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
815 that it knows need redrawing. But it can't know about what another program
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
816 has written. To tell Vim to redraw the screen: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
817
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
818 CTRL-L
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
819
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
820 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
821
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
822 Next chapter: |usr_11.txt| Recovering from a crash
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
823
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
824 Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: