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
:%s/$/\^M/

% – 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
%s/.*/\U&/
from: http://sites.netscape.net/ianjdonaldson/

Replace tabs with a comma in vi
:1,$s/\([TAB]\)/,/g

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

deletes every line that contains “string”
:g/string/d

deletes every line that does not contain “string”
:v/string/d
or
:g!/string/d

delete every empty line
:g/^$/d

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
with
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
:1,5t35

copy lines 1 through 5 at the end of the file
:1,5t$

remove null lines in the file
:g/^$/d

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

10/23/2001
Trying to do regex but even simple ones won’t work
:%s#\(dec.\)#\1\1# Substitute pattern match failed
:%s#\(dec.\)#\1\1#
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
[/literal]

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.