Skip to main content

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

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

Uppercase a file%file
%s/.*/\U&/
from: http://sites.netscape.net/ianjdonaldson/

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

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

deletes every line that contains "string"“string”
:g/string/d (came from http://bounce.to/vi )

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

delete every empty line:line
:g/^$/dremoved

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

Replace the following linescreatelines
create user user103 identitified by user103createuser103
create user user104 identitified by user104createuser104
create user user105 identitified by user105withgrantuser105
with
grant connect to user103grantuser103
grant connect to user104grantuser104
grant connect to user105:user105
:%s/^.*\(user.*$\)/grant connect to \1/gcopyg

copy lines 1 through 5 after line 35:35
:1,5t35copy5t35

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

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

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

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

vi - :set ignorecase for case-insensitive searching
:set noignorecase[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"2”.