How to paste into vims command line was asked here here.
But how can I copy from the vim command line?
For example:
:python import sys; print(sys.executable)
Now I want to copy that line to the clipboard, for pasting it into an other editor.
Yes, it is possible.
If you executed a command :foo, the command text line will be stored in register :, you can see it by :echo #:.
To have those value in clipboard, you just let + register have the same value.
So :let #+=#: or call setreg('+',#:) should help you.
You can assign to a register the content of another register:
:let #+ = #:
Will assign in the system clipboard (#+) the last executed : command (#:).
However, this will become your last executed command. If you want to keep your python command as the last one, you can do the following:
First open the command history:
q:
Go on the line of the command you want to copy, then yank it in the system register (visually selects the whole line and yanks it):
"+yy
This will copy the whole line with the new line character at the end, if you just want the command without the new line, you can do:
v$h"+y
Finally, close the command history:
:q
q: open command-line window, it's filled with command-line history, you can copy via "+yy.
Related
I have installed tmux.
tmux -V
tmux 2.3
Set my configure file.
cat ~/.tmux.conf
set -g mouse on
Enter tmux and open a two vertical windows in it,open python3 console in the left,open vim in the right.
Now move my cursor at the beginning of the first line in the right with mouse.
Enter into normal mode and input 2yy+, to copy two lines in my + register.
Move cursor at the left python3 console window ,how can i paste content in + register into the python console?
#Kent,do as you say:
1.Move cursor at the beginning of first line,and type "+2Y
2.Move cursor to the left window,and middle-click mouse,nothing happen.
3.press ctrl+b then press ] key.
first your vim should be compiled with +clipboard see vim --version | grep 'clipboard'
To copy ( or delete ) in any vim register you can use the following syntex
"<register name><oprator><motion> (see :h registers )e.g.
"ayy(copy current line in register a) or
"bdd(delete current line in register b) or
"*ce(delete to the end of the current work and place content in register * using c will also put you in insert mode
to copy whole line you can use yy
and system clipboard is mapped to either + or * ( depending on the os )
so to copy the whole line into system clipboard you can use
"*yy or "+yy (depending on the os)
or to copy 2 lines
"*2yy or "+2yy ( to copy current and the line after current line )
once the content is copied in the system clipboard you can paste in tmux using ( command + v or ctrl + shift + v )
or to map system clipboard with tmux paste buffer see https://unix.stackexchange.com/questions/67673/copy-paste-text-selections-between-tmux-and-the-clipboard#72340
2yy+ does NOT copy two lines into + reg, instead, it yanks two lines to " reg, then moves the cursor to first non-blank char in next line
You can on the vim side do: "*2Y then do a mouse middle-click on the python console.
or simply select the lines you want to copy in vim by mouse, then middle click in python console
I didn't quite like with the accepted solution that it depends on a graphical environment for the clipboard since this does not work when vim can not access the clipboard, which is almost always the case for ssh connections.
So I cam up with another solution:
Instead of using the external clipboard vim can pass the text directly into tmux' paste buffer by piping it into
tmux load-buffer -
After that you can paste the content of the buffer with prefix + ] into the active tmux pane.
There are various ways to pass the text from vim to tmux:
# to write the current line into the tmux buffer:
:.w !tmux load-buffer -
# to write all *lines* within the visual selection into the tmux buffer:
:'<,'>w !tmux load-buffer -
# to pipe the content of a register (e.g. from a previous selection) into the buffer:
# #" being the unnamed register, #0 - #9 the numbered registers, and so on
:call system('tmux load-buffer -', #")
Using tmux paste-buffer you can even trigger the pasting into the correct pane at the same time:
# assuming the python pane is at :0.0
:call system('tmux load-buffer -; tmux paste-buffer -t :0.0', #")
You can now also easily map the last line to a key to send the visually selected text to the python pane.
I wanted to paste the yanked line in the vim command prompt after typing certain command.
I saw a solution where they asked to enter <Ctrl-R><Shift-"> to paste the yanked lines in the vim command prompt, however I am having the following problems:
When I try like, :tabnew and then type <Ctrl-R><Shift-">, whatever yanked line gets pasted after :tabnew line.
Eg: :tabnew /disk/bin/hello.log
The above solution doesn't work if I map the same above command in the vimrc. I tried adding the following map in my .vimrc:
:map <S-P> :<C-R><S-">
When I try :tabnew and type <S-P>, it is not pasting the yanked line, i.e. the mapped command is not working.
Can anyone help me on the above scenario?
FOLLOW-UP QUERY:
Is it possible to mix normal mode and command line mode operations?
For Eg:
a. I have a line in text file which is a directory path and wanted to open that directory in vim.
b. Instead of doing Yanking [S-Y] the line and then doing mapped command [map <C-T><C-O> :tabnew <C-R><S-"><bs><CR>] to open the directory for vim, is it possible to do something as given below ?
nnoremap <F7> <S-Y>cnoremap:tabnew <C-R><S-"><bs><CR>
Please drop you comments/suggestions?
The : command line prompt is "Command-line-mode" (see :h Command-line-mode, :h cmdline, or :h : [all show the same help]). You can map keys in that mode using :cnoremap. So you seem to be looking for this:
:cnoremap <s-p> <c-r>"<bs>
The backspace at the end removes the trailing end-of-line character that is (probably) at the end of the buffer.
I very strongly suggest you use a different mapping than <s-p>, because that will be triggered every time you try to type a capital "P".
How can I paste the contents of the system clipboard to all files in the argument list in Vim?
You can do the following:
:argdo execute 'normal! "+p' | w
Explanation:
:argdo Run the command that follows on each file in the argument list. Alternatively, you can use :windo to run a command on each window, or :bufdo to run a command on each buffer.
execute "normal! ..." Run the sequence of commands after normal! as if they were entered in normal mode. Ignore all mappings and replace string escape sequences like \<esc>.
"+p Paste the register for the system clipboard. Note that Vim has to be compiled with the +clipboard feature enabled for this to work.
| w Write every file, whether it was updated or not. Alternatively, use | update to only write files that were changed.
For more details, see:
Learn Vimscript the Hard Way
Vim 101: Search and Replace on Multiple Files
Run a command in multiple buffers
Originally answered by Roberto Balejík
I am aware I can select an entire line in Vim in visual mode by pressing Shift+V. Now, can I achieve the same (i. e. placing the cursor to the specific line within a file and selecting this very line) from the shell, using only Vim command line options?
Currently, the best I have is
vim +[line] -c 'set cursorline' [file]
but this is not exactly what I want.
Use the normal command normal to run commands in normal mode.
vim +[line] -c 'normal! V' [file]
Take a look at :h normal
is there a way to use vim/vi in the vim command line? Sometimes I write a long
command in vim such as:
:!./script /home/user/pet --flag=1
and I want to change for instance "user" by "other". What I usually do is to
navigate the command line with right arrow which is time consuming and even more
when I want to go to the beginning of the line. I would like to have something
like "0" to go there or w/b to move by words. Or use j/k to go to the next/previous
command.
Thanks.
:h cedit
in command line, type ctrl-F(default) to enter command window.
or in normal mode type q:
(for search, type q/)
Vim has a feature called the "commandline window". You can enter it with Control-F by default when you're already on the commandline, or q: from normal mode, edit the commandline using vim commands, and press enter to execute. It also contains your command history so that you can yank previous commands if you like. See :help cmdline-window for more information.
I'm not aware how you can use Vim commands to edit a command directly on the command line, but if you enter the command window q: you get can use regular Vim editing to edit commands.
From there you can execute commands by hitting <CR> or use Ctrl-C to copy the command to the regular command line.
If you run set -o vi you will have vim capabilities in your command line. Just put 'set -o vi' in your .bashrc file or equivalent to have it by default.