annotate runtime/doc/usr_12.txt @ 23788:d12ef361d9de v8.2.2435

patch 8.2.2435: setline() gives an error for some types Commit: https://github.com/vim/vim/commit/3445320839a38b3b0c253513b125da8298ec27d6 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jan 31 13:08:38 2021 +0100 patch 8.2.2435: setline() gives an error for some types Problem: setline() gives an error for some types. Solution: Allow any type, convert each item to a string.
author Bram Moolenaar <Bram@vim.org>
date Sun, 31 Jan 2021 13:15:04 +0100
parents af69c9335223
children 5bda4653aced
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18879
af69c9335223 Vim 8.2 release
Bram Moolenaar <Bram@vim.org>
parents: 14519
diff changeset
1 *usr_12.txt* For Vim version 8.2. Last change: 2017 Aug 11
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 Clever tricks
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 By combining several commands you can make Vim do nearly everything. In this
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 chapter a number of useful combinations will be presented. This uses the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 commands introduced in the previous chapters and a few more.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 |12.1| Replace a word
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 |12.2| Change "Last, First" to "First Last"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 |12.3| Sort a list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 |12.4| Reverse line order
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 |12.5| Count words
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 |12.6| Find a man page
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 |12.7| Trim blanks
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 |12.8| Find where a word is used
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 Next chapter: |usr_20.txt| Typing command-line commands quickly
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 Previous chapter: |usr_11.txt| Recovering from a crash
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 Table of contents: |usr_toc.txt|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 *12.1* Replace a word
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 The substitute command can be used to replace all occurrences of a word with
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 another word: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 :%s/four/4/g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 The "%" range means to replace in all lines. The "g" flag at the end causes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 all words in a line to be replaced.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 This will not do the right thing if your file also contains "thirtyfour".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 It would be replaced with "thirty4". To avoid this, use the "\<" item to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 match the start of a word: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 :%s/\<four/4/g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40
1125
96cd8222a819 updated for version 7.1a
vimboss
parents: 874
diff changeset
41 Obviously, this still goes wrong on "fourteen". Use "\>" to match the end of
96cd8222a819 updated for version 7.1a
vimboss
parents: 874
diff changeset
42 a word: >
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 :%s/\<four\>/4/g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 If you are programming, you might want to replace "four" in comments, but not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 in the code. Since this is difficult to specify, add the "c" flag to have the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 substitute command prompt you for each replacement: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 :%s/\<four\>/4/gc
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 REPLACING IN SEVERAL FILES
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 Suppose you want to replace a word in more than one file. You could edit each
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 file and type the command manually. It's a lot faster to use record and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 playback.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 Let's assume you have a directory with C++ files, all ending in ".cpp".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 There is a function called "GetResp" that you want to rename to "GetAnswer".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 vim *.cpp Start Vim, defining the argument list to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 contain all the C++ files. You are now in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 first file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 qq Start recording into the q register
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 :%s/\<GetResp\>/GetAnswer/g
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 Do the replacements in the first file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 :wnext Write this file and move to the next one.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 q Stop recording.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 @q Execute the q register. This will replay the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 substitution and ":wnext". You can verify
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 that this doesn't produce an error message.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 999@q Execute the q register on the remaining files.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 At the last file you will get an error message, because ":wnext" cannot move
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 to the next file. This stops the execution, and everything is done.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 Note:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 When playing back a recorded sequence, an error stops the execution.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 Therefore, make sure you don't get an error message when recording.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 There is one catch: If one of the .cpp files does not contain the word
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 "GetResp", you will get an error and replacing will stop. To avoid this, add
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 the "e" flag to the substitute command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 :%s/\<GetResp\>/GetAnswer/ge
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 The "e" flag tells ":substitute" that not finding a match is not an error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 *12.2* Change "Last, First" to "First Last"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 You have a list of names in this form:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 Doe, John ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 Smith, Peter ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 You want to change that to:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 John Doe ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 Peter Smith ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 This can be done with just one command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 :%s/\([^,]*\), \(.*\)/\2 \1/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 Let's break this down in parts. Obviously it starts with a substitute
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 command. The "%" is the line range, which stands for the whole file. Thus
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 the substitution is done in every line in the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 The arguments for the substitute command are "/from/to/". The slashes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 separate the "from" pattern and the "to" string. This is what the "from"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 pattern contains:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 \([^,]*\), \(.*\) ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 The first part between \( \) matches "Last" \( \)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 match anything but a comma [^,]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 any number of times *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 matches ", " literally ,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 The second part between \( \) matches "First" \( \)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 any character .
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 any number of times *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 In the "to" part we have "\2" and "\1". These are called backreferences.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 They refer to the text matched by the "\( \)" parts in the pattern. "\2"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 refers to the text matched by the second "\( \)", which is the "First" name.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 "\1" refers to the first "\( \)", which is the "Last" name.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 You can use up to nine backreferences in the "to" part of a substitute
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 command. "\0" stands for the whole matched pattern. There are a few more
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 special items in a substitute command, see |sub-replace-special|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 *12.3* Sort a list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 In a Makefile you often have a list of files. For example:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 OBJS = \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 version.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 pch.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 getopt.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 util.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 getopt1.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 inp.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 patch.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 backup.o ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 To sort this list, filter the text through the external sort command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 /^OBJS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 j
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 :.,/^$/-1!sort
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 This goes to the first line, where "OBJS" is the first thing in the line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 Then it goes one line down and filters the lines until the next empty line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 You could also select the lines in Visual mode and then use "!sort". That's
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 easier to type, but more work when there are many lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 The result is this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 OBJS = \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 backup.o ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 getopt.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 getopt1.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 inp.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 patch.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 pch.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165 util.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 version.o \ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169 Notice that a backslash at the end of each line is used to indicate the line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 continues. After sorting, this is wrong! The "backup.o" line that was at
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 the end didn't have a backslash. Now that it sorts to another place, it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 must have a backslash.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 The simplest solution is to add the backslash with "A \<Esc>". You can
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 keep the backslash in the last line, if you make sure an empty line comes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 after it. That way you don't have this problem again.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 *12.4* Reverse line order
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 The |:global| command can be combined with the |:move| command to move all the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 lines before the first line, resulting in a reversed file. The command is: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 :global/^/m 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 Abbreviated: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187 :g/^/m 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 The "^" regular expression matches the beginning of the line (even if the line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 is blank). The |:move| command moves the matching line to after the mythical
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191 zeroth line, so the current matching line becomes the first line of the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 As the |:global| command is not confused by the changing line numbering,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 |:global| proceeds to match all remaining lines of the file and puts each as
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 the first.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 This also works on a range of lines. First move to above the first line and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197 mark it with "mt". Then move the cursor to the last line in the range and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 type: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 :'t+1,.g/^/m 't
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203 *12.5* Count words
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205 Sometimes you have to write a text with a maximum number of words. Vim can
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 count the words for you.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207 When the whole file is what you want to count the words in, use this
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208 command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 g CTRL-G
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 Do not type a space after the g, this is just used here to make the command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 easy to read.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 The output looks like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 Col 1 of 0; Line 141 of 157; Word 748 of 774; Byte 4489 of 4976 ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 You can see on which word you are (748), and the total number of words in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 file (774).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 When the text is only part of a file, you could move to the start of the text,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222 type "g CTRL-G", move to the end of the text, type "g CTRL-G" again, and then
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223 use your brain to compute the difference in the word position. That's a good
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 exercise, but there is an easier way. With Visual mode, select the text you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 want to count words in. Then type g CTRL-G. The result:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 Selected 5 of 293 Lines; 70 of 1884 Words; 359 of 10928 Bytes ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 For other ways to count words, lines and other items, see |count-items|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 *12.6* Find a man page *find-manpage*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234 While editing a shell script or C program, you are using a command or function
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 that you want to find the man page for (this is on Unix). Let's first use a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 simple way: Move the cursor to the word you want to find help on and press >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 K
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240 Vim will run the external "man" program on the word. If the man page is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241 found, it is displayed. This uses the normal pager to scroll through the text
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 (mostly the "more" program). When you get to the end pressing <Enter> will
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 get you back into Vim.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 A disadvantage is that you can't see the man page and the text you are working
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 on at the same time. There is a trick to make the man page appear in a Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 window. First, load the man filetype plugin: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 :runtime! ftplugin/man.vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 Put this command in your vimrc file if you intend to do this often. Now you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252 can use the ":Man" command to open a window on a man page: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 :Man csh
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256 You can scroll around and the text is highlighted. This allows you to find
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 the help you were looking for. Use CTRL-W w to jump to the window with the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 text you were working on.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 To find a man page in a specific section, put the section number first.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 For example, to look in section 3 for "echo": >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 :Man 3 echo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 To jump to another man page, which is in the text with the typical form
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 "word(1)", press CTRL-] on it. Further ":Man" commands will use the same
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 window.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 To display a man page for the word under the cursor, use this: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 \K
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 (If you redefined the <Leader>, use it instead of the backslash).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273 For example, you want to know the return value of "strstr()" while editing
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 this line:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275
693
05dc93b9c61f updated for version 7.0208
vimboss
parents: 43
diff changeset
276 if ( strstr (input, "aap") == ) ~
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278 Move the cursor to somewhere on "strstr" and type "\K". A window will open
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 to display the man page for strstr().
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 *12.7* Trim blanks
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 Some people find spaces and tabs at the end of a line useless, wasteful, and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285 ugly. To remove whitespace at the end of every line, execute the following
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288 :%s/\s\+$//
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 The line range "%" is used, thus this works on the whole file. The pattern
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 that the ":substitute" command matches with is "\s\+$". This finds white
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 space characters (\s), 1 or more of them (\+), before the end-of-line ($).
12045
444ad56c0cac Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 10198
diff changeset
293 Later will be explained how you write patterns like this, see |usr_27.txt|.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 The "to" part of the substitute command is empty: "//". Thus it replaces
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 with nothing, effectively deleting the matched white space.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296
1281
8e1a88055846 updated for version 7.1
vimboss
parents: 1226
diff changeset
297 Another wasteful use of spaces is placing them before a tab. Often these can
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 be deleted without changing the amount of white space. But not always!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 Therefore, you can best do this manually. Use this search command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 /
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 You cannot see it, but there is a space before a tab in this command. Thus
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304 it's "/<Space><Tab>". Now use "x" to delete the space and check that the
1281
8e1a88055846 updated for version 7.1
vimboss
parents: 1226
diff changeset
305 amount of white space doesn't change. You might have to insert a tab if it
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 does change. Type "n" to find the next match. Repeat this until no more
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307 matches can be found.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 *12.8* Find where a word is used
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 If you are a UNIX user, you can use a combination of Vim and the grep command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 to edit all the files that contain a given word. This is extremely useful if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 you are working on a program and want to view or edit all the files that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315 contain a specific variable.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 For example, suppose you want to edit all the C program files that contain
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317 the word "frame_counter". To do this you use the command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 vim `grep -l frame_counter *.c`
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 Let's look at this command in detail. The grep command searches through a set
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 of files for a given word. Because the -l argument is specified, the command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 will only list the files containing the word and not print the matching lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 The word it is searching for is "frame_counter". Actually, this can be any
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 regular expression. (Note: What grep uses for regular expressions is not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 exactly the same as what Vim uses.)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 The entire command is enclosed in backticks (`). This tells the UNIX shell
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 to run this command and pretend that the results were typed on the command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 line. So what happens is that the grep command is run and produces a list of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330 files, these files are put on the Vim command line. This results in Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331 editing the file list that is the output of grep. You can then use commands
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332 like ":next" and ":first" to browse through the files.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 FINDING EACH LINE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 The above command only finds the files in which the word is found. You still
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338 have to find the word within the files.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 Vim has a built-in command that you can use to search a set of files for a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 given string. If you want to find all occurrences of "error_string" in all C
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 program files, for example, enter the following command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 :grep error_string *.c
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 This causes Vim to search for the string "error_string" in all the specified
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 files (*.c). The editor will now open the first file where a match is found
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 and position the cursor on the first matching line. To go to the next
43
f55897d6921d updated for version 7.0026
vimboss
parents: 7
diff changeset
348 matching line (no matter in what file it is), use the ":cnext" command. To go
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 to the previous match, use the ":cprev" command. Use ":clist" to see all the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350 matches and where they are.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 The ":grep" command uses the external commands grep (on Unix) or findstr
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 (on Windows). You can change this by setting the option 'grepprg'.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 Next chapter: |usr_20.txt| Typing command-line commands quickly
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357
14519
5c5908e81e93 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 13963
diff changeset
358 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: