robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Eisuke Kawashimadocs: adjust modelines for HTML rendering #396497e778205ab

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