Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

What is the command to copy all the contents to the system clipboard from a file open in an instance of the vi editor on a remote server, accessed via SSH. Also, what is the shortcut for pasting from system clipboard to vi.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

UPDATE: it became clear that vipin8169 runs vim on a remote server -- naturally, the vim over there does not have access to the clipboard of the local workstation, so the answer below is not applicable.

:set clipboard=unnamed

Everything you yank from now on will be copied to the clipboard, so to copy the whole file, do 1GvGGy in the normal mode. After that, you can use Ctrl-V or middle button click in any other application to paste the contents of the file.

Explanation: 1G means go to the beginning of the file (2G would be "go to the beginning of the second line"). v is to start marking a block. GG goes to the end of the file, y yanks it into the unnamed register (which is now also clipboard thanks to :set clipboard=unnamed).

Also, whenever you copy something from now on, you will be able to paste it with the regular p keypress.

share|improve this answer
    
to do 1GvGGy i need to press colon again or what ?? –  vipin8169 Sep 13 '12 at 12:52
1  
Nope, you need to be in the default mode. 1G means go to the beginning of the file (2G would be "go to the beginning of the second line"). v is to start marking a block. GG goes to the end of the file, y yanks it into the unnamed register (which is now also clipboard thanks to :set clipboard=unnamed). –  January Sep 13 '12 at 12:59
1  
Argh, that means you are accessing the server over SSH? This could not work, because the vim instance running on the server does not have access to your local clipboard! Just copy the file to your local workstation with scp, then: scp server:/path/to/file . and open it with gedit, you will be able to copy / paste at will in the usual simple manner (ctrl-a, ctrl-c). –  January Sep 13 '12 at 13:11
1  
Depends. If gedit is installed on the remote server, you can use X forwarding to start X programs on the remote server, but showing them on your local display. For that, start ssh with the option -X. Another solution: mount the remote directory using sshfs, you will be able to edit it as if it was a mounted file share. And what is the problem with scp? –  January Sep 13 '12 at 13:19
1  
Come on, this is simpler than ssh. Works just like cp, only it can access remote servers. Also, this is one of the most important tools. Say your server is called "heaven", your file is on "heaven" at /home/barzalius/howtokilldragons.txt. Copy it with scp heaven:/home/barzalius/howtokilldragons.txt .. However, if you have a different username on the remote server, you need to copy it like that: scp remoteusername@heaven:/home/barzalius/howtokilldragons.txt .. –  January Sep 13 '12 at 13:28

This question cannot be answered in a simple way, but here you will find a very good tutorial.

share|improve this answer
    
I'd rather backup the file using cp filepath ~ than doing that stuff :-P –  vipin8169 Sep 13 '12 at 12:59
1  
The question can be perfectly answered in a simple way. The tutorial you provide is only for very special cases, when the easy, regular way does not work. I've been using this method for ages. –  January Sep 13 '12 at 13:04
    
it describes the solution for vim, but i have never used vim. I'm using vi on ubuntu 12.04 –  vipin8169 Sep 13 '12 at 13:09
1  
nope, it describes a very obscure and for most of the purposes useless solution for vim, which is at odds with the widely used and documented solution in vim (set clipboard=unnamed). Also, your vi is an alias to vim.tiny, so yes, you are using vim. –  January Sep 13 '12 at 13:15
    
ok. i never knew. Thanks for the precious info :D –  vipin8169 Sep 13 '12 at 13:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.