vim copy/yank without leaving visual mode - vim

In normal text edit after I select and copy, the selection stays still, so I can copy again quickly. But in vim when I press y in visual mode, I enter normal mode, so I need to select again. I want a quicker way.
Reasons:
When testing a console app, I copy test data from text editor and paste into console. Then I find bugs and edit code, the clipboard may change and I need to copy and paste test data to test again

You can reselect last visual region typing gv
*gv* *v_gv* *reselect-Visual*
gv Start Visual mode with the same area as the previous
area and the same mode.
In Visual mode the current and the previous Visual
area are exchanged.
After using "p" or "P" in Visual mode the text that
was put will be selected.

You do not need to be in visual mode in order to yank text. While in normal mode, the yank command can be combined with a motion: y{motion}. You can read more about that here: http://vim.wikia.com/wiki/VimTip312#Copying_and_cutting_in_normal_mode
It also sounds like you would like to yank text into the system clipboard. On most Linux distributions, you can do this by yanking text into the + register. Depending on the distro, you may need to install gvim.

Related

How do I keep text selected when changing from Visual to Insert mode?

I am running Vim extension in VS Code and want to be able to select text using Visual mode and keep that text selected when I'm in Insert mode. Currently I am able to select text in Visual mode but the selection becomes unselected when I change to Insert mode. Is this possible?
In Vim, you can only be in one mode; so it's either insert mode or visual mode. That said, Vim remembers the start, end, and type of the previous selection, and it can be easily reselected via gv (cp. :help reselect-Visual).
As Vim has special atoms to specify mark positions in a regular expression (:help /\%'m), and the '< and '> marks correspond to the current selection, we can use :match to make the last (characterwise) selection permanently visible:
:match Visual /\%'<\_.*\%'>\_./

What's a common use case of "selection mode" in vim?

It's the first time i noticed about the selection mode in vim when i accidentally triggered it from visual-line mode by <c-g>.
Vim already has visual mode for text selection what's the use case of selection mode, can anyone give me a hint on this?
(note: i've checked the manual page which describes it as
a resemblance of MS Windows text selection
but i still cannot quite understand why do we need any mouse actions in vim)
Select-mode is commonly used in snippet plugins such as vim-snipmate. Mappings can be created that are unique to select-mode so as not to interfere with regular visual mode behaviour.
Here is an excerpt from Practical Vim by Drew Neil:
If you are happy to embrace the modal nature of Vim, then you should
find little use for Select mode, which holds the hand of users who
want to make Vim behave more like other text editors. I can think of
only one place where I consistently use Select mode: when using a
plugin that emulates TextMate's snippet functionality, Select mode
highlights the active placeholder.
As the documentation suggests, select mode is a bit different from visual mode. Here's the commands you can do in it:
Commands in Select mode:
- Printable characters, <NL> and <CR> cause the selection to be deleted, and
Vim enters Insert mode. The typed character is inserted.
- Non-printable movement commands, with the Shift key pressed, extend the
selection. 'keymodel' must include "startsel".
- Non-printable movement commands, with the Shift key NOT pressed, stop Select
mode. 'keymodel' must include "stopsel".
- ESC stops Select mode.
- CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O*
- CTRL-G switches to Visual mode.
Otherwise, typed characters are handled as in Visual mode.
When using an operator in Select mode, and the selection is linewise, the
selected lines are operated upon, but like in characterwise selection. For
example, when a whole line is deleted, it can later be pasted halfway a line.
You can see the docs here.
What they mean by "resembles MS Windows selection" is probably that you can extend the selection with Shift+Arrows, and also that any printable characters entered will substitute the selected text and enter insert mode.
Also, see this question/answer at the vi.SE.

Pasting from clipboard in vim

I am trying to paste a string I copied from a webpage in vim. I know that the string is copied in the system clipboard. My vim returns 1 when I run the :echo has('clipboard') command, so I typed :set paste, then positioned the cursor to the desired spot and hit "*p, but this doesn't seem to paste my whole selection. I copied to the clipboard Vader(father figure) but it only pastes Vader. Or is it pasting this because the word behind the cursor is Vader? And finally, what is the easiest way to paste something in vim from the system's clipboard?
I mention that I am using Ubuntu 14.04 (if this has any relevance).
I always use Ctrl+Shift+v in Insert mode, when pasting from system clipboard after doing set paste. Never had any problems doing so.
Otherwise you can use the vim-unimpaired plugin by tim pope and use the yo key mapping set by the plugin to paste. Using yo automatically sets the paste option and after you paste and leave Insert mode, it automatically toggles the set paste option.
Have this line in your .vimrc
set pastetoggle=<F2>
So whenever you press F2 in insert mode you can paste normally with no problems.
For more information refer here.
You only need to set paste if you intend to paste while in insert mode using the middle mouse button or any shortcut your terminal provides.
Now, it is possible that the register you're looking for is quote+. To confirm that just run :registers in order to view the contents of all registers.
I would also advise you to read the following help section :help x11-selection.
I'm currently using the system clipboard in vim as default register in combination with a clipboard manager like Diodon or Klipper (for KDE).
Add this to your vim configuration ~/.vimrc
" Use the system clipboard for yank / delete / paste operations
if has('unnamedplus')
set clipboard=unnamed,unnamedplus
endif
With this configuration vim copy/paste operations (in both directions) behave just like in any other graphical editor (the if has ('unnamedplus') assure that this behaviour is enabled only if vim is compiled with the +clipboard option otherwise behaves as default).
Note: on Windows and MacOS using unnamed or unnamedplus is equivalent.
On Linux are two different entities:
unnamed register is the selection buffer (try to select something with the mouse and then center click or click both sx and dx mouse buttons in another place).
unnamedplus register is actually the system clipboard

How to paste text into Vim Visual Mode?

i am working in vim and I would like to paste yanked text into Visual Mode. Is it possible?
The p command works in visual mode, too (cp. :help v_p). However, the default register contents will be overwritten. (My ReplaceWithRegister plugin has an alternative command that keeps it.)
Alternatively, you can s or c to replace the visual selection, and then use one of the insert mode commands like <C-R>{register} to insert the register contents. For pasting the default register, you need to use "_s to avoid overwriting its contents.

selecting in visual mode to paste outside vim window

I need to paste some selected block in visual mode to outside of vim. Currently I need to select this block manually from mouse to paste outside of vim.
As selecting texts in visual mode is easier ,it would be efficient to select some text for pasting outside of vim.
You could yank the text into the + (plus) register, that is mapped to the system clipboard. Just select the text in the mode you like and then type "+y.
Disclaimer: Linux
So what I have noticed is you need clipboard support compiled in to your vim. I ended up compiling my own vim, which had the clipboard support. To check run vim --version and look for a +clipboard or a -clipboard, if it's + then yay you have it, if it's - then you need to compile vim yourself or download a version with clipboard support compiled in. Then the answers people have said seem to work. For me "*y copies into the buffer that is pasted by pressing the middle button, and "+y copies into the buffer which is the normal control + c or on the terminal control + shift + c so what I put into my vimrc was
map <C-c> "+y
that way doing control + c me paste it somewhere else by pressing the exact same command
:wq
If you are using GUI-based gvim, simply yank your text into the "clipboard register" by prefixing your yanking command with "+. That is, when you have finished selecting your text in visual mode, press "+y to yank your text then it will be in your system clipboard.
If you are using text-based vim and your vim has clipboard access to your current system, it's just the same as gvim. If your vim has no clipboard access, try to establish the clipboard connection as described in this page:
http://www.quora.com/How-can-you-copy-all-contents-of-a-text-file-opened-in-vim-through-Putty-on-a-Windows-desktop-to-Windows-clipboard
You can bind contents of the visual selection to system primary buffer (* register in vim, usually referred as «mouse» buffer) by using
set clipboard^=autoselect
You have to identify the register that vim is using to get the outside clipboard.
First copy any text outside vim and then inside vim do the command :registers and look for the text you copied, once you have identified the register simply use it every time you need to copy and paste from the outside:
for example:
Im using gvim in Windows7 and the register used by vim to get the external clipboard is
*"
then in vim select the text and do
*"y to copy (yank) and paste outside as usual
and to paste inside vim from outside do *"p
you can also do a map to the register to easy copy/paste

Resources