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.
Related
For example, I open a file which includes
FATAL ERROR: Simulation failed. Please read /path/to/the/error/file.error
What I want to do is copy the path of the error file, which I've already done that by "v" into the visual mode and "y" to copy it, and open that file by splitting the window by ":sp /path/to/the/error/file.error".
My problem is: how to paste the copied path in command mode?
Vim also has a feature to open the filename under the cursor directly.
If you want to open it in a new split, then you can use Ctrl+W, f (or the equivalent
Ctrl+W, Ctrl+F).
See :help CTRL-W_f for more details.
The mapping to open the file under the cursor in the current window (not in a new split) is gf.
In command mode, use Ctrl-R, which is paste register, then " to paste the last yanked thing from the unnamed register.
to see your current registers, execute the following command :reg
please could someone give me a list of some keyboard shortcut editor vim in particular to save and close?
I try ctrl + s orders or ctrl + q or ctrl + x and then ctrl + q but no success until a present
ZZ does save and close
ZQ does close without save
both work in normal mode.
For quiting vim shell,
hit ESC and
then :q for quit without any changes.
Enter
Some commands are here:
:q[uit] Quit Vim. This fails when changes have been made.
:q[uit]! Quit without writing.
:cq[uit] Quit always, without writing.
:wq Write the current file and exit.
:wq! Write the current file and exit always.
:wq {file} Write to {file}. Exit if not editing the last
:wq! {file} Write to {file} and exit always.
:[range]wq[!] [file] Same as above, but only write the lines in [range].
ZZ Write current file, if modified, and exit.
ZQ Quit current file and exit (same as ":q!").
For quick overview : Vim commands
The quick answer is:
Hit ESC
type :wq [name of the file, if it's a new file]
type Enter
The long answer, well, it's really long...
Some commands in this guide start with a colon: pressing it will display the command prompt where the subsequent command is written.
Commands without a colon are more like hotkeys - they can be used in the Vim default mode (which is the mode Vim starts in).
Commands written in CAPITAL LETTERS are specific keys: for example, ESC means the escape key on your keyboard.
All commands in Vim are case-sensitive.
EXITING VIM
To quit, discarding any changes you might have made:
:q!
Memorize: quit dammit!
To quit, saving any changes you've made:
:wq
Memorize: write to disk and quit
NAVIGATING THE EDITOR
To move around the currently open file, use your arrow keys.
To move to line 285:
:285
To search for the word import:
/import
EDITING TEXT
To start inserting text on the current cursor location:
i
Memorize: insert
To start inserting at the end of the current line:
A
Memorize: Append
To exit insert mode, and return to the default mode:
ESC
SELECTING TEXT
To start selecting, enter the visual mode:
v
Memorize: visual
Select text by moving with your arrow keys.
To exit visual mode:
ESC
COPY, CUT, PASTE
To copy the current selection into the buffer (think of it as a clipboard):
y
Memorize: yank
To cut the current selection:
d
Memorize: delete
To copy the current line into the buffer:
yy
Memorize: yank yank
To copy 3 lines including the current line into the buffer:
3yy
To cut the current line and place it into the buffer:
dd
Memorize: delete delete
To cut 5 lines including the current line:
5dd
To paste the buffer before the current line:
P
Note: Uppercase P
To paste the buffer after the current line:
p
UNDO AND REDO
To undo the last change:
u
Memorize: uh-oh :)
To redo the last change you just undid:
CTRL + R
To see the number of changes:
:undolist
To undo the last two changes:
2u
The Vim multi-level undo tree is very powerful. Read more about it here.
OPENING FILES
To open the file index.html instead of the current one:
:edit index.html
SAVING FILES
To save the file you're currently editing:
:w
Memorize: write to disk
To save the file with a different name, here changes.txt (ie. Save As):
:w changes.txt
Searching and Replacing
To search and replace all occurences of a string in the file:
:%s/typo/corrected/g
To search and replace, but prompt before replacing:
:%s/typo/corrected/gc
Memorize: confirm
Syntax highlighting and Indentation
Turn on syntax highlighting:
:syntax on
Enable automatic indentation:
:set autoindent
Increase indentation on multiple lines by selecting them in visual mode, and pressing:
>
Working with multiple files
TABS
To open server.py in a new tab:
:tabe server.py
Memorize: tab edit
To move to the next tab on the right:
:tabn
Memorize: tab next
To move to the previous tab on the left:
:tabp
Memorize: tab previous
To close a tab, move to it and use :q or :wq as you would normally.
SPLIT VIEW
To open templates/base.html in a vertical split screen:
:vs templates/base.html
Memorize: vertical split
To open shared.js in a horizontal split screen:
:sp shared.js
Memorize: the 'default' horizontal split
To move between split screens:
CTRL + W + ARROW KEYS
To close a split screen, move to it and use :q or :wq as you would normally.
More information at here.
Those are very basic questions. It is better for you to start vim in tutorial mode, like this:
$ vimtutor
I have a split window open in GVIM.
On the bottom window I have a text file open.
This text file contains a list of files (with full path).
What I would like is a command so that according to my cursor position on a file name in the bottom window, that file will open on the top window. At the moment, when I press g-f it opens it in the bottom window instead of the file list.
Any ideas?
EDIT: I managed to solve it with a macro (copy line, ctrl-w, up, :e and paste). Any better suggestions are welcome!
You could use a mapping like this one:
nnoremap <F7> :let cf = expand("<cfile>") <bar> wincmd k <bar> execute "e " . cf<CR>
save the filename under the cursor in a variable,
move the cursor to the window above,
edit the file.
But what problem are you trying to solve?
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.
I want to search for $maximumTotalAllowedAfterFinish and replace it with $minimumTotalAllowedAfterFinish. Instead of typing the long text:
:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g
Is there a way to COPY these long variable names down into the search line, since, on the command line I can't type "p" to paste?
You can insert the contents of a numbered or named register by typing CTRLR {0-9a-z"%#:-=.}. By typing CTRL-R CTRL-W you can paste the current word under the cursor. See:
:he cmdline-editing
for more information.
Copy it as normal, then do CtrlR" to paste. There are lots of other CtrlR shortcuts (e.g, a calculator, current filename, clipboard contents). Type :help c_<C-R> to see the full list.
Copy:
1) v (or highlight with mouse, in visual mode)
2) y (yank)
Paste:
1) / (search mode)
2) Ctrl + R + 0 (paste from yanked register)
Type q: to get into history editing mode in a new buffer. Then edit the last line of the buffer and press Enter to execute it.
Or create the command in a vim buffer , e.g. type it in the buffer:
s/foo/bar/gci
And copy it to a named register, with "ayy (if the cursor is on that line!).
Now you can execute the contents of the "a" register from Vim's Ex command line with:
:[OPTIONAL_RANGE]#a
I use it all the time.
You can place the cursor on the word that you want to add to your pattern and then press / or : to enter either the search or the command mode, and then press CtrlRCtrlW to copy the word. Source
Typically, you would do that with mouse selecting (perhaps CtrlIns or CtrlC after selecting) and then, when in the command/search line, middle-clicking (or ShiftIns or CtrlV).
Another way, is to write your command/search line in the text buffer with all the editing available in text buffers, starting with : and all, then, on the line, do:
"add#a
which will store the whole command line in buffer a, and then execute it. It won't be stored in the command history, though.
Try creating the following line in the text buffer as an example for the key presses above:
:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g
Finally, you can enter q: to enter history editing in a text buffer.
add a line: cnoremap <c-v> <c-r>+ in your vimrc, then you can use ctrl-v to paste.