how to copy from other file in vi editor - linux

how to copy from other file with ctrl-c and in vi editor with p yank (paste), i have no idea!!
thinks

You can also do: :r filename
This will pull in the file. It can also be used for output from a command:
:r! grep some_text file
:r! which perl
The other way is to use buffers.
:e other_file.txt
Once you yank, you can :bn to switch to the other buffer and paste
If you are just concerned about pasting, ctrl-v or shift+insert also work to paste the contents of the clipboard.

Once you have yanked (copied) text in vi, you can type <ESC>:e filename to open another file for editing. Your yank buffer will still be the same, letting you paste into the other file. You must be copying text from one file in vi into another file in vi.
If you want to paste text from outside of vi, you need to setup your terminal and vi specially to allow that.

You can't. The yank buffer is private to vim, not shared with the system clipboard.

Related

how to copy something from vim editor to shell command line

I intend to copy something from vim editor to shell command line. I have tried many ways, but I find all of them only work when in the same vim editor. My need is to copy and paste between vim editor and shell command line. That's, vim -> shell
PS: I am using putty.
EDIT:
BTW, what if just copy something from vim editor and paste it to terminal both in a unix/linux box?
ATTENTION:
output of :echo has("X11") in vim is 0, so my system does not support X11!
If your vim version >= 7.3.74, you can set the vim to use the system clipboard by default by adding a line to your .vimrc:
set clipboard=unnamedplus
for the detail, see this link. Then you use the y(ank) command in vim, the content is in the system clipboard.
EDIT:
this solution and the "*y solution require the Vim that has clipboard support. Since you're lack of that, use cat/grep/less/tail... to extract the text you want to copy.
" + y to yank text in vim, then ctrl + shift + v to paste in the terminal.
from the comments, it seems that your mouse middle button works for * buffer, you can select text in vim, then press: 3 keystrokes:
"*y
and switch to other program, terminal, browser or whatever, click mouse middle to get yanked text.
read :h y for details, how to yank text and save in a specific register (in this case, it is register *)
Using pipe
Select line[s] by using V command then execute this selection by command :'<,'>w !sh

How to yank between files? [duplicate]

This question already has answers here:
Copy and paste content from one file to another file in vi
(19 answers)
Closed 9 years ago.
For example, when I yank some text from vim, then :wq, and open a new file. When I try to paste the text I just yanked, it doesn't work. So how to yank text between files?
use "+y to send the yanked text to the clipboard, and "+p to paste it into the new file.
So if I have a file named A that contains abcdef and I (in visual mode) select def and then press "+y while in normal mode. I have just copied def to the system clipboard. Now I can open up a file named B and (while in normal mode) press "+p the text def will be pasted into file B.
Don't quit the editor after writing with :wq
Instead, just write the file with :w and then edit the new file with :e file. Or Edit both at the same time by splitting with :sp file and ^W to switch between the screens.
if you have X11/windows, you could use "+y or "*y to yank. and same register to paste.
don't do :wq and then vim newfile. do :e newfile in same vim. then you could yank and paste between buffers. just press y and p
if you work on tty or via ssh, and don't want to do (2) either, you could save yanked part to a file, in newfile read the file.
I recommend the (2) option.
Vim has the capability of viewing multiple files at once. Use the :sp *file_name* "split" or :vs *file_name* "vertical split" commands to view another file in the same terminal.
Navigation between open file windows is simple: press Control_key + wand then either 'h', 'j', 'k', or 'l' to move the cursor to the file window to either the left, the bottom, the top, or the right of the current window. Simply 'yank' the text that you mean to copy in one terminal window, move to the terminal window containing the file that you mean to copy into as described above, and 'put' the text at the cursor location as usual.
By the way, you may use the :wqa command to close all open terminal windows and write changes made to the open files to the disk.

copy a line/multiple-lines from vi editor to cmd line

Is there a shortcut key to copy a line from vi editor and then paste it into a terminal.
Currently I select the text with mouse and then press crtl+shift+c (I'm using default settings of KDE) and then do ctrl+shift+v on the konsole. The problem with this option is when the line is long enough to wrap. In that case copy-paste inserts extra spaces which I have to fix after pasting on terminal.
This is very annoying specially when the line copied is very long and contains file names (typically commands used to invoke compiler).
You can yank to the X clipboard by putting the contents in the * register.
To do this use "*yy on the line you want to yank. Then outside of vim you should be able to paste it.
If you don't have access to a clipboard via X or screen or tmux, you can write the contents of the buffer to a temp file, then go to the terminal and invoke $(cat temp-file) or open an editor for the command line and read in the file. Whatever shell you are using probably provides a mechanism for opening an editor on the command line. In bash with vi-style readline keybindings you can type v to get a vi session. If you shell does not provide that functionality, try a different shell.

Move section to new file in vim

I have a section in file1 which should be moved to file2. Normally I'd do this by visually-selecting the block, deleting it, :wq from file1, open file2, then paste.
Is there an easy way to move a block of text from file to file2 without closing vim?
Alternatively:
write selection to new file:'<,'>w file2
then reselect and delete with gvd
The first step is covered in vimtutor Lesson 5.3: SELECTING TEXT TO WRITE.
You could open your new file in a split using :sp newfile and then delete the block as normal from your first file. Then, change to the other split with ctrl w and w. Then use p to put the deleted content into the other file.
You could open the file in a new buffer.
just open the file via :e file2 and paste the text. To move quickly between the buffers use either :e # or :b #
see :help buffers for more information
Since no one mentioned that: you can use tabs instead.
Select your block.
Delete it with d
Create a new buffer in new tab with :tabnew newfile.name
Paste it and save it with p and :w
You can go back with gT or close current tab with :q
I personally newer use buffers -- only tabs. Read more about them in :help tabpage

vim copy command to clipboard / buffer

How to copy the ex command to the clipboard or paste it to the buffer?
Using gvim on Windows.
The windows clipboard can be accessed through the buffer +. So pasting your clipboard as an ex-command can be done with <C-R>+. If you want to copy your ex-commands to the clipboard, you need to show the command history (q:) and copy it into the clipboard buffer ("+yy).
Enter command history with (from normal mode)
q:
Then select and copy(yank) commands you need with
"*y
To copy the last executed command to your clipboard:
:let #+=#:
Essentially assign the command (:) register to the system clipboard (+) register.
:call setreg('+', getreg(':'))
not exactly an answer to the question, but on the same lines, the command    ":p    will put/paste the last ex command into the file. it can then be yanked into the clipboard with the command     V"+yy
neither of these will open another window.
they are also useful for when you come up with a really good really long command that you want to save.
If you source mswin.vim in your setup it will map the default Windows copy/paste keys to use the clipboard. If you want to do it yourself please see Soulmerge's answer.

Resources