Operating System - HP-UX
1827594 Members
2659 Online
109965 Solutions
New Discussion

Some Tips & Tricks for "VI Editor"

 
SOLVED
Go to solution
Shahbaz_1
Regular Advisor

Some Tips & Tricks for "VI Editor"

Hi Friends,
Please give some tips, to work better and smart with VI editor.
Anything related to vi, from setting up terminal for vi editor to editing multiple files etc

Thanks in Adv

Regards
Shah
Let's share the great thing "THE KNOWLEDGE"
32 REPLIES 32
Mateja Bezjak
Respected Contributor

Re: Some Tips & Tricks for "VI Editor"

Hi,

Here is a link to VI tutorial
http://ecn.www.ecn.purdue.edu/ECN/Documents/VI/

Regards,
Mateja
Michael Tully
Honored Contributor
Solution

Re: Some Tips & Tricks for "VI Editor"

Here's two simple tricks that are used almost everyday to change one string to another.

Say you want to change in a file the string joe to fred.

From command mode:
:1,$s/joe/fred/g

To insert a string at the beginning
of the line
:1,$s/^/insert/g

If you wanted to do this at the end
of the line replace the '^' with a
'$'

If you wished to change multiple files
it might be best to use 'sed'
Anyone for a Mutiny ?
steven Burgess_2
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

Sha

Search anad replace

/text - search forward direction starting from curretn location
?text search text backward
/ repeat previous search in forward dir
? repeat previous searcn backward dir
n repeat search in same dir
N repeat search in opposite dir
:s/old/new search old and replace with new
:m,ns/old/new/g search old from line m and replace it to line n with new
:s/old/new/g search old in entire file and replace with new
/text a space between the / and text, only whole words are searched
/^text search text at the beginning of a line
/text$ text at the end of a line
/(more than one word) - use parenthesis to search for more than one word

Exporting text from a current file

If you have opened file1 in the editor and want to save lines 3 to 47 as file3, use the command :3,47w file3

to save the opened file as a new file :w filename

to open multiple files

vi file1 file2 file3

vi options are configured in $HOME/.exrc

to set line number , enter in .exrc

set nu
set showmode

to show current mode

Theres loads more

Steve
take your time and think things through
Michael Tully
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

Here is a link to some information regarding 'sed'

http://www.math.fu-berlin.de/~guckes/sed/

To really learn some powerful text editing and programming you may also want to look at perl.

Start here:
http://www.perl.org/
http://www.cpan.org
Anyone for a Mutiny ?
steven Burgess_2
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

Shah

I have attached a whole load of sed examples

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

Here they are
take your time and think things through
Chris Wilshaw
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

typing

:set list

displays non-printable characters (hidden control characters etc). Can be invaluable in debugging a faulty script or data.
-------------------------------------

use J to join append the next line to the end of the current line.

-------------------------------------
Cut and paste file section to new file

At first line, hit
m
where the buffer is a letter

Go to end of section you want to copy, and type

"ay'

You should now see a message telling you that a number of lines have been yanked.

type

:e
to start a new file, then

"ap

to paste the contents of the buffer to the new file.
--------------------------------------
Bill McNAMARA_1
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

:set all
to view special settings

ie
:set noautoindent
to avoid cut paste problems..

Also get a shell with

:!ksh

exit
(to return to vi)

:!ls /tmp

or any other command to run a command.

Later,
Bill



It works for me (tm)
Frank Slootweg
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

See also the standard HP-UX documentation, i.e. for example "Using the vi Editor" in the "Using Your HP Workstation" (or similar) manual.

For on-line versions, see <>.
Martin Johnson
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

:.= show current line number
:= show how many lines in the file.

HP has a book "The Ultimate guide to the VI and EX Text Editors" that will tell you everything you ever want to know about vi.

HTH
Marty
Jeanine Kone
Trusted Contributor

Re: Some Tips & Tricks for "VI Editor"

I like to use:

:r filename

to insert the contents of the specified file into the current file you are editing. I use this alot to add headers and standard env setups to my scripts.
BKUMAR
Frequent Advisor

Re: Some Tips & Tricks for "VI Editor"

Hi

VVVV IMP to open a large files

[
vi
:set dir=/
:e

]

so that you have to set the largefilesystem with enough space of your largefile....so vi default searches for /var/tmp....if it doesnt have enought space of your largefile....

it wont open it completely and if you save it accidentally....it will be a problem

take care

Unix Administration Most Dedicated and Challenging Career in the World
Tony Romero
Advisor

Re: Some Tips & Tricks for "VI Editor"

Use the following to convert alpha chars from lower to upper case.

sed '1,3y/abcde/ABCDE/' filename

Freedom!!!
John Dvorchak
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

A very simple one that I seem to use every session is move to the 1st line in the file:

/1G

Last line in the file:
/G

Search for a string or number etc..:
/keyword_that_you_want_to_search_for
n (to repeat the last find forward)
N (to repeat the last find backward)

Take me to the end of the line and put me in insert mode so I can add text:
A

All of the responses were great but I find that these are the most used, plus search and replace already given to you.

If it has wheels or a skirt, you can't afford it.
harry d brown jr
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"


http://www.talug.org/minutes/19990310/lesson/lesson.html

http://computips.virtualave.net/tips/vi_commands.html

http://www.roxanne.org/vi.html


How many of us have done this:

ls -l /somedir > /tmp/dirlst.txt
then
vi /tmp/dirlst.txt

when we could have done this

vi /tmp/dirlst.txt
!!ls -l /somedir

live free or die
harry
Live Free or Die
kish_1
Valued Contributor

Re: Some Tips & Tricks for "VI Editor"

1.if you want interchange the letter when you make mistake while typeing
Ans: xp

2. if you want opn a last line of file

Ans: vi + /var/adm/syslog/syslog.log

share the power of the knowledge
Robert Sipe
Occasional Advisor

Re: Some Tips & Tricks for "VI Editor"

with regards to search and replace

:%s/old/new/gc

Where the g is global and the c is confirm before commit.
None
Robert Sipe
Occasional Advisor

Re: Some Tips & Tricks for "VI Editor"

if you need to include a calendar into your vi session

:! /usr/bin/cal [arg]

of course that will work with any shell command.
None
Ralph Grothe
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

Hi,

I guess one could fill a whole book with cute little vi tricks.
Here are some things I appreceate with vi.
Maybe they have already been addressed?

Shell escapes are a really nice feature (but sometimes dreaded by paranoid sysadmins ;-)

You have already seen the bang of the ex commands

:! ll /var/adm

But what I think has been missed is the combination with ex's read command.
How about dumping the output of a command in your current vi buffer?

:r! ll /var/adm

This come really handy to me, when I am too lazy to type endless paths to commands that usually don't reside under /usr/bin

:r! which perl

Then you can escape to the shell without leaving your vi session when you have several commands to issue

:sh

After your commands type exit to get back to your vi session.

Have you already discovered the power of mappings?
Edit your .exrc file, and insert a mapping like this:
(This is to insert a standard Perl header when pressing the F1 key)

map ^[[001q :r!which perl^M^[I#!^[A -w^M^Muse strict;^M^M

You get the control characters when you typ Ctrl+v (or C^V) together, which quotes the next key.
Madness, thy name is system administration
Steve Post
Trusted Contributor

Re: Some Tips & Tricks for "VI Editor"

I'm in a directory with a pile of shell scripts. I need to change "billy" to "bob". But I want to SEE and verify what I am changing.

1. grep -il "billy" > list
2. vi list. Add vi to the front, and "\" to the end of each line.
3. sh list (this edits all the files).
:n to go to the next file
:rew to rewind to the begining of the list
/billy -to search for the from string.

cw bob -to change billy to bob

. to repeat last command (usually the command
that changes "billy" to "bob").

Another useful tip for editing multiple files.
"a23yy Copies 23 lines into a thingy called a.
"ap Pastes the contents of "a" to your current spot in the file.
You can use the rest of the alphabit too.

This allows you to copy the contents of one file, and drop it into files interactively.

Here's an example of how it works.

what to type <-- comments
vi file1 file2 <--edit to big text files.
25G <--go to line 25
"a8yy <-- get 8 lines from file1 at line25
2G <--go to line 2
"b12yy <-- get 12 lines from file1 at line2
:n <--go to file2
40G <-- go to line 40 in file2
"bp <-- paste 12 lines in buffer b to file2
"ap <-- paste 8 lines from buffer a to file2.
3G <-- go to line 3 in file2
"c23yy <-- get 23 lines from file2
:w <-- save the change to file2
:rew <-- go back to file1
100G <-- go to line 100 in file1
"cp <-- paste 23 lines from buffer c to file1
:wq! <-- get out and save the change to file1.

A third tip for people that hate vi.
Use a windows terminal emulator with cut and paste, and windows notepad.

hope this helps.
Bill Hassell
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

The book: "Ultimate Guide To vi" is very useful. One of the more useful tips is that you can use external commands to provide data or formatting.

To get a banner into your file at the current location:

:.!banner MyBanner

To format a paragraph with word wrap, place the cursor somewhere on the first line of the paragraph (the paragraph must end with a blank line), then:

!}exec adjust -m72

(just type it as shown--the cursor will jump to the bottom of the page but when you press Return, the paragraph will be word wrapped up to column 72) As always, type: u to undo the last change.


Bill Hassell, sysadmin
Shahbaz_1
Regular Advisor

Re: Some Tips & Tricks for "VI Editor"

Thanks, every body

Excellent tips.

One Q please

What can be done with $HOME/.exrc file?

Regards
Shah
Let's share the great thing "THE KNOWLEDGE"
steven Burgess_2
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

Hi shah

The $HOME/.exrc file is basically a config file for your own vi sessions

let me expand

Each option can be set in vi with the :set option - where the word option is replaced with some valid option name.

Here a some of these options

:set number - set line number in front of each line

:set all - List all options

:set autoindent - The next line is indented the same number of character as the current line

:set readonly - sets the current file as readonly

:set wrapmargin=n - sets the wrap margin equal to 'n' ie . If you are using 80 cloumn margin and the wrap margin is set to 6, every line will be wrapped to the next line after 74 chras

:set showmode - shows the user which mode they are currently in , ie 'insert' 'replace' etc

So to make these options permanent you simply create the $HOME/.exrc file and add them to that file. You can use shorter versions for each option

se nu (for set number)
se ai (for autoindent)
se sh (for showmode)

Hope this helps

Steve
take your time and think things through
Martin Burnett_2
Trusted Contributor

Re: Some Tips & Tricks for "VI Editor"

Hello,

If you ever need to clear and redraw the screen use:

ctrl+l or ctrl+r

To display a status line on the bottom of the screen:

ctrl+g or :f

Thanks for participating in the forums,

Martin
Chaos reigns within. Reflect, repent, and reboot. Order shall return.