代码拉取完成,页面将自动刷新
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>usr_10 - Vim Documentation</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim8.0">
<meta name="syntax" content="help">
<meta name="settings" content="no_pre,use_css,expand_tabs">
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="jquery.min.js" type="text/javascript"></script>
<script src="mark-current-page.js" type="text/javascript"></script>
</head>
<body>
<header>
<div class="header">
<a href="http://vim-jp.org/">vim-jp</a>
/ <a href="http://vim-jp.org/vimdoc-en/">vimdoc-en</a>
/ usr_10<br />
<a name="top"></a><h1>usr_10 - Vim Documentation</h1>
<a href="index.html">Return to main</a>
<span class="EnglishJapaneseLink">
<span class="CurrentLanguage">English</span>
</span>
</div>
</header>
<nav>
<dl>
<dt>BASIC</dt>
<dd><ul>
<li><a href="quickref.html">quickref</a></li>
<li><a href="sponsor.html">sponsor</a></li>
</ul></dd>
<dt>USER MANUAL</dt>
<dd><ul>
<li><a href="usr_toc.html">usr_toc</a></li>
</ul></dd>
<dt>Getting Started</dt>
<dd><ul>
<li><a href="usr_01.html">usr_01</a></li>
<li><a href="usr_02.html">usr_02</a></li>
<li><a href="usr_03.html">usr_03</a></li>
<li><a href="usr_04.html">usr_04</a></li>
<li><a href="usr_05.html">usr_05</a></li>
<li><a href="usr_06.html">usr_06</a></li>
<li><a href="usr_07.html">usr_07</a></li>
<li><a href="usr_08.html">usr_08</a></li>
<li><a href="usr_09.html">usr_09</a></li>
<li><a href="usr_10.html">usr_10</a></li>
<li><a href="usr_11.html">usr_11</a></li>
<li><a href="usr_12.html">usr_12</a></li>
</ul></dd>
<dt>Editing Effectively</dt>
<dd><ul>
<li><a href="usr_20.html">usr_20</a></li>
<li><a href="usr_21.html">usr_21</a></li>
<li><a href="usr_22.html">usr_22</a></li>
<li><a href="usr_23.html">usr_23</a></li>
<li><a href="usr_24.html">usr_24</a></li>
<li><a href="usr_25.html">usr_25</a></li>
<li><a href="usr_26.html">usr_26</a></li>
<li><a href="usr_27.html">usr_27</a></li>
<li><a href="usr_28.html">usr_28</a></li>
<li><a href="usr_29.html">usr_29</a></li>
<li><a href="usr_30.html">usr_30</a></li>
<li><a href="usr_31.html">usr_31</a></li>
<li><a href="usr_32.html">usr_32</a></li>
</ul></dd>
<dt>Tuning Vim</dt>
<dd><ul>
<li><a href="usr_40.html">usr_40</a></li>
<li><a href="usr_41.html">usr_41</a></li>
<li><a href="usr_42.html">usr_42</a></li>
<li><a href="usr_43.html">usr_43</a></li>
<li><a href="usr_44.html">usr_44</a></li>
<li><a href="usr_45.html">usr_45</a></li>
</ul></dd>
<dt>Making Vim Run</dt>
<dd><ul>
<li><a href="usr_90.html">usr_90</a></li>
</ul></dd>
<dt>General subjects</dt>
<dd><ul>
<li><a href="intro.html">intro</a></li>
<li><a href="index.html">help</a></li>
<li><a href="helphelp.html">helphelp</a></li>
<li><a href="vimindex.html">index</a></li>
<li><a href="tags.html">tags</a></li>
<li><a href="howto.html">howto</a></li>
<li><a href="tips.html">tips</a></li>
<li><a href="message.html">message</a></li>
<li><a href="quotes.html">quotes</a></li>
<li><a href="todo.html">todo</a></li>
<li><a href="debug.html">debug</a></li>
<li><a href="develop.html">develop</a></li>
<li><a href="uganda.html">uganda</a></li>
</ul></dd>
<dt>Basic editing</dt>
<dd><ul>
<li><a href="starting.html">starting</a></li>
<li><a href="editing.html">editing</a></li>
<li><a href="motion.html">motion</a></li>
<li><a href="scroll.html">scroll</a></li>
<li><a href="insert.html">insert</a></li>
<li><a href="change.html">change</a></li>
<li><a href="indent.html">indent</a></li>
<li><a href="undo.html">undo</a></li>
<li><a href="repeat.html">repeat</a></li>
<li><a href="visual.html">visual</a></li>
<li><a href="various.html">various</a></li>
<li><a href="recover.html">recover</a></li>
</ul></dd>
<dt>Advanced editing</dt>
<dd><ul>
<li><a href="cmdline.html">cmdline</a></li>
<li><a href="options.html">options</a></li>
<li><a href="pattern.html">pattern</a></li>
<li><a href="map.html">map</a></li>
<li><a href="tagsrch.html">tagsrch</a></li>
<li><a href="quickfix.html">quickfix</a></li>
<li><a href="windows.html">windows</a></li>
<li><a href="tabpage.html">tabpage</a></li>
<li><a href="syntax.html">syntax</a></li>
<li><a href="spell.html">spell</a></li>
<li><a href="diff.html">diff</a></li>
<li><a href="autocmd.html">autocmd</a></li>
<li><a href="filetype.html">filetype</a></li>
<li><a href="eval.html">eval</a></li>
<li><a href="channel.html">channel</a></li>
<li><a href="fold.html">fold</a></li>
</ul></dd>
<dt>Special issues</dt>
<dd><ul>
<li><a href="print.html">print</a></li>
<li><a href="remote.html">remote</a></li>
<li><a href="term.html">term</a></li>
<li><a href="digraph.html">digraph</a></li>
<li><a href="mbyte.html">mbyte</a></li>
<li><a href="mlang.html">mlang</a></li>
<li><a href="arabic.html">arabic</a></li>
<li><a href="farsi.html">farsi</a></li>
<li><a href="hebrew.html">hebrew</a></li>
<li><a href="russian.html">russian</a></li>
<li><a href="ft_ada.html">ft_ada</a></li>
<li><a href="ft_sql.html">ft_sql</a></li>
<li><a href="hangulin.html">hangulin</a></li>
<li><a href="rileft.html">rileft</a></li>
</ul></dd>
<dt>GUI</dt>
<dd><ul>
<li><a href="gui.html">gui</a></li>
<li><a href="gui_w32.html">gui_w32</a></li>
<li><a href="gui_x11.html">gui_x11</a></li>
</ul></dd>
<dt>Interfaces</dt>
<dd><ul>
<li><a href="if_cscop.html">if_cscop</a></li>
<li><a href="if_lua.html">if_lua</a></li>
<li><a href="if_mzsch.html">if_mzsch</a></li>
<li><a href="if_perl.html">if_perl</a></li>
<li><a href="if_pyth.html">if_pyth</a></li>
<li><a href="if_tcl.html">if_tcl</a></li>
<li><a href="if_ole.html">if_ole</a></li>
<li><a href="if_ruby.html">if_ruby</a></li>
<li><a href="debugger.html">debugger</a></li>
<li><a href="workshop.html">workshop</a></li>
<li><a href="netbeans.html">netbeans</a></li>
<li><a href="sign.html">sign</a></li>
</ul></dd>
<dt>Versions</dt>
<dd><ul>
<li><a href="vi_diff.html">vi_diff</a></li>
<li><a href="version4.html">version4</a></li>
<li><a href="version5.html">version5</a></li>
<li><a href="version6.html">version6</a></li>
<li><a href="version7.html">version7</a></li>
<li><a href="version8.html">version8</a></li>
</ul></dd>
<dt>Remarks about specific systems</dt>
<dd><ul>
<li><a href="os_390.html">os_390</a></li>
<li><a href="os_amiga.html">os_amiga</a></li>
<li><a href="os_beos.html">os_beos</a></li>
<li><a href="os_dos.html">os_dos</a></li>
<li><a href="os_mac.html">os_mac</a></li>
<li><a href="os_mint.html">os_mint</a></li>
<li><a href="os_msdos.html">os_msdos</a></li>
<li><a href="os_os2.html">os_os2</a></li>
<li><a href="os_qnx.html">os_qnx</a></li>
<li><a href="os_risc.html">os_risc</a></li>
<li><a href="os_unix.html">os_unix</a></li>
<li><a href="os_vms.html">os_vms</a></li>
<li><a href="os_win32.html">os_win32</a></li>
</ul></dd>
<dt>Standard plugins</dt>
<dd><ul>
<li><a href="pi_getscript.html">pi_getscript</a></li>
<li><a href="pi_gzip.html">pi_gzip</a></li>
<li><a href="pi_logipat.html">pi_logipat</a></li>
<li><a href="pi_netrw.html">pi_netrw</a></li>
<li><a href="pi_paren.html">pi_paren</a></li>
<li><a href="pi_tar.html">pi_tar</a></li>
<li><a href="pi_vimball.html">pi_vimball</a></li>
<li><a href="pi_zip.html">pi_zip</a></li>
</ul></dd>
<dt>Filetype plugins</dt>
<dd><ul>
<li><a href="pi_spec.html">pi_spec</a></li>
</ul></dd>
<dt>Others</dt>
<dd><ul>
<li><a href="vim_faq.html">vim_faq</a></li>
</ul></dd>
</dl>
</nav>
<article class="Vimdoc VimdocJa">
<div id='vimCodeElement'>
<a class="Constant" href="usr_10.html" name="usr_10.txt">usr_10.txt</a> For <span class="Identifier">Vim version 8.0.</span> Last change: 2006 Nov 05<br>
<br>
VIM USER MANUAL - by Bram Moolenaar<br>
<br>
Making big changes<br>
<br>
<br>
In chapter 4 several ways to make small changes were explained. This chapter<br>
goes into making changes that are repeated or can affect a large amount of<br>
text. The Visual mode allows doing various things with blocks of text. Use<br>
an external program to do really complicated things.<br>
<br>
<a class="Identifier" href="usr_10.html#10.1">10.1</a> Record and playback commands<br>
<a class="Identifier" href="usr_10.html#10.2">10.2</a> Substitution<br>
<a class="Identifier" href="usr_10.html#10.3">10.3</a> Command ranges<br>
<a class="Identifier" href="usr_10.html#10.4">10.4</a> The global command<br>
<a class="Identifier" href="usr_10.html#10.5">10.5</a> Visual block mode<br>
<a class="Identifier" href="usr_10.html#10.6">10.6</a> Reading and writing part of a file<br>
<a class="Identifier" href="usr_10.html#10.7">10.7</a> Formatting text<br>
<a class="Identifier" href="usr_10.html#10.8">10.8</a> Changing case<br>
<a class="Identifier" href="usr_10.html#10.9">10.9</a> Using an external program<br>
<br>
Next chapter: <a class="Identifier" href="usr_11.html">usr_11.txt</a> Recovering from a crash<br>
Previous chapter: <a class="Identifier" href="usr_09.html">usr_09.txt</a> Using the GUI<br>
Table of contents: <a class="Identifier" href="usr_toc.html">usr_toc.txt</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.1" name="10.1">10.1</a> Record and playback commands<br>
<br>
The "." command repeats the preceding change. But what if you want to do<br>
something more complex than a single change? That's where command recording<br>
comes in. There are three steps:<br>
<br>
1. The "q<span class="Special">{register}</span>" command starts recording keystrokes into the register<br>
named <span class="Special">{register}</span>. The register name must be between a and z.<br>
2. Type your commands.<br>
3. To finish recording, press q (without any extra character).<br>
<br>
You can now execute the macro by typing the command "@<span class="Special">{register}</span>".<br>
<br>
Take a look at how to use these commands in practice. You have a list of<br>
filenames that look like this:<br>
<br>
<span class="PreProc">stdio.h</span><br>
<span class="PreProc">fcntl.h</span><br>
<span class="PreProc">unistd.h</span><br>
<span class="PreProc">stdlib.h</span><br>
<br>
And what you want is the following:<br>
<br>
<span class="PreProc">#include "stdio.h"</span><br>
<span class="PreProc">#include "fcntl.h"</span><br>
<span class="PreProc">#include "unistd.h"</span><br>
<span class="PreProc">#include "stdlib.h"</span><br>
<br>
You start by moving to the first character of the first line. Next you<br>
execute the following commands:<br>
<br>
qa Start recording a macro in register a.<br>
^ Move to the beginning of the line.<br>
i#include "<span class="Special"><Esc></span> Insert the string #include " at the beginning<br>
of the line.<br>
$ Move to the end of the line.<br>
a"<span class="Special"><Esc></span> Append the character double quotation mark (")<br>
to the end of the line.<br>
j Go to the next line.<br>
q Stop recording the macro.<br>
<br>
Now that you have done the work once, you can repeat the change by typing the<br>
command "@a" three times.<br>
The "@a" command can be preceded by a count, which will cause the macro to<br>
be executed that number of times. In this case you would type:<br>
<br>
<div class="helpExample"> 3@a</div>
<br>
<br>
MOVE AND EXECUTE<br>
<br>
You might have the lines you want to change in various places. Just move the<br>
cursor to each location and use the "@a" command. If you have done that once,<br>
you can do it again with "@@". That's a bit easier to type. If you now<br>
execute register b with "@b", the next "@@" will use register b.<br>
If you compare the playback method with using ".", there are several<br>
differences. First of all, "." can only repeat one change. As seen in the<br>
example above, "@a" can do several changes, and move around as well.<br>
Secondly, "." can only remember the last change. Executing a register allows<br>
you to make any changes and then still use "@a" to replay the recorded<br>
commands. Finally, you can use 26 different registers. Thus you can remember<br>
26 different command sequences to execute.<br>
<br>
<br>
USING REGISTERS<br>
<br>
The registers used for recording are the same ones you used for yank and<br>
delete commands. This allows you to mix recording with other commands to<br>
manipulate the registers.<br>
Suppose you have recorded a few commands in register n. When you execute<br>
this with "@n" you notice you did something wrong. You could try recording<br>
again, but perhaps you will make another mistake. Instead, use this trick:<br>
<br>
G Go to the end of the file.<br>
o<span class="Special"><Esc></span> Create an empty line.<br>
"np Put the text from the n register. You now see<br>
the commands you typed as text in the file.<br>
<span class="Special">{edits}</span> Change the commands that were wrong. This is<br>
just like editing text.<br>
0 Go to the start of the line.<br>
"ny$ Yank the corrected commands into the n<br>
register.<br>
dd Delete the scratch line.<br>
<br>
Now you can execute the corrected commands with "@n". (If your recorded<br>
commands include line breaks, adjust the last two items in the example to<br>
include all the lines.)<br>
<br>
<br>
APPENDING TO A REGISTER<br>
<br>
So far we have used a lowercase letter for the register name. To append to a<br>
register, use an uppercase letter.<br>
Suppose you have recorded a command to change a word to register c. It<br>
works properly, but you would like to add a search for the next word to<br>
change. This can be done with:<br>
<br>
<div class="helpExample"> qC/word<Enter>q</div>
<br>
You start with "qC", which records to the c register and appends. Thus<br>
writing to an uppercase register name means to append to the register with<br>
the same letter, but lowercase.<br>
<br>
This works both with recording and with yank and delete commands. For<br>
example, you want to collect a sequence of lines into the a register. Yank<br>
the first line with:<br>
<br>
<div class="helpExample"> "aY</div>
<br>
Now move to the second line, and type:<br>
<br>
<div class="helpExample"> "AY</div>
<br>
Repeat this command for all lines. The a register now contains all those<br>
lines, in the order you yanked them.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.2" name="10.2">10.2</a> Substitution <a class="Constant" href="usr_10.html#find-replace" name="find-replace">find-replace</a><br>
<br>
The ":substitute" command enables you to perform string replacements on a<br>
whole range of lines. The general form of this command is as follows:<br>
<br>
<div class="helpExample"> :[range]substitute/from/to/[flags]</div>
<br>
This command changes the "from" string to the "to" string in the lines<br>
specified with <span class="Special">[range]</span>. For example, you can change "Professor" to "Teacher"<br>
in all lines with the following command:<br>
<br>
<div class="helpExample"> :%substitute/Professor/Teacher/</div>
<br>
<span class="Todo">Note</span>:<br>
The ":substitute" command is almost never spelled out completely.<br>
Most of the time, people use the abbreviated version ":s". From here<br>
on the abbreviation will be used.<br>
<br>
The "%" before the command specifies the command works on all lines. Without<br>
a range, ":s" only works on the current line. More about ranges in the next<br>
section <a class="Identifier" href="usr_10.html#10.3">10.3</a>.<br>
<br>
By default, the ":substitute" command changes only the first occurrence on<br>
each line. For example, the preceding command changes the line:<br>
<br>
<span class="PreProc">Professor Smith criticized Professor Johnson today.</span><br>
<br>
to:<br>
<br>
<span class="PreProc">Teacher Smith criticized Professor Johnson today.</span><br>
<br>
To change every occurrence on the line, you need to add the g (global) flag.<br>
The command:<br>
<br>
<div class="helpExample"> :%s/Professor/Teacher/g</div>
<br>
results in (starting with the original line):<br>
<br>
<span class="PreProc">Teacher Smith criticized Teacher Johnson today.</span><br>
<br>
Other flags include p (print), which causes the ":substitute" command to print<br>
out the last line it changes. The c (confirm) flag tells ":substitute" to ask<br>
you for confirmation before it performs each substitution. Enter the<br>
following:<br>
<br>
<div class="helpExample"> :%s/Professor/Teacher/c</div>
<br>
Vim finds the first occurrence of "Professor" and displays the text it is<br>
about to change. You get the following prompt:<br>
<br>
<div class="helpExample"> replace with Teacher (y/n/a/q/l/^E/^Y)?</div>
<br>
At this point, you must enter one of the following answers:<br>
<br>
y Yes; make this change.<br>
n No; skip this match.<br>
a All; make this change and all remaining ones without<br>
further confirmation.<br>
q Quit; don't make any more changes.<br>
l Last; make this change and then quit.<br>
<span class="Special">CTRL-E</span> Scroll the text one line up.<br>
<span class="Special">CTRL-Y</span> Scroll the text one line down.<br>
<br>
<br>
The "from" part of the substitute command is actually a pattern. The same<br>
kind as used for the search command. For example, this command only<br>
substitutes "the" when it appears at the start of a line:<br>
<br>
<div class="helpExample"> :s/^the/these/</div>
<br>
If you are substituting with a "from" or "to" part that includes a slash, you<br>
need to put a backslash before it. A simpler way is to use another character<br>
instead of the slash. A plus, for example:<br>
<br>
<div class="helpExample"> :s+one/two+one or two+</div>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.3" name="10.3">10.3</a> Command ranges<br>
<br>
The ":substitute" command, and many other : commands, can be applied to a<br>
selection of lines. This is called a range.<br>
The simple form of a range is <span class="Special">{number}</span>,<span class="Special">{number}</span>. For example:<br>
<br>
<div class="helpExample"> :1,5s/this/that/g</div>
<br>
Executes the substitute command on the lines 1 to 5. Line 5 is included.<br>
The range is always placed before the command.<br>
<br>
A single number can be used to address one specific line:<br>
<br>
<div class="helpExample"> :54s/President/Fool/</div>
<br>
Some commands work on the whole file when you do not specify a range. To make<br>
them work on the current line the "." address is used. The ":write" command<br>
works like that. Without a range, it writes the whole file. To make it write<br>
only the current line into a file:<br>
<br>
<div class="helpExample"> :.write otherfile</div>
<br>
The first line always has number one. How about the last line? The "$"<br>
character is used for this. For example, to substitute in the lines from the<br>
cursor to the end:<br>
<br>
<div class="helpExample"> :.,$s/yes/no/</div>
<br>
The "%" range that we used before, is actually a short way to say "1,$", from<br>
the first to the last line.<br>
<br>
<br>
USING A PATTERN IN A RANGE<br>
<br>
Suppose you are editing a chapter in a book, and want to replace all<br>
occurrences of "grey" with "gray". But only in this chapter, not in the next<br>
one. You know that only chapter boundaries have the word "Chapter" in the<br>
first column. This command will work then:<br>
<br>
<div class="helpExample"> :?^Chapter?,/^Chapter/s=grey=gray=g</div>
<br>
You can see a search pattern is used twice. The first "?^Chapter?" finds the<br>
line above the current position that matches this pattern. Thus the ?pattern?<br>
range is used to search backwards. Similarly, "/^Chapter/" is used to search<br>
forward for the start of the next chapter.<br>
To avoid confusion with the slashes, the "=" character was used in the<br>
substitute command here. A slash or another character would have worked as<br>
well.<br>
<br>
<br>
ADD AND SUBTRACT<br>
<br>
There is a slight error in the above command: If the title of the next chapter<br>
had included "grey" it would be replaced as well. Maybe that's what you<br>
wanted, but what if you didn't? Then you can specify an offset.<br>
To search for a pattern and then use the line above it:<br>
<br>
<div class="helpExample"> /Chapter/-1</div>
<br>
You can use any number instead of the 1. To address the second line below the<br>
match:<br>
<br>
<div class="helpExample"> /Chapter/+2</div>
<br>
The offsets can also be used with the other items in a range. Look at this<br>
one:<br>
<br>
<div class="helpExample"> :.+3,$-5</div>
<br>
This specifies the range that starts three lines below the cursor and ends<br>
five lines before the last line in the file.<br>
<br>
<br>
USING MARKS<br>
<br>
Instead of figuring out the line numbers of certain positions, remembering them<br>
and typing them in a range, you can use marks.<br>
Place the marks as mentioned in chapter 3. For example, use "mt" to mark<br>
the top of an area and "mb" to mark the bottom. Then you can use this range<br>
to specify the lines between the marks (including the lines with the marks):<br>
<br>
<div class="helpExample"> :'t,'b</div>
<br>
<br>
VISUAL MODE AND RANGES<br>
<br>
You can select text with Visual mode. If you then press ":" to start a colon<br>
command, you will see this:<br>
<br>
<div class="helpExample"> :'<,'></div>
<br>
Now you can type the command and it will be applied to the range of lines that<br>
was visually selected.<br>
<br>
<span class="Todo">Note</span>:<br>
When using Visual mode to select part of a line, or using <span class="Special">CTRL-V</span> to<br>
select a block of text, the colon commands will still apply to whole<br>
lines. This might change in a future version of Vim.<br>
<br>
The '< and '> are actually marks, placed at the start and end of the Visual<br>
selection. The marks remain at their position until another Visual selection<br>
is made. Thus you can use the "'<" command to jump to position where the<br>
Visual area started. And you can mix the marks with other items:<br>
<br>
<div class="helpExample"> :'>,$</div>
<br>
This addresses the lines from the end of the Visual area to the end of the<br>
file.<br>
<br>
<br>
A NUMBER OF LINES<br>
<br>
When you know how many lines you want to change, you can type the number and<br>
then ":". For example, when you type "5:", you will get:<br>
<br>
<div class="helpExample"> :.,.+4</div>
<br>
Now you can type the command you want to use. It will use the range "."<br>
(current line) until ".+4" (four lines down). Thus it spans five lines.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.4" name="10.4">10.4</a> The global command<br>
<br>
The ":global" command is one of the more powerful features of Vim. It allows<br>
you to find a match for a pattern and execute a command there. The general<br>
form is:<br>
<br>
<div class="helpExample"> :[range]global/{pattern}/{command}</div>
<br>
This is similar to the ":substitute" command. But, instead of replacing the<br>
matched text with other text, the command <span class="Special">{command}</span> is executed.<br>
<br>
<span class="Todo">Note</span>:<br>
The command executed for ":global" must be one that starts with a<br>
colon. Normal mode commands can not be used directly. The <a class="Identifier" href="various.html#:normal">:normal</a><br>
command can do this for you.<br>
<br>
Suppose you want to change "foobar" to "barfoo", but only in C++ style<br>
comments. These comments start with "//". Use this command:<br>
<br>
<div class="helpExample"> :g+//+s/foobar/barfoo/g</div>
<br>
This starts with ":g". That is short for ":global", just like ":s" is short<br>
for ":substitute". Then the pattern, enclosed in plus characters. Since the<br>
pattern we are looking for contains a slash, this uses the plus character to<br>
separate the pattern. Next comes the substitute command that changes "foobar"<br>
into "barfoo".<br>
The default range for the global command is the whole file. Thus no range<br>
was specified in this example. This is different from ":substitute", which<br>
works on one line without a range.<br>
The command isn't perfect, since it also matches lines where "//" appears<br>
halfway a line, and the substitution will also take place before the "//".<br>
<br>
Just like with ":substitute", any pattern can be used. When you learn more<br>
complicated patterns later, you can use them here.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.5" name="10.5">10.5</a> Visual block mode<br>
<br>
With <span class="Special">CTRL-V</span> you can start selection of a rectangular area of text. There are<br>
a few commands that do something special with the text block.<br>
<br>
There is something special about using the "$" command in Visual block mode.<br>
When the last motion command used was "$", all lines in the Visual selection<br>
will extend until the end of the line, also when the line with the cursor is<br>
shorter. This remains effective until you use a motion command that moves the<br>
cursor horizontally. Thus using "j" keeps it, "h" stops it.<br>
<br>
<br>
INSERTING TEXT<br>
<br>
The command "I<span class="Special">{string}<Esc></span>" inserts the text <span class="Special">{string}</span> in each line, just<br>
left of the visual block. You start by pressing <span class="Special">CTRL-V</span> to enter visual block<br>
mode. Now you move the cursor to define your block. Next you type I to enter<br>
Insert mode, followed by the text to insert. As you type, the text appears on<br>
the first line only.<br>
After you press <span class="Special"><Esc></span> to end the insert, the text will magically be<br>
inserted in the rest of the lines contained in the visual selection. Example:<br>
<br>
<span class="PreProc">include one</span><br>
<span class="PreProc">include two</span><br>
<span class="PreProc">include three</span><br>
<span class="PreProc">include four</span><br>
<br>
Move the cursor to the "o" of "one" and press <span class="Special">CTRL-V</span>. Move it down with "3j"<br>
to "four". You now have a block selection that spans four lines. Now type:<br>
<br>
<div class="helpExample"> Imain.<Esc></div>
<br>
The result:<br>
<br>
<span class="PreProc">include main.one</span><br>
<span class="PreProc">include main.two</span><br>
<span class="PreProc">include main.three</span><br>
<span class="PreProc">include main.four</span><br>
<br>
If the block spans short lines that do not extend into the block, the text is<br>
not inserted in that line. For example, make a Visual block selection that<br>
includes the word "long" in the first and last line of this text, and thus has<br>
no text selected in the second line:<br>
<br>
<span class="PreProc">This is a long line</span><br>
<span class="PreProc">short</span><br>
<span class="PreProc">Any other long line</span><br>
<br>
^^^^ selected block<br>
<br>
Now use the command "Ivery <span class="Special"><Esc></span>". The result is:<br>
<br>
<span class="PreProc">This is a very long line</span><br>
<span class="PreProc">short</span><br>
<span class="PreProc">Any other very long line</span><br>
<br>
In the short line no text was inserted.<br>
<br>
If the string you insert contains a newline, the "I" acts just like a Normal<br>
insert command and affects only the first line of the block.<br>
<br>
The "A" command works the same way, except that it appends after the right<br>
side of the block. And it does insert text in a short line. Thus you can<br>
make a choice whether you do or don't want to append text to a short line.<br>
There is one special case for "A": Select a Visual block and then use "$"<br>
to make the block extend to the end of each line. Using "A" now will append<br>
the text to the end of each line.<br>
Using the same example from above, and then typing "$A XXX<span class="Special"><Esc></span>, you get<br>
this result:<br>
<br>
<span class="PreProc">This is a long line XXX</span><br>
<span class="PreProc">short XXX</span><br>
<span class="PreProc">Any other long line XXX</span><br>
<br>
This really requires using the "$" command. Vim remembers that it was used.<br>
Making the same selection by moving the cursor to the end of the longest line<br>
with other movement commands will not have the same result.<br>
<br>
<br>
CHANGING TEXT<br>
<br>
The Visual block "c" command deletes the block and then throws you into Insert<br>
mode to enable you to type in a string. The string will be inserted in each<br>
line in the block.<br>
Starting with the same selection of the "long" words as above, then typing<br>
"c_LONG_<span class="Special"><Esc></span>", you get this:<br>
<br>
<span class="PreProc">This is a _LONG_ line</span><br>
<span class="PreProc">short</span><br>
<span class="PreProc">Any other _LONG_ line</span><br>
<br>
Just like with "I" the short line is not changed. Also, you can't enter a<br>
newline in the new text.<br>
<br>
The "C" command deletes text from the left edge of the block to the end of<br>
line. It then puts you in Insert mode so that you can type in a string,<br>
which is added to the end of each line.<br>
Starting with the same text again, and typing "Cnew text<span class="Special"><Esc></span>" you get:<br>
<br>
<span class="PreProc">This is a new text</span><br>
<span class="PreProc">short</span><br>
<span class="PreProc">Any other new text</span><br>
<br>
Notice that, even though only the "long" word was selected, the text after it<br>
is deleted as well. Thus only the location of the left edge of the visual<br>
block really matters.<br>
Again, short lines that do not reach into the block are excluded.<br>
<br>
Other commands that change the characters in the block:<br>
<br>
~ swap case (a -> A and A -> a)<br>
U make uppercase (a -> A and A -> A)<br>
u make lowercase (a -> a and A -> a)<br>
<br>
<br>
FILLING WITH A CHARACTER<br>
<br>
To fill the whole block with one character, use the "r" command. Again,<br>
starting with the same example text from above, and then typing "rx":<br>
<br>
<span class="PreProc">This is a xxxx line</span><br>
<span class="PreProc">short</span><br>
<span class="PreProc">Any other xxxx line</span><br>
<br>
<br>
<span class="Todo">Note</span>:<br>
If you want to include characters beyond the end of the line in the<br>
block, check out the <a class="Type" href="options.html#'virtualedit'">'virtualedit'</a> feature in chapter 25.<br>
<br>
<br>
SHIFTING<br>
<br>
The command ">" shifts the selected text to the right one shift amount,<br>
inserting whitespace. The starting point for this shift is the left edge of<br>
the visual block.<br>
With the same example again, ">" gives this result:<br>
<br>
<span class="PreProc">This is a long line</span><br>
<span class="PreProc">short</span><br>
<span class="PreProc">Any other long line</span><br>
<br>
The shift amount is specified with the <a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a> option. To change it to<br>
use 4 spaces:<br>
<br>
<div class="helpExample"> :set shiftwidth=4</div>
<br>
The "<" command removes one shift amount of whitespace at the left<br>
edge of the block. This command is limited by the amount of text that is<br>
there; so if there is less than a shift amount of whitespace available, it<br>
removes what it can.<br>
<br>
<br>
JOINING LINES<br>
<br>
The "J" command joins all selected lines together into one line. Thus it<br>
removes the line breaks. Actually, the line break, leading white space and<br>
trailing white space is replaced by one space. Two spaces are used after a<br>
line ending (that can be changed with the <a class="Type" href="options.html#'joinspaces'">'joinspaces'</a> option).<br>
Let's use the example that we got so familiar with now. The result of<br>
using the "J" command:<br>
<br>
<span class="PreProc">This is a long line short Any other long line</span><br>
<br>
The "J" command doesn't require a blockwise selection. It works with "v" and<br>
"V" selection in exactly the same way.<br>
<br>
If you don't want the white space to be changed, use the "gJ" command.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.6" name="10.6">10.6</a> Reading and writing part of a file<br>
<br>
When you are writing an e-mail message, you may want to include another file.<br>
This can be done with the ":read <span class="Special">{filename}</span>" command. The text of the file is<br>
put below the cursor line.<br>
Starting with this text:<br>
<br>
<span class="PreProc">Hi John,</span><br>
<span class="PreProc">Here is the diff that fixes the bug:</span><br>
<span class="PreProc">Bye, Pierre.</span><br>
<br>
Move the cursor to the second line and type:<br>
<br>
<div class="helpExample"> :read patch</div>
<br>
The file named "patch" will be inserted, with this result:<br>
<br>
<span class="PreProc">Hi John,</span><br>
<span class="PreProc">Here is the diff that fixes the bug:</span><br>
<span class="PreProc">2c2</span><br>
<span class="PreProc">< for (i = 0; i <= length; ++i)</span><br>
<span class="PreProc">---</span><br>
<span class="PreProc">> for (i = 0; i < length; ++i)</span><br>
<span class="PreProc">Bye, Pierre.</span><br>
<br>
The ":read" command accepts a range. The file will be put below the last line<br>
number of this range. Thus ":$r patch" appends the file "patch" at the end of<br>
the file.<br>
What if you want to read the file above the first line? This can be done<br>
with the line number zero. This line doesn't really exist, you will get an<br>
error message when using it with most commands. But this command is allowed:<br>
<br>
<div class="helpExample"> :0read patch</div>
<br>
The file "patch" will be put above the first line of the file.<br>
<br>
<br>
WRITING A RANGE OF LINES<br>
<br>
To write a range of lines to a file, the ":write" command can be used.<br>
Without a range it writes the whole file. With a range only the specified<br>
lines are written:<br>
<br>
<div class="helpExample"> :.,$write tempo</div>
<br>
This writes the lines from the cursor until the end of the file into the file<br>
"tempo". If this file already exists you will get an error message. Vim<br>
protects you from accidentally overwriting an existing file. If you know what<br>
you are doing and want to overwrite the file, append !:<br>
<br>
<div class="helpExample"> :.,$write! tempo</div>
<br>
CAREFUL: The ! must follow the ":write" command immediately, without white<br>
space. Otherwise it becomes a filter command, which is explained later in<br>
this chapter.<br>
<br>
<br>
APPENDING TO A FILE<br>
<br>
In the first section of this chapter was explained how to collect a number of<br>
lines into a register. The same can be done to collect lines in a file.<br>
Write the first line with this command:<br>
<br>
<div class="helpExample"> :.write collection</div>
<br>
Now move the cursor to the second line you want to collect, and type this:<br>
<br>
<div class="helpExample"> :.write >>collection</div>
<br>
The ">>" tells Vim the "collection" file is not to be written as a new file,<br>
but the line must be appended at the end. You can repeat this as many times<br>
as you like.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.7" name="10.7">10.7</a> Formatting text<br>
<br>
When you are typing plain text, it's nice if the length of each line is<br>
automatically trimmed to fit in the window. To make this happen while<br>
inserting text, set the <a class="Type" href="options.html#'textwidth'">'textwidth'</a> option:<br>
<br>
<div class="helpExample"> :set textwidth=72</div>
<br>
You might remember that in the example vimrc file this command was used for<br>
every text file. Thus if you are using that vimrc file, you were already<br>
using it. To check the current value of <a class="Type" href="options.html#'textwidth'">'textwidth'</a>:<br>
<br>
<div class="helpExample"> :set textwidth</div>
<br>
Now lines will be broken to take only up to 72 characters. But when you<br>
insert text halfway a line, or when you delete a few words, the lines will get<br>
too long or too short. Vim doesn't automatically reformat the text.<br>
To tell Vim to format the current paragraph:<br>
<br>
<div class="helpExample"> gqap</div>
<br>
This starts with the "gq" command, which is an operator. Following is "ap",<br>
the text object that stands for "a paragraph". A paragraph is separated from<br>
the next paragraph by an empty line.<br>
<br>
<span class="Todo">Note</span>:<br>
A blank line, which contains white space, does NOT separate<br>
paragraphs. This is hard to notice!<br>
<br>
Instead of "ap" you could use any motion or text object. If your paragraphs<br>
are properly separated, you can use this command to format the whole file:<br>
<br>
<div class="helpExample"> gggqG</div>
<br>
"gg" takes you to the first line, "gq" is the format operator and "G" the<br>
motion that jumps to the last line.<br>
<br>
In case your paragraphs aren't clearly defined, you can format just the lines<br>
you manually select. Move the cursor to the first line you want to format.<br>
Start with the command "gqj". This formats the current line and the one below<br>
it. If the first line was short, words from the next line will be appended.<br>
If it was too long, words will be moved to the next line. The cursor moves to<br>
the second line. Now you can use "." to repeat the command. Keep doing this<br>
until you are at the end of the text you want to format.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.8" name="10.8">10.8</a> Changing case<br>
<br>
You have text with section headers in lowercase. You want to make the word<br>
"section" all uppercase. Do this with the "gU" operator. Start with the<br>
cursor in the first column:<br>
<br>
<div class="helpExample"> gUw</div>
section header ----> SECTION header<br>
<br>
The "gu" operator does exactly the opposite:<br>
<br>
<div class="helpExample"> guw</div>
SECTION header ----> section header<br>
<br>
You can also use "g~" to swap case. All these are operators, thus they work<br>
with any motion command, with text objects and in Visual mode.<br>
To make an operator work on lines you double it. The delete operator is<br>
"d", thus to delete a line you use "dd". Similarly, "gugu" makes a whole line<br>
lowercase. This can be shortened to "guu". "gUgU" is shortened to "gUU" and<br>
"g~g~" to "g~~". Example:<br>
<br>
<div class="helpExample"> g~~ </div>
<span class="PreProc">Some GIRLS have Fun ----> sOME girls HAVE fUN</span><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_10.html#10.9" name="10.9">10.9</a> Using an external program<br>
<br>
Vim has a very powerful set of commands, it can do anything. But there may<br>
still be something that an external command can do better or faster.<br>
The command "!<span class="Special">{motion}{program}</span>" takes a block of text and filters it<br>
through an external program. In other words, it runs the system command<br>
represented by <span class="Special">{program}</span>, giving it the block of text represented by <span class="Special">{motion}</span><br>
as input. The output of this command then replaces the selected block.<br>
Because this summarizes badly if you are unfamiliar with UNIX filters, take<br>
a look at an example. The sort command sorts a file. If you execute the<br>
following command, the unsorted file input.txt will be sorted and written to<br>
output.txt. (This works on both UNIX and Microsoft Windows.)<br>
<br>
<div class="helpExample"> sort <input.txt >output.txt</div>
<br>
Now do the same thing in Vim. You want to sort lines 1 through 5 of a file.<br>
You start by putting the cursor on line 1. Next you execute the following<br>
command:<br>
<br>
<div class="helpExample"> !5G</div>
<br>
The "!" tells Vim that you are performing a filter operation. The Vim editor<br>
expects a motion command to follow, indicating which part of the file to<br>
filter. The "5G" command tells Vim to go to line 5, so it now knows that it<br>
is to filter lines 1 (the current line) through 5.<br>
In anticipation of the filtering, the cursor drops to the bottom of the<br>
screen and a ! prompt displays. You can now type in the name of the filter<br>
program, in this case "sort". Therefore, your full command is as follows:<br>
<br>
<div class="helpExample"> !5Gsort<Enter></div>
<br>
The result is that the sort program is run on the first 5 lines. The output<br>
of the program replaces these lines.<br>
<br>
line 55 line 11<br>
line 33 line 22<br>
line 11 --> line 33<br>
line 22 line 44<br>
line 44 line 55<br>
last line last line<br>
<br>
The "!!" command filters the current line through a filter. In Unix the "date"<br>
command prints the current time and date. "!!date<span class="Special"><Enter></span>" replaces the current<br>
line with the output of "date". This is useful to add a timestamp to a file.<br>
<br>
<br>
WHEN IT DOESN'T WORK<br>
<br>
Starting a shell, sending it text and capturing the output requires that Vim<br>
knows how the shell works exactly. When you have problems with filtering,<br>
check the values of these options:<br>
<br>
<a class="Type" href="options.html#'shell'">'shell'</a> specifies the program that Vim uses to execute<br>
external programs.<br>
<a class="Type" href="options.html#'shellcmdflag'">'shellcmdflag'</a> argument to pass a command to the shell<br>
<a class="Type" href="options.html#'shellquote'">'shellquote'</a> quote to be used around the command<br>
<a class="Type" href="options.html#'shellxquote'">'shellxquote'</a> quote to be used around the command and redirection<br>
<a class="Type" href="options.html#'shelltype'">'shelltype'</a> kind of shell (only for the Amiga)<br>
<a class="Type" href="options.html#'shellslash'">'shellslash'</a> use forward slashes in the command (only for<br>
MS-Windows and alikes)<br>
<a class="Type" href="options.html#'shellredir'">'shellredir'</a> string used to write the command output into a file<br>
<br>
On Unix this is hardly ever a problem, because there are two kinds of shells:<br>
"sh" like and "csh" like. Vim checks the <a class="Type" href="options.html#'shell'">'shell'</a> option and sets related<br>
options automatically, depending on whether it sees "csh" somewhere in<br>
<a class="Type" href="options.html#'shell'">'shell'</a>.<br>
On MS-Windows, however, there are many different shells and you might have<br>
to tune the options to make filtering work. Check the help for the options<br>
for more information.<br>
<br>
<br>
READING COMMAND OUTPUT<br>
<br>
To read the contents of the current directory into the file, use this:<br>
<br>
on Unix:<br>
<div class="helpExample"> :read !ls</div>
on MS-Windows:<br>
<div class="helpExample"> :read !dir</div>
<br>
The output of the "ls" or "dir" command is captured and inserted in the text,<br>
below the cursor. This is similar to reading a file, except that the "!" is<br>
used to tell Vim that a command follows.<br>
The command may have arguments. And a range can be used to tell where Vim<br>
should put the lines:<br>
<br>
<div class="helpExample"> :0read !date -u</div>
<br>
This inserts the current time and date in UTC format at the top of the file.<br>
(Well, if you have a date command that accepts the "-u" argument.) <span class="Todo">Note</span> the<br>
difference with using "!!date": that replaced a line, while ":read !date" will<br>
insert a line.<br>
<br>
<br>
WRITING TEXT TO A COMMAND<br>
<br>
The Unix command "wc" counts words. To count the words in the current file:<br>
<br>
<div class="helpExample"> :write !wc</div>
<br>
This is the same write command as before, but instead of a file name the "!"<br>
character is used and the name of an external command. The written text will<br>
be passed to the specified command as its standard input. The output could<br>
look like this:<br>
<br>
<span class="PreProc">4 47 249</span><br>
<br>
The "wc" command isn't verbose. This means you have 4 lines, 47 words and 249<br>
characters.<br>
<br>
Watch out for this mistake:<br>
<br>
<div class="helpExample"> :write! wc</div>
<br>
This will write the file "wc" in the current directory, with force. White<br>
space is important here!<br>
<br>
<br>
REDRAWING THE SCREEN<br>
<br>
If the external command produced an error message, the display may have been<br>
messed up. Vim is very efficient and only redraws those parts of the screen<br>
that it knows need redrawing. But it can't know about what another program<br>
has written. To tell Vim to redraw the screen:<br>
<br>
<div class="helpExample"> CTRL-L</div>
<br>
<span class="PreProc">==============================================================================</span><br>
<br>
Next chapter: <a class="Identifier" href="usr_11.html">usr_11.txt</a> Recovering from a crash<br>
<br>
Copyright: see <a class="Identifier" href="usr_01.html#manual-copyright">manual-copyright</a> vim:tw=78:ts=8:ft=help:norl:<br>
</div>
</article>
<footer>
<a href="#top">Return to the top</a> - <a href="index.html">Return to main</a>
<span class="EnglishJapaneseLink">
<span class="CurrentLanguage">English</span>
</span>
<br />
<div style="text-align:right;">
Hosted by <a href="https://github.com/vim-jp/vimdoc-en">vimdoc-en project</a><br />
If you met any problem, please report it to <a href="https://github.com/vim-jp/vimdoc-en/issues">issue</a>.<br />
</div>
</footer>
<!--<script src="js/check-referrer.js" type="text/javascript"></script>-->
</body>
</html>
<!-- vim:set ts=8 sts=2 sw=2 tw=0 et: -->
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。