1828580 Members
2236 Online
109982 Solutions
New Discussion

vi command ?

 
SOLVED
Go to solution
Jerry_109
Super Advisor

vi command ?

Hello All,

Just wanted to ask (2) questions about using vi :

1) how do you delete text from current line to end of file ?

2) from command line, how do you change/substitute a string throughout the entire line :
ex:
command # 1
# mkdir foo1 foo2 foo3 foo4

I want pull this same command from history
( esc k ) and substitute "foo" with "toe"
so it executes :

# mkdir toe1 toe2 toe3 toe4

please advise, Thanks
12 REPLIES 12
Rick Garland
Honored Contributor
Solution

Re: vi command ?

The answer for (1) is 'dG'

taken from the 'dd' to delete a line and the 'G' to go to end of file.

For (2) I would use sed.
Attached file gives lots of 1 liners for sed. This is from an old post which came from an old site.
Jerry_109
Super Advisor

Re: vi command ?

First command works fine, thanks, but the second does not seem clear? I'm I using "sed"
from the command line? Is there an example to use after the "esc k" ?
Jerry_109
Super Advisor

Re: vi command ?

for example in "vi" I use :
1,$s/foo/toe/g

How do I use this from the command line ?
Rick Garland
Honored Contributor

Re: vi command ?

cat $file | sed s/foo/toe/g

Run this command and you will see output on screen. Can redirect this output to a file

cat $file | sed s/foo/toe/g > $file.2

Jerry_109
Super Advisor

Re: vi command ?

Sorry I was not clear on what I was trying to do. I was trying to change a particular string from the command line to execute using
the history to pull up previous cmd, and substitute to "string1" w/ "string2" on the command line. I still might not be clear, but as an example :

command1
# mkdir foo1 foo2 foo3 foo3
# "esc k" returns
# mkdir foo1 foo2 foo3 foo3

When the history returns the previous command
above, that's where I want to substitute
"foo" w/ "toe" ? I'm trying to substitute directly from cmd line, and not output to a file.
Rick Garland
Honored Contributor

Re: vi command ?

Putting it that way, I'm not sure this can be done.

If you have the 'esc-k' functionality, the editing functionality is probably with vi (this is a guess). So bring the history, mkdir toe1 toe2 toe3 toe4, the cursor will be on the 'm' of the mkdir. Use the 'w' key to advance 1 word, 'cw' to change word from foo to toe, hit the esc key to go back into command mode, 'w' to advance to next word, 'cw' to change word from foo1 to toe1, etc...

Rick Garland
Honored Contributor

Re: vi command ?

Here is a vi cheat sheet I have lying around.

LAST LINE MODE


/PATTERN search forward
?PATTERN search backwards
/ or ? or n repeat last search
N reverse last search
:w write buffer to disk
:w NEWFILE write buffer to NEWFILE
:w >> FILE append buffer to FILE
:w! FILE force a write to FILE
:wq write then quit
:q quit
:q! quit without writing
:f show file name and line #
:cd DIRECTORY change DIRECTORY
:r read file into buffer
:r FILE read named FILE
:e edit
:e! discard buffer and edit
:e FILE edit a FILE
:e # pop between files
:s/OLD/NEW/ current line sub. 1st
:s/OLD/NEW/g current line sub. all
:1,7s/OLD/NEW lines 1-7 sub. all
:%s/OLD/NEW all lines sub. 1st
:%s/OLD/NEW/g all lines sub. all
:%s/^/PATTERN /g insert PATTERN at beginning of line all lines
:%s/$/ PATTERN/g insert PATTERN at end of all lines
:set nu show line numbers
:set nonu hide line numbers
:set sm enable matching ()s
:set nosm disable matching ()s

$ end of line
^ beginning of line
/ search
:%s/.$// remove last character on every line

INSERT MODE


a append after cursor
A append at end of line
i insert before cursor
I insert at start of line
o open a line below cursor
O open a line above cursor
cw change word at cursor
c#w change # of words
C change text to end of line
r replace a character
R replace text unti
#a add # copies of text
#a add # copies of text at line end
#i insert # copies of text
#I insert # copies of text at line start
^w erase last word
go to command mode

COMMAND MODE


h move cursor left
l move cursor right
j move cursor down one line
k move cursor up one line
G go to end of file
#G go to line #
H go to top of screen
M go to middle of screen
L go to bottom of screen
w move cursor forward one word
b move cursor back one word
#w move cursor forward # words
#b move cursor back # words
F scroll forward one page
B scroll back one page
x delete character
dd delete line
#dd delete # lines
dw delete word
#dw delete # words
d$ delete to end of line
P paste before cursor
p paste after cursor
"vp paste buffer v after cursor
xp transpose two characters
Y yank a line
#Y yank # lines
"v#Y yank # lines to buffer v
. repeat last command
G show which line on
u undo

CHANGE A VALUE THROUGHT A FILE
:g/$oldpattern/s//$newpattern/g

this was for changing DNS values in the db.* files

:g/SOA/s/$oldpattern/$newpattern/g

The 'g' at the beginning and end of the command indicate that the changes are to occur horizontally and vertically
Jerry_109
Super Advisor

Re: vi command ?

Thanks for all the support. I saw another admin do this some time ago, and let him get away without getting the command. I'll keep
trying.
Jared Middleton
Frequent Advisor

Re: vi command ?

Does the Esc-k imply you are using ksh?

Under bash, the following command will do the substitution you seek:
$(echo !mkdir | sed -e "s/foo/toe/g")

For example:
# mkdir foo1 foo2 foo3
# ls
foo1 foo2 foo3
# $(echo !mkdir | sed -e "s/foo/toe/g")
$(echo mkdir foo1 foo2 foo3 | sed -e "s/foo/toe/g")
# ls
foo1 foo2 foo3 toe1 toe2 toe3

I'm not sure how to suppress the echo of that command line to the screen though (if it even matters) redirecting to >/dev/null didn't seem to work.

-Jared
Jerry_109
Super Advisor

Re: vi command ?

Yes, ksh
Vincent Fleming
Honored Contributor

Re: vi command ?

When you do the k and pull up a previous command, type a "v", and it will load that command into vi proper.

(This assumes you have "set -o vi" set, which it sound like you do.)

You can then use your typical editing commands to modify the shell command or script - this is VERY handy for developing small shell scripts, by the way.

so:

# mkdir foo1 foo2 foo3 foo4
( esc k )
v
:s/foo/toe/g
ZZ --- to write, quit, execute command


Cheers,
Vince
No matter where you go, there you are.
Jerry_109
Super Advisor

Re: vi command ?

You are the man, it works great.

Thanks.......... :>) !