vi-tips
- Move to line 1.
-
:1 --(: command)
1G --(G command)
- Move to line 444 --(: command).
-
:444
--(oops: vi error = "Illegal address: only 410 lines in the file." )
- Move to line 444 --(G command).
-
444G
--(oops: STILL only 410 lines, but vi reports NO error message)
- Spell check a document without exiting vi.
-
:!spell % > %.sp (%=present_filename)
:e %.sp (Edit new shell checked document)
:e# (To get back to your document)
- Remove blank lines from a file.
-
:g/^$/d (:g globally delete blank lines)
:v/./d (:v opposite :g, so :v/./ == NOT_BLANK)
:%!cat -s ("cat -s" deletes dup blanklines)
- Remove duplicate blank lines leaving first blank line intact.
-
:%!sed /./,/^$/!d
-- % in range = entire file
-- ! = send what follows on line as argument(s) to the shell
-- sed /./,/^$/!d = xxx
- Get rid of trailing whitespace.
-
:%s/[ ^I]*$/ (Substitute all spaces and/or tabs.)
Writing from buffers
- Save contents of a buffer to filename:
-
:e filename^M -- edit filename
"ap -- "ap = print buffer a
:w -- write out filename
-
Edit a new file and put 'a's contents in it.
(to save it)
- Save part of a file to another file:
- mark text at the top of the region to be saved
- ma
- Mark text at the bottom of the region to be saved
- mb
- From marker 'a to marker 'b write to filename
- :'a,'b w filename
Switching cases
:X/pattern_1/Y/pattern_2/pattern_3/Z
:RANGEs/pattern_1/Y/pattern_2/Z
| | |
:1,111s/uglyduck/bigduck/g
| | |
In the replacement part of a substitution command,
i.e. between the second "/" and third "/",
\u means make the following character upper case
\l means make the following character lower case
\U means make the rest of the replacement upper case
\L means make the rest of the replacement lower case
How about a few examples?
1. Make the first letter of every word from line 18 to 43 uppercase.
From lines 18 to 43, substitute "<" with "&Amp;", globally per line.
:18,43s/\<./\u&/g
2. Change "uPPeR" and "LoweR" in any mixture of cases to lowercase.
substitute
:s/[UuLl][PpOo][PpWw][Ee][Rr]/\L&/
3. Make the whole file uppercase.
:%s/.*/\U&/
4. Make the region from line m to line n all uppercase.
:'m,'ns/.*/\U&/
5. Make a paragraph all lowercase.
:?^$?,/^$/s/.*/\L&/
6. Make the first letter of every word in a paragraph uppercase.
:?^$?,/^$/s/\([^ ][^ ]*\)/\u&/g
7. Make the second word of each line uppercase.
:1,$s/^\([^ ]*\) \([^ ]*\) \(.*\)/\1 \U\2\e \3/
| Advanced search and replace | To change Gravity isn't just a "good idea." It's the "law." to Gravity isn't just a ``good idea.'' It's the ``law.'' type :g/$/s/"\([^"]*\)"/``\1''/g The following will find words that begin and end with a vowel: /\<[aeiouAEIOU][a-zA-Z']*[aeiouAEIOU]\> |
| Line deletions |
| :g/string/d | deletes every line that contains string, while |
| :v/string/d | deletes every line that does not contain string. |
| Creating a horizontal ruler |
Ever want to know what character space you are on a
line? What I do is to create a horizontal ruler, then
store it in one of my non-volitile paste buffers for
later use in the session.
1. 7i.........!<esc> creates a seventy-character bar consisting of seven sets of '.........!'.
2. "cdd removes the newly created bar to buffer c.
3. Later, when you need the bar, just do "cp and it will appear, like this:.
.........!.........!.........!.........!.........!.........!.........!
If you have perl on your system, replace step one (7i.........!) with the following:
:r !perl -e 'for ($i=1;$i<8;$i+=1) {print ".........$i";} print ".........\n";'
you get this ruler instead!
.........1.........2.........3.........4.........5.........6.........7.........
Shell commands to operate on a line, range or paragraph
I mentioned use of the !}fmt command during the discussion of the .exrc file.
Using fmt as an example again, here are the three common ways to use it:
* :.!fmt to format one line.
* !!fmt better way to format one line.
* !}fmt to format one paragraph.
* :.,$!fmt to format from the current line to the end of the document.
* :.,/searchstring/!fmt to format from the current line to any occurance of searchstring.
Of course, any reasonable shell formatting command may be used in
this way, and fmt itself may be used with options, such as fmt
-60 to change from the default line length to sixty.
|
| Reading in from the shell |
I have another command I use quite frequently with vi,
which is to read in something from the shell. While
this is quite an obvious trick when one reads the
command reference, I find that many folks miss this
one. Use this whenever you want to include text which
is the output of a command. Let's use, as an example,
the case where one wants a copy of the current months
calendar:
:r !cal
or
!!cal
Pretty simple, huh? Remember, this will replace the
contents of the line it was executed on, so use it on a
blank line if you don't want to replace something.
Appending the current line elsewhere
Here is one more I often use; the case is when I am
editing a file and want one line to go on to the end of
another file.
First move to the line in question, then do the
following:
:.w >> filename
Writing out lines between marks
If you want to write out a file using the lines from
one mark to another, there is an easy way suggested by
John Burns
(john.h.burns@boeing.invalid_address_for_spammers_remove_to_reply.com).
Using the m mark command, and a pair of the twenty-six
available markers (a to z), you can mark an area of the
file you are in, say ma and mb, then write that section
out:
:'a,'bw filename
or
:'a,'bw >> filename
to append.
Getting a chunk from one file to another
Often-times, one finds that one wants to take
something from one file and put it into another. Let's
say that one wants a paragraph from file foo to be put
into file bar. Here are the steps:
1. vi foo bar 2. go to the paragraph you want to take,
and position on the first character of the first line.
3. "ay} is executed.
This says, put into non-volitile buffer a what is
yanked from the current cursor-position to the
end-of-paragraph (denoted by the '}' character). 4.
type in , if you are using my .exrc file, or :n if you
aren't. 5. find the spot you want to insert the yanked
stuff. 6. type in "ap. That's it!
|
| |
| reference site | http://unix.t-a-y-l-o-r.com/Vi.html |