vi editing notes

vi is a Unix text editor that is almost always available, on any Unix or Linux system. Knowing how to edit in it means you’ll always have an editor available, for editing config files or whatever.

To convert a unix text file to a windows/dos text file

% – the whole file (same as 1,$) s – substitute / – substitution delimeter $ – end of line \ – (1 backslash) escape next character ^M – carriage return (enter ctrl-v ctrl-m)

Uppercase a file

Replace tabs with a comma in vi

Replace tabs with two spaces. Do it with Perl:
perl -pi.anytabs -e ‘s/\t/ /g’

deletes every line that contains “string”

deletes every line that does not contain “string”

delete every empty line

remove trailing blanks
:%s/ *$//g

Replace the following lines
create user user103 identitified by user103
create user user104 identitified by user104
create user user105 identitified by user105
grant connect to user103
grant connect to user104
grant connect to user105
:%s/^.\(user.$\)/grant connect to \1/g

copy lines 1 through 5 after line 35

copy lines 1 through 5 at the end of the file

remove null lines in the file

Replace commas with Carriage Returns
:1,$s/,/CTRL-V then press Return and ^M will pop up, then /g

Trying to do regex but even simple ones won’t work
:%s#\(dec.\)#\1\1# Substitute pattern match failed
Anything with .
in it fails.
Found that we were pointing at the /usr/ucb version of vedit. Using /usr/bin/vi it works. Changed the alias to point to right one. This is a common problem where the man entry talks about the version you aren’t using.

vi – :set ignorecase for case-insensitive searching
:set noignorecase

To do a quick indent of several lines -
Go to the first line that you want to indent and type in the number of lines after that that you want to shift. Then push SHIFT + > (the greater than sign) to move your lines to the right and click SHIFT + < to move your lines to the left (you might have to push the carrot button twice). To change the indention size, you have to set the width in your vi config file. Go to your home directory and add the following line to the .exrc file: “sw=2”.

This article was originally posted on the UCLA Programmers Wiki.