| |
IRIX: system: IRIX: tricks and fun with vi (last edit: 2003-01-05)
tricks and fun with vi Under IRIX
_____________________________________________________________________________
set list show tabs ^I and end of line $ (set nolist)
set ic ignore case
set shell
_____________________________________________________________________________
:sh open a shell
_____________________________________________________________________________
change or replace text
in place of '^I' use the tab key !!!
:s/^/^I/ go to beginning of line and insert 3x tabs
:g/^/s//^I/g do the same as above but for each line in the file
:36,39s/^/^I/ from line 36 to line 39 insert a tab (replace
beginning of line with a tab
genrela syntax:
:[adr1[,adr2]]s/alt/neu/[flags] flags: c confirm
g global
p print changed lines
replace ALL occurence in a file:
--------------------------------
Add comment from the first (1) to the last ($) line in file:
:1,$ s/^/# /g
Add comment from the current (.) line and the next 9 (+9) lines
:.,+9 s/^/# /g
replace on word (suchmuster1) by another (suchmuster2)
:1,$ s/suchmuster1/suchmuster2/g
oder
:%s/suchmuster1/suchmuster2/g
_____________________________________________________________________________
& repeat last :s argument
_____________________________________________________________________________
cursor movement
===============
H high go to top of the screen
M middle go to middle of screen
L low go to bottomw of screen
cursor is at a certain line
z+ redraw screen with that line at the top of the screen
z. redraw screen with that line in the middle
z- redraw screen with that line at the bottom
^ means key
n^f move screens forward
n^e scroll window down lines
n^y scroll window up lines
mx mark cursor position with character x
`x move to position marked with x
'x move to beginning of line where marker x resides
go to another position
y'x yank from current position to line where marker x resides
d'x delete from current position until line with x (including that line)
13| go to column 13
13 go forward 13 characters
_____________________________________________________________________________
deleting copying and moving text
=================================
"bdd delete a line and write it into buffer -b-
"b4dw delete next 4 words and write it into buffer -b-
"bp retrieve contents of buffer -b- (puts content of buffer after cursor)
"bP retrieve contents of buffer -b- (puts content of buffer before cursor)
"b5yw yank copy of next 5 characters into buffer -b-
3X delete 3 characters LEFT to cursor
R write over existing text
>> move this line one tab to right
<< move this line one tab to left
:set sw 4 #and set the shift width to 4 characters
:10,25>> apply that command from line 10 to line 25
_____________________________________________________________________________
~HOME/.exrc
set nu
# underl is an abbreviation for 80x_
ab underl ___________________________________________________________________
_____________________________________________________________________________
mapping:
vi .exrc
i insert mode.
map (control v)(then key you want, say up arrow) k
the line comes out like:
map ^[[A k
(escape) end insert mode
:wq write file and quit
next time he starts vi, the key is mapped.
_____________________________________________________________________________
:%!xxd // wechselt in die Hexansicht...
:%!xxd -r // ... undwieder zurueck
_____________________________________________________________________________
UNIX GURU UNIVERSE
APPENDING TEXT AT BOTH ENDS
If you need to append some
text to both the ends of a
string in vi, use the
following command in vi:
:/^\(.*\)/s//starttextcomeshere \1 endtextcomeshere/
If "iamworthless" is the text
at the position of the cursor
in the vi editor, the output
of the command:
:/^\(.*\)/s//DONTFEEL \1 ANYTIME/
DONTFEEL iamworthless ANYTIME
As you guessed, "DONTFEEL " and
" ANYTIME" are the strings you
would like to crush
"iamworthless" with. The "\1" is
the variable that stores the
entire pattern searched by
the "/\(.*\)/"
_____________________________________________________________________________
SPLIT SCREEN EDITING
You can split screen into multiple windows in vi.
(Depending on your version of VI)
TO create the empty window enter the command:
:new
TO move between the screens use the commands:
To Move to lower window j
Move to upper window k
TO make the current window the only window
on the screen closing all other windows, use:
:only
VIM supports this feature and can be
found at: http://www.math.fu-berlin.de/~guckes/vim/
nick: above tips works fine for vim but NOT for IRIX 6.5 vi
_____________________________________________________________________________
WHERE ARE THOSE PARENTHESES?
Do you ever lose your way in multiple levels of parentheses
or braces? If you program in Perl or C, you know what I
mean. Let the vi "%" command help you. Move the cursor to
a parenthesis or brace and type %. vi will move the
cursor to the corresponding character. Hit % again and vi
will return to the original parenthesis or brace.
For extra power, issue "/{" to find the first open brace.
Hit "%" twice to find the corresponding brace and return.
Hit "n" to find the next open brace. This allows you to
zip through the file checking your program structure.
It makes you feel sorry for Windows-based editors!
_____________________________________________________________________________
COMMENT OUT MULTIPLE LINES in vi
Ever wanted to comment out multiple lines of code while
writing shell scripts in vi, but didn't want
to pound your keyboard for half an hour doing it?
(I#Escape j.j.j.j. = carpal tunnel)
Here's the nerdy way to do it:
:.,+N-1 s/^/#/g
Where N-1 is the number of lines minus one that you want to comment
out, and s/^/#/g is the regular expression (the pattern between the
first two slashes is what you want to replace, in this case the
beginning of the line, and the pattern between the last two slashes
is what you want to replace it with).
For example, if you're a bad
coder like me and want to comment
out 500 lines of code, you would do:
:.,+499 s/^/#/g
_____________________________________________________________________________
APPENDING TEXT AT BOTH ENDS
If you need to append some text to both the ends of a string in
vi, use the following command:
:/^\(.*\)/s//DONTFEEL \1 ANYTIME/
If "iamworthless" is the text at the position of the cursor in
the vi editor, the output of the command would be
DONTSAY iamworthless ANYTIME
As you guessed, "DONTSAY " and " ANYTIME" are the strings you
would like to crush "iamworthless" with. The "\1" is the variable
that stores the entire pattern searched by the "/\(.*\)/"
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
VI FILE REMOVAL
If you have opened several files in vi and you want to remove the
file you are currently viewing, then you can map the F1 key for
removing the file like:
:map #1 :!rm %^M^M
Just press F1 and the file will be removed. ^M is put by pressing
Ctrl-V and Ctrl-M one after another.
Nick:
map other useful things to F1:
:map #1 :r !ls -l /CDROM/dist
pressing F1 & return will
read in the output of the UNIX command 'ls -l /CDROM/dist'
_____________________________________________________________________________
from UGU:
WHERE ARE THOSE PARENTHESES?
Do you ever lose your way in multiple levels of parentheses
or braces? If you program in Perl or C, you know what I
mean. Let the vi "%" command help you. Move the cursor to
a parenthesis or brace and type %. vi will move the
cursor to the corresponding character. Hit % again and vi
will return to the original parenthesis or brace.
For extra power, issue "/{" to find the first open brace.
Hit "%" twice to find the corresponding brace and return.
Hit "n" to find the next open brace. This allows you to
zip through the file checking your program structure.
It makes you feel sorry for Windows-based editors!
_____________________________________________________________________________
REMOVING CONTROL CHARACTERS
Have you ever faced problem of removing
Control Characters from Multiple Files,
Here is the solution create script file
with following entries,
vi -c "%s/^M//g" -c "wq" junk1.c
vi -c "%s/^M//g" -c "wq" junk2.c
vi -c "%s/^M//g" -c "wq" junk3.c
vi -c "%s/^M//g" -c "wq" junk4.c
vi -c "%s/^M//g" -c "wq" junk5.c
vi -c "%s/^M//g" -c "wq" junk6.c
If the filename of this script file
is "xyz":
#./xyz
This will remove the control characters(^M)
from all the files junk1.c ... junk6.c
To create ^M = Holding the control
press 'v' & 'm'.
To create ^H = Holding the control press
'v' & 'h'.
_____________________________________________________________________________
CLEANUP DOS FILES
If you deal with DOS files and the "^M" character
always appears at the end of the line, here are
two ways to get rid of them.
If you edit the DOS text file with the "vi"
editor in UNIX, use the following from the
"vi" command line:
:%s/^V^M//g
>From a Unix shell use the command:
% sed 's/^V^M//g' foo > foo.new
NOTE: ^V is control V and ^M is control M or Enter
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
GETTING RID OF ^M
How to get rid of ^M in text files using vi.
:1,$ s'^M''g
But how to get this '^M' in the command line:
Keep CTRL pressed and then press v and m
_____________________________________________________________________________
READ IN THE OUTPUT
While in VI, if you enter:
:r !my_command
Then you will insert the output of the command you've entered on the
next line of the file that you're editing.
_____________________________________________________________________________
CONVERTING CAPS IN VI
To convert:
THE CAT SAT ON THE MAT
to
The Cat Sat On The Mat
in vim, add the following to .vimrc:
map #1 :s/\([A-Z]\)\([A-Z][A-Z]*\)/\1\L\2/g
It maps a suitable regular-expression based search and replace to the F1
function key.
_____________________________________________________________________________
A VI FILE REVERSAL
A file reversal if you dont have tac or want to use a script.
Edit the file in vi and type:
:g/.*/m0
_____________________________________________________________________________
VI COPING BETWEEN FILES
VI editor is a very powerful editor. Suppose you want to copy
line numbers 1-10 and 23-77 of file A and paste them on file B at
the same time, what will you do ?? Here I am giving a tip
to do this :-
Place the cursor on line no. 1 Press ESCAPE
Press Shift "(Shift double quotes) Press a
Press 10yy [the number of lines you want to copy]
Now place the cursor on line no. 23 Press ESCAPE
Press Shift "(Shift double quotes) Press b
Press 55yy [the number of lines you want to copy]
Now the first 10 lines have been copied in buffer "a" and next
55 lines have been copied in buffer "b".
Now press " : " (COLON) to get the vi prompt.
Hit e "Destination file name"
******************
nick: this is a good one; with " : e "
we can open another file without loosing the content of our buffers !!
******************
Once you enter the Destination file go to the line where you
want the first 10 lines in buffer "a" to be inserted.
Press ESCAPE. Press Shift "(Shift double quotes)
Press a. Press p.
The first 10 lines in buffer "a" gets inserted.
Now go to the line where you want the rest 55 lines in buffer
"b" to be inserted.
Press ESCAPE. Press Shift "(Shift double quotes)
Press b. Press p.
The rest 55 lines in buffer "b" gets inserted.
_____________________________________________________________________________
VI REVERSAL
In vi editor you can reverse the order of lines by just
executing the following ed command at the : prompt.
g/^/m0
_____________________________________________________________________________
|