Select ALL in VI Editor

Hi all,

How can i select all and copy text in VI Editor.

Rakesh Gupta

The only thought I have is to go to the top line of your document and do "yyX", where X is the number of lines in your doc. That will yank all the lines into the buffer. Then put your cursor where you want the copy to go and do a "p"

Is there no other way as with yyX i would have to know the number of lines in advance (though its not a problem) but still if there is shorter way please point it out.

Thanks for the reply,

Rakesh

You can do it this way also, which doesn't require you to know the number of lines:

:%Y a

which yanks all lines into buffer a.

Then, when you want to paste, position your cursor where you want it and type this in (without being in command mode):

Paste below the cursor position:

"ap

or

Paste above the cursor position:

"aP

I tried

in my VI editor for GNOME Terminal 2.6.0

but

works fine.

I think we sould type :

thanks for the reply

My bad, it was a typo.

Hi,
Can you elaborate pls. Does the command allow you to paste it another file?

I tried but it would not work. Any clue?
Thanks

No, only to where the cursor is as I indicated in my earlier post. If you want the selection to be written to another file then you need to do something like this:

:%w newfile.txt

This will overwrite the file named 'newfile.txt', if it exists. If you just wanted to append to the file, you would do a %y as stated above to yank (copy) the text you want, and then do a ':e newfile.txt', which would open the file in the same vi session. Then just type 'p' to paste in the text after your cursor.

You could also open a file using vi and import the entire contents of another file using ':r file.txt'

or

:%w >> newfile.txt

to append

Thanks guys, your precious collaboration enables so many of us to learn so many things daily.

This is indeed a great forum ...

If i type

and if i open a new fine and do:

(not in command mode)

what i get is only 50 lines

and unix says

do u know whats going on here??

Thankyou.

Why are you doing

:ap

( I'm assuming you meant colon instead of double quote ) ?

I don't think the 'a' is necessary. I don't even use it in the yank.

%y

is all that I do.

Just type 'p' in command mode to paste in everything that is in the 'yank' buffer.

To paste buffer a you must use "ap while not in command mode. The :ap doesn't equate to anything in vi.

Sorry, I can't help you here. I just ran tests yanking 100000 and 200000 lines into a buffer and I can switch files and paste every line. Perhaps your tmp space isn't adequate?

I will want to add few more questions in the ongoing vi discussion, I have following quiries:

1) What about if I'm in the middle of a file and I want to yank from there to the end of file without knowing the number of lines, is it possible? suppose from line 12 to eof?

2) What is the command in vi to goto a specific line number, suppose I want to move cursor to the 1st line, 8th line or last line without knowing the total number of lines.

3) If there is any command in vi to count to number of lines?

4) How can I run shell commands without leaving the vi session.

5) How can I use vi non-interactively, can any one give a simple example here.

I personally believe that there should be a specific forum for UNIX editors, at least for vi, vim, & emacs. There are tons of tips and tricks related to these editors, which come only with practice and no one can find those in any tutorial. So it would be really great if our senior members share their experiences with novices like me. Lets hope Administrators of these forums think about that.

Cheers,
Patras

Let's take them one by one.

  1. You can do <n>,$y in the command mode where n is the number of line and $ represents end of file. e.g. :15,$y

  2. you can just enter the line number in command mode. e.g. :20 with place the cursor at line 20.

  3. You can always do a set nu in command mode and check it.

  4. Do a !<command name> in escape mode.

  5. Can you eloborate more on what you want.

I am also a novice ....but i think this might be helpful :slight_smile:
regards
Apoorva Kumar

Thanks for your reply.

What about if I don't know the line number? Is there a way in vi to show the line number with each line? And what would be the command to copy lines from any line to begining of file ie 1st line?

That works fine.

Can you pls give me example of this?

Yeah, that worked for me

Suppose I want add a line "Test Line" after line number 11, without opening vi, is it possible?

Thanks in advance for any reply.

Cheers,
Patras

Dear Patras,

Do a escape the print ": set nu".
I hope this will answer your 1 and 3 question. As far as your 5th point is concern you need to use sed with appened option, that needs some advance knowledge of sed.

regards
Apoorva Kumar

Thanks a lot Mr. Kumar for your reply. set nu command is wonderful. Can you pls tell me if there is any way that when I login, my login files run automatically this command for vi? So I'll not have to worry about running this command every time I launch vi? You know any thing about that?
For point 5, it means non-interactive editing is not possible at all with vi?

Cheers,
Patras