USR_12

*usr_12.txt*    For IdeaVIM version 0.12.0.  Last change: 2006 Nov 12

                     IdeaVIM USER MANUAL - by Rick Maddy

                                Clever tricks


By combining several commands you can make Vim do nearly everything.  In this
chapter a number of useful combinations will be presented.  This uses the
commands introduced in the previous chapters and a few more.

|12.1|  Replace a word
|12.2|  Change "Last, First" to "First Last"
|12.3|  Sort a list
|12.4|  Reverse line order
|12.5|  Count words
|12.6|  Find a man page
|12.7|  Trim blanks
|12.8|  Find where a word is used

     Next chapter: |usr_20.txt|  Typing command-line commands quickly
 Previous chapter: |usr_11.txt|  Recovering from a crash
Table of contents: |usr_toc.txt|


*12.1*  Replace a word

The substitute command can be used to replace all occurrences of a word with
another word:

        :%s/four/4/g

The "%" range means to replace in all lines.  The "g" flag at the end causes
all words in a line to be replaced.
   This will not do the right thing if your file also contains "thirtyfour".
It would be replaced with "thirty4".  To avoid this, use the "\<" item to
match the start of a word:

        :%s/\<four/4/g

Obviously, this still goes wrong on "fourty".  Use "\>" to match the end of a
word:

        :%s/\<four\>/4/g

If you are programming, you might want to replace "four" in comments, but not
in the code.  Since this is difficult to specify, add the "c" flag to have the
substitute command prompt you for each replacement:


        :%s/\<four\>/4/gc


REPLACING IN SEVERAL FILES

Suppose you want to replace a word in more than one file.  You could edit each
file and type the command manually.  It's a lot faster to use record and
playback.
   Let's assume you have a directory with C++ files, all ending in ".cpp".
There is a function called "GetResp" that you want to rename to "GetAnswer".

        vim *.cpp               Start Vim, defining the argument list to
                                contain all the C++ files.  You are now in the
                                first file.
        qq                      Start recording into the q register
        :%s/\<GetResp\>/GetAnswer/g
                                Do the replacements in the first file.
        :wnext                  Write this file and move to the next one.
        q                       Stop recording.
        @q                      Execute the q register.  This will replay the
                                substitution and ":wnext".  You can verify
                                that this doesn't produce an error message.
        999@q                   Execute the q register on the remaining files.

At the last file you will get an error message, because ":wnext" cannot move
to the next file.  This stops the execution, and everything is done.

        Note:
        When playing back a recorded sequence, an error stops the execution.
        Therefore, make sure you don't get an error message when recording.

There is one catch: If one of the .cpp files does not contain the word
"GetResp", you will get an error and replacing will stop.  To avoid this, add
the "e" flag to the substitute command:

        :%s/\<GetResp\>/GetAnswer/ge

The "e" flag tells ":substitute" that not finding a match is not an error.


*12.2*  Change "Last, First" to "First Last"

You have a list of names in this form:

        Doe, John 
        Smith, Peter 

You want to change that to:

        John Doe 
        Peter Smith 

This can be done with just one command:

        :%s/\([^,]*\), \(.*\)/\2 \1/

Let's break this down in parts.  Obviously it starts with a substitute
command.  The "%" is the line range, which stands for the whole file.  Thus
the substitution is done in every line in the file.
   The arguments for the substitute command are "/from/to/".  The slashes
separate the "from" pattern and the "to" string.  This is what the "from"
pattern contains:
                                                        \([^,]*\), \(.*\) 

        The first part between \( \) matches "Last"     \(     \)
            match anything but a comma                    [^,]
            any number of times                               *
        matches ", " literally                                   ,
        The second part between \( \) matches "First"              \(  \)
            any character                                            .
            any number of times                                       *

In the "to" part we have "\2" and "\1".  These are called backreferences.
They refer to the text matched by the "\( \)" parts in the pattern.  "\2"
refers to the text matched by the second "\( \)", which is the "First" name.
"\1" refers to the first "\( \)", which is the "Last" name.
   You can use up to nine backreferences in the "to" part of a substitute
command.  "\0" stands for the whole matched pattern.  There are a few more
special items in a substitute command, see |sub-replace-special|.


*12.3*  Sort a list

In a Makefile you often have a list of files.  For example:

        OBJS = \ 
                version.o \ 
                pch.o \ 
                getopt.o \ 
                util.o \ 
                getopt1.o \ 
                inp.o \ 
                patch.o \ 
                backup.o 

To sort this list, filter the text through the external sort command:

        /^OBJS
        j
        :.,/^$/-1!sort

This goes to the first line, where "OBJS" is the first thing in the line.
Then it goes one line down and filters the lines until the next empty line.
You could also select the lines in Visual mode and then use "!sort".  That's
easier to type, but more work when there are many lines.
   The result is this:

        OBJS = \ 
                backup.o 
                getopt.o \ 
                getopt1.o \ 
                inp.o \ 
                patch.o \ 
                pch.o \ 
                util.o \ 
                version.o \ 


Notice that a backslash at the end of each line is used to indicate the line
continues.  After sorting, this is wrong!  The "backup.o" line that was at
the end didn't have a backslash.  Now that it sorts to another place, it
must have a backslash.
   The simplest solution is to add the backslash with "A \<Esc>".  You can
keep the backslash in the last line, if you make sure an empty line comes
after it.  That way you don't have this problem again.


*12.4*  Reverse line order

This functionality is not currently support by IdeaVIM.


*12.5*  Count words

This functionality is not currently support by IdeaVIM.


*12.6*  Find a man page                                 *find-manpage*

This functionality is not currently support by IdeaVIM.

Though similar functionality is provided. The "K" command activates IDEA's
Quick Documentation Lookup.


*12.7*  Trim blanks

Some people find spaces and tabs at the end of a line useless, wasteful, and
ugly.  To remove whitespace at the end of every line, execute the following
command:

        :%s/\s\+$//

The line range "%" is used, thus this works on the whole file.  The pattern
that the ":substitute" command matches with is "\s\+$".  This finds white
space characters (\s), 1 or more of them (\+), before the end-of-line ($).
Later will be explained how you write patterns like this |usr_27.txt|.
   The "to" part of the substitute command is empty: "//".  Thus it replaces
with nothing, effectively deleting the matched white space.

Another wasteful use of spaces is placing them before a Tab.  Often these can
be deleted without changing the amount of white space.  But not always!
Therefore, you can best do this manually.  Use this search command:

        /

You cannot see it, but there is a space before a tab in this command.  Thus
it's "/<Space><Tab>".   Now use "x" to delete the space and check that the
amount of white space doesn't change.  You might have to insert a Tab if it
does change.  Type "n" to find the next match.  Repeat this until no more
matches can be found.


*12.8*  Find where a word is used

This information does not apply to IdeaVIM.


Next chapter: |usr_20.txt|  Typing command-line commands quickly

Copyright: see |manual-copyright|