Operating System - HP-UX
1833767 Members
2217 Online
110063 Solutions
New Discussion

Some Tips & Tricks for "VI Editor"

 
SOLVED
Go to solution
Rich Wright
Trusted Contributor

Re: Some Tips & Tricks for "VI Editor"

Delete every line with
:%g//d

Sort the file
:%!sort

Shahbaz_1
Regular Advisor

Re: Some Tips & Tricks for "VI Editor"

Thanks, every body

Absolutely greate inputs.


Regards
Shah
Let's share the great thing "THE KNOWLEDGE"
Rob Saunders
Occasional Advisor

Re: Some Tips & Tricks for "VI Editor"

A couple of additional tips,

1) To write to a file when you don't actually own it:

:w ! su root -c 'cat > %'

(This will prompt for root's password and allow you to overwrite the file)

2) To write to contents of a file to a shell command line (stepping through a large script for example)

:5,50w ! ksh -s

(The above example writes lines 5 through 50 to a korn shell

Additionally, you might be able to assign these to function keys depending on your system...
Shahbaz_1
Regular Advisor

Re: Some Tips & Tricks for "VI Editor"

Hi friends,
Really "vi" is very powerful "editor".

Once again, I thanks, everybody for their great contributions.

Please make this Question "vi - Tips & Tricks" of the forum, a single point of reference for "vi editor".

After the Bob's input, I feel there are so many powerful commands in vi.

So please continue your inputs and unhide the powerful features of "vi editor"


Regards
Shah
Let's share the great thing "THE KNOWLEDGE"
Carsten Krege
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

One of my favourite commands is to delete the "^M" characters at the end of a file. You get them when transferring ASCII files from Unix to a PC using "bin" settings in ftp.

:1,$s/CTRL-V CTRL-M$//g

Carsten
-------------------------------------------------------------------------------------------------
In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. -- HhGttG
Shahbaz_1
Regular Advisor

Re: Some Tips & Tricks for "VI Editor"

Hi,
How to ignore the case, while searching text.
e.g.
when I want to search say word "hp" in a file

/hp, it does not search for HP or Hp/hP.


Thanks & Regards
Shah
Let's share the great thing "THE KNOWLEDGE"
Stefan Schulz
Honored Contributor

Re: Some Tips & Tricks for "VI Editor"

Hi,

to ignore case do:

:set ic

Do your search. If you want to set it back, do:

: setz noic

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Chandrakumar Karunamoor
Occasional Advisor

Re: Some Tips & Tricks for "VI Editor"

Hi Shah,

:s/old/new/|s/abc/xyz/
to give two substitute commands.

:s/$/,/ 14
substiutes the last character with comma for 14 lines. Equivalent to :.,+13s/$/,/

:/kohli/-1 s/^/Father of Indian IT/
searches for Kohli (assuming you have :se ic) forward from you current line and the substitue command is executed on the line before it.

:/^Warning/;/^Warning d
deletes the second occurrence of the line starting with Warning. /^Warning/ ; // d gives the same result. Also
:12;/string/ is a valid address.

:1,$j
for joining all the lines.
:s/|/CTRL+V CTRL+M/g
substiutes the '|' with the newline character.

Hope this helps.
Thanks,
Chandrakumar