neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

usr_12.txt (12853B)


      1 *usr_12.txt*	Nvim
      2 
      3 
      4 	     VIM USER MANUAL	by Bram Moolenaar
      5 
      6 
      7 			Clever tricks
      8 
      9 
     10 By combining several commands you can make Vim do nearly everything.  In this
     11 chapter a number of useful combinations will be presented.  This uses the
     12 commands 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
     25 Table of contents: |usr_toc.txt|
     26 
     27 ==============================================================================
     28 *12.1*	Replace a word
     29 
     30 The substitute command can be used to replace all occurrences of a word with
     31 another word: >
     32 
     33 :%s/four/4/g
     34 
     35 The "%" range means to replace in all lines.  The "g" flag at the end causes
     36 all words in a line to be replaced.
     37   This will not do the right thing if your file also contains "thirtyfour".
     38 It would be replaced with "thirty4".  To avoid this, use the "\<" item to
     39 match the start of a word: >
     40 
     41 :%s/\<four/4/g
     42 
     43 Obviously, this still goes wrong on "fourteen".  Use "\>" to match the end of
     44 a word: >
     45 
     46 :%s/\<four\>/4/g
     47 
     48 If you are programming, you might want to replace "four" in comments, but not
     49 in the code.  Since this is difficult to specify, add the "c" flag to have the
     50 substitute command prompt you for each replacement: >
     51 
     52 
     53 :%s/\<four\>/4/gc
     54 
     55 
     56 REPLACING IN SEVERAL FILES
     57 
     58 Suppose you want to replace a word in more than one file.  You could edit each
     59 file and type the command manually.  It's a lot faster to use record and
     60 playback.
     61   Let's assume you have a directory with C++ files, all ending in ".cpp".
     62 There 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 
     77 At the last file you will get an error message, because ":wnext" cannot move
     78 to 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 
     84 There 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
     86 the "e" flag to the substitute command: >
     87 
     88 :%s/\<GetResp\>/GetAnswer/ge
     89 
     90 The "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 
     95 You have a list of names in this form:
     96 
     97 Doe, John ~
     98 Smith, Peter ~
     99 
    100 You want to change that to:
    101 
    102 John Doe ~
    103 Peter Smith ~
    104 
    105 This can be done with just one command: >
    106 
    107 :%s/\([^,]*\), \(.*\)/\2 \1/
    108 
    109 Let's break this down in parts.  Obviously it starts with a substitute
    110 command.  The "%" is the line range, which stands for the whole file.  Thus
    111 the substitution is done in every line in the file.
    112   The arguments for the substitute command are "/from/to/".  The slashes
    113 separate the "from" pattern and the "to" string.  This is what the "from"
    114 pattern 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 
    125 In the "to" part we have "\2" and "\1".  These are called backreferences.
    126 They refer to the text matched by the "\( \)" parts in the pattern.  "\2"
    127 refers 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
    130 command.  "\0" stands for the whole matched pattern.  There are a few more
    131 special items in a substitute command, see |sub-replace-special|.
    132 
    133 ==============================================================================
    134 *12.3*	Sort a list
    135 
    136 In 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 
    148 To sort this list, filter the text through the external sort command: >
    149 
    150 /^OBJS
    151 j
    152 :.,/^$/-1!sort
    153 
    154 This goes to the first line, where "OBJS" is the first thing in the line.
    155 Then it goes one line down and filters the lines until the next empty line.
    156 You could also select the lines in Visual mode and then use "!sort".  That's
    157 easier 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 
    171 Notice that a backslash at the end of each line is used to indicate the line
    172 continues.  After sorting, this is wrong!  The "backup.o" line that was at
    173 the end didn't have a backslash.  Now that it sorts to another place, it
    174 must have a backslash.
    175   The simplest solution is to add the backslash with "A \<Esc>".  You can
    176 keep the backslash in the last line, if you make sure an empty line comes
    177 after it.  That way you don't have this problem again.
    178 
    179 ==============================================================================
    180 *12.4*	Reverse line order
    181 
    182 The |:global| command can be combined with the |:move| command to move all the
    183 lines before the first line, resulting in a reversed file.  The command is: >
    184 
    185 :global/^/move 0
    186 
    187 Abbreviated: >
    188 
    189 :g/^/m 0
    190 
    191 The "^" regular expression matches the beginning of the line (even if the line
    192 is blank).  The |:move| command moves the matching line to after the imaginary
    193 zeroth line, so the current matching line becomes the first line of the file.
    194 As 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
    196 the first.
    197 
    198 This also works on a range of lines.  First move to above the first line and
    199 mark it with "mt".  Then move the cursor to the last line in the range and
    200 type: >
    201 
    202 :'t+1,.g/^/m 't
    203 
    204 ==============================================================================
    205 *12.5*	Count words
    206 
    207 Sometimes you have to write a text with a maximum number of words.  Vim can
    208 count the words for you.
    209   When the whole file is what you want to count the words in, use this
    210 command: >
    211 
    212 g CTRL-G
    213 
    214 Do not type a space after the g, this is just used here to make the command
    215 easy 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 
    220 You can see on which word you are (748), and the total number of words in the
    221 file (774).
    222 
    223 When the text is only part of a file, you could move to the start of the text,
    224 type "g CTRL-G", move to the end of the text, type "g CTRL-G" again, and then
    225 use your brain to compute the difference in the word position.  That's a good
    226 exercise, but there is an easier way.  With Visual mode, select the text you
    227 want 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 
    231 For other ways to count words, lines and other items, see |count-items|.
    232 
    233 ==============================================================================
    234 *12.6*	Find a man page					*find-manpage*
    235 
    236 While editing a shell script or C program, you are using a command or function
    237 that you want to find the man page for (this is on Unix).  Let's first use a
    238 simple way: Move the cursor to the word you want to find help on and press >
    239 
    240 K
    241 
    242 Nvim will run |:Man| on the word.  If the man page is found, it is displayed.
    243 You can also use the |:Man| command to open a window on a man page: >
    244 
    245 :Man csh
    246 
    247 You can scroll around and the text is highlighted.  This allows you to find
    248 the help you were looking for.  Use CTRL-W w to jump to the window with the
    249 text you were working on.
    250   To find a man page in a specific section, put the section number first.
    251 For example, to look in section 3 for "echo": >
    252 
    253 :Man 3 echo
    254 
    255 To 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
    257 window.
    258 
    259 To display a man page for the word under the cursor, use this: >
    260 
    261 K
    262 
    263 For example, you want to know the return value of "strstr()" while editing
    264 this line:
    265 
    266 if ( strstr (input, "aap") == ) ~
    267 
    268 Move the cursor to somewhere on "strstr" and type "K".  A window will open
    269 to display the man page for strstr().
    270 
    271 ==============================================================================
    272 *12.7*	Trim blanks
    273 
    274 Some people find spaces and tabs at the end of a line useless, wasteful, and
    275 ugly.  To remove whitespace at the end of every line, execute the following
    276 command: >
    277 
    278 :%s/\s\+$//
    279 
    280 The line range "%" is used, thus this works on the whole file.  The pattern
    281 that the ":substitute" command matches with is "\s\+$".  This finds white
    282 space characters (\s), 1 or more of them (\+), before the end-of-line ($).
    283 Later 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
    285 with nothing, effectively deleting the matched white space.
    286 
    287 Another wasteful use of spaces is placing them before a tab.  Often these can
    288 be deleted without changing the amount of white space.  But not always!
    289 Therefore, you can best do this manually.  Use this search command: >
    290 
    291 / 	
    292 
    293 You cannot see it, but there is a space before a tab in this command.  Thus
    294 it's "/<Space><Tab>".   Now use "x" to delete the space and check that the
    295 amount of white space doesn't change.  You might have to insert a tab if it
    296 does change.  Type "n" to find the next match.  Repeat this until no more
    297 matches can be found.
    298 
    299 ==============================================================================
    300 *12.8*	Find where a word is used
    301 
    302 If you are a Unix user, you can use a combination of Vim and the grep command
    303 to edit all the files that contain a given word.  This is extremely useful if
    304 you are working on a program and want to view or edit all the files that
    305 contain a specific variable.
    306   For example, suppose you want to edit all the C program files that contain
    307 the word "frame_counter".  To do this you use the command: >
    308 
    309 vim `grep -l frame_counter *.c`
    310 
    311 Let's look at this command in detail.  The grep command searches through a set
    312 of files for a given word.  Because the -l argument is specified, the command
    313 will only list the files containing the word and not print the matching lines.
    314 The word it is searching for is "frame_counter".  Actually, this can be any
    315 regular expression.  (Note: What grep uses for regular expressions is not
    316 exactly the same as what Vim uses.)
    317   The entire command is enclosed in backticks (`).  This tells the Unix shell
    318 to run this command and pretend that the results were typed on the command
    319 line.  So what happens is that the grep command is run and produces a list of
    320 files, these files are put on the Vim command line.  This results in Vim
    321 editing the file list that is the output of grep.  You can then use commands
    322 like ":next" and ":first" to browse through the files.
    323 
    324 
    325 FINDING EACH LINE
    326 
    327 The above command only finds the files in which the word is found.  You still
    328 have 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
    330 given string.  If you want to find all occurrences of "error_string" in all C
    331 program files, for example, enter the following command: >
    332 
    333 :grep error_string *.c
    334 
    335 This causes Vim to search for the string "error_string" in all the specified
    336 files (`*.c`).  The editor will now open the first file where a match is found
    337 and position the cursor on the first matching line.  To go to the next
    338 matching line (no matter in what file it is), use the ":cnext" command.  To go
    339 to the previous match, use the ":cprev" command.  Use ":clist" to see all the
    340 matches 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 
    346 Next chapter: |usr_20.txt|  Typing command-line commands quickly
    347 
    348 Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl: