How to enter in Visual block Mode in Vim by command - vim

My system has Ctrl + V reserved for the use of the clipboard, so I cannot access to Vim's visual mode by using that combination of buttons.
Do you know what is the command such as :VisualBlockModeEnable or something similar to toggle this view?
Also I would like to change the binding in my ~/.vimrc
Thanks

:execute "normal! \<C-v>"
This will enter visual block mode.
To remap it use:
:nnoremap X <c-v>

With the information from #Doktor, I do:
:command! Vb :execute "normal! \<C-v>"
Then I do :Vb if I want to enter it.
(I am using Alacritty to run NuShell, and I have set C-v to paste text so I needed a workaround to enter Visual Block mode while keeping this setting.)

Related

How to copy from Vim to system clipboard using wayland and without compiled vim `+clipboard` feature flag

I cannot copy into the + or * register.
:echo has('clipboard') from within Vim returns 0 meaning I don't have that feature flag, I don't want to recompile.
I'm running wayland so I cannot use X11 based solutions
I had trouble finding resources so here is what ended up working by adding in ~/.vimrc.
nnoremap <C-#> :call system("wl-copy", #")<CR>
wl-copy is a Command-line copy/paste utilities for Wayland and it will copy the piped content you give it to system clipboard.
What mapping above achieves is
Activate the mapping with Ctrl + #. or choose any convenient key combo
nnoremap <C-#>
take the contents of the " register,
denoted by the #" argument
and pipe contents of #" as an argument to the system wl-copy function
shown by :call system("wl-copy", #").
Alternatively
Assuming you only want to copy line sections of the file, do shift+v to go into visual mode and only highlight the lines I want to copy. Then do.
:'<,'>w !wl-copy
where
'<,'> - means you used visual mode to select a range (you don't type this)
w !{cmd} - write the range to the stdin of cmd, see more at :help w_c
You can map that with
xnoremap <silent> <C-#> :w !wl-copy<CR><CR>
xnoremap: mapping will work in visual mode only
<silent>: mapping which will not be echoed on the command line
<C-#>: desired key combination
:w !{cmd}: write the range to the stdin of cmd
<CR><CR>: two enters are needed otherwise command line waits for another command
The feature the author is asking for will be hopefully implemented in vim. See for details: https://github.com/vim/vim/issues/5157
There is also a workaround plugin: https://github.com/jasonccox/vim-wayland-clipboard
As for me, I'm using my own workaround:
" Yank into all these at once:
" vim y/p register
" wayland primary
" wayland clipboard
xnoremap <silent> <leader>y y:call system("wl-copy --trim-newline", #*)<cr>:call system("wl-copy -p --trim-newline", #*)<cr>

Can't configure VsVim to let Visual Studio handle ctrl+c, ctrl+v and ctrl+x

I've been using VsVim for about a year now. I was always able to to copy/paste text using default ctrl+c and ctrl+v shortcuts. It stopped working for me a few days ago. I figured VsVim Keyboard setting must've changed somehow, to handle those keys instead letting Visual Studio do it. Currently pressing Ctrl+v toggles Visual Mode. The problem is there are no ctrl+v or ctrl+c entries in the vsvim settings. Any workarounds for this? I hate having to right-click my mouse to get to the copy/paste menu, and the clipboard buffer is even more of a pain.
The VsVim Wiki talks about this particular problem.
As stated there
If you choose to have VsVim handle Ctrl-X/C/V, you can add the following to the _vsvimrc file in order to arrive at a standard Windows behavior:
This means:
1.) If it doesn't exist, create a file _vsvimrc in your %userprofile% (most likely something like C:\Users\YOURUSERNAME\).
2.) Add the following code to this file:
" CTRL-X and SHIFT-Del are Cut
vnoremap <C-X> "+x
vnoremap <S-Del> "+x
" CTRL-C and CTRL-Insert are Copy
vnoremap <C-C> "+y
vnoremap <C-Insert> "+y
" CTRL-V and SHIFT-Insert are Paste
map <C-V> "+gP
map <S-Insert> "+gP
imap <C-V> <Esc>"+gpa
cmap <C-V> <C-R>+
cmap <S-Insert> <C-R>+
imap <S-Insert> <C-V>
vmap <S-Insert> <C-V>
" Use CTRL-Q to do what CTRL-V used to do
noremap <C-Q> <C-V>
You can adjust these settings to fit your needs.
_vsvimrc will be loaded last and therefore override options that have been set before from what ever configuration.
The above VsVim Wiki link has more useful keybindings to get Windows-like behaviour (e.g. CTRL-Z to undo with noremap <C-Z> u).

Changing VIM Visual Block Shortcut

I want to change the shortcut for visual block from Ctrl-v to something else. The issue is I am using a text expansion program on my base OS that is using the Ctrl-V shortcut for paste into my VNC session. If I change my paste shortcut in my VNC OS then my text expansion doesn't work.
Currently if I press Ctrl-v in vim then it pastes text, and if I press Ctrl-q then nothing happens. What is the easiest way to get my visual block functionality back without losing my text expansion, likely by changing the shortcut for visual block?
You can remap it. I use line-wise visual mode more often than “character-wise,” so I have this in my ~/.vimrc:
nnoremap v V
nnoremap V v
You could do something similar with Ctrl-v:
nnoremap v <c-v> " remap `v` to `Ctrl-v`

vim: Use ctrl-Q for visual block mode in vim-gnome

I use vim on windows and linux. On linux I would like to set ctrl-Q to visual block selection, but still maintain behave mswin which sets ctrl-v to paste.
How can I keep behave mswin and use ctrl-Q for visual block mode?
edit: I though mswin would also map ctrl-Q to visual block mode, but in vim-gnome ctrl-Q does nothing
first of all, I highly recommend you to forget the windows vim shortcuts if you work on a linux box. such as: ctrl-v, ctrl-q, ctrl-c ...
well you must think this isn't the answer to your question. now I post the "answer".
to make ctrl-q work as ctrl-v (block selection) on a linux box, you have to tell, you work with gvim or vim in terminal.
Gvim
If it was gvim, it is easier, just create a mapping, like:
nnoremap <c-q> <c-v>
Terminal Vim
If you want to make <c-q> work in your terminal vim, you need to understand the default <C-q> has special meaning in your terminal settings.
In your terminal, pressing <c-q> will sent stty start signal. This is important when you first stop your terminal output scrolling(by ctrl-s), and then you want to resume. That is, in terminal vim, if you pressed C-q, it will be captured first by terminal. You can of course change that rule, by disable the stty start definition. like:
stty start undef
you could add this to your .bashrc file (I assume you were using bash) if you want to make it as default.
with this line executed, you can create the same mapping nnoremap <c-q> <c-v> in your vim, and pressing <c-q> in normal mode, vim is gonna enter block-wise selection mode.
After all, again, I suggest you forget the windows mapping if you work on linux box.
If you don't want to change your terminal settings (stty start undef to allow using ctrl q as mentioned in Kent's answer) to be able to use ctrl v for paste, you can make ctrl v paste only in visual and insert modes, and make it block select in normal mode:
" Paste from clipboard when in insert mode.
imap <C-V> <ESC>"+gpa
" Paste from clipboard when in visual mode. (Replace whatever is selected in visual mode.)
vmap <C-V> "+gp
You can also copy to the clipboard from visual mode:
" Copy selection to clipboard when in visual mode.
vmap <C-C> "+y

Paste in insert mode?

Is it possible to paste in insert mode in Vim?
While in insert mode hit CTRL-R {register}
Examples:
CTRL-R * will insert in the contents of the clipboard
CTRL-R " (the unnamed register) inserts the last delete or yank.
To find this in vim's help type :h i_ctrl-r
If you don't want Vim to mangle formatting in incoming pasted text, you might also want to consider using: :set paste. This will prevent Vim from re-tabbing your code. When done pasting, :set nopaste will return to the normal behavior.
It's also possible to toggle the mode with a single key, by adding something like set pastetoggle=<F2> to your .vimrc. More details on toggling auto-indent are here.
No not directly. What you can do though is quickly exit insert mode for a single normal mode operation with Ctrl-O and then paste from there which will end by putting you back in insert mode.
Key Combo: Ctrl-O p
EDIT: Interesting. It does appear that there is a way as several other people have listed.
While in insert mode, you can use Ctrl-R {register}, where register can be:
+ for the clipboard,
* for the X clipboard (last selected text in X),
" for the unnamed register (last delete or yank in Vim),
or a number of others (see :h registers).
Ctrl-R {register} inserts the text as if it were typed.
Ctrl-R Ctrl-O {register} inserts the text with the original indentation.
Ctrl-R Ctrl-P {register} inserts the text and auto-indents it.
Ctrl-O can be used to run any normal mode command before returning to insert mode, so Ctrl-O "+p can also be used, for example.
For more information, view the documentation with :h i_ctrl-r
You can use this to paste from clipboard with Ctrlv:
set pastetoggle=<F10>
inoremap <C-v> <F10><C-r>+<F10>
And this for yanking visual selection into clipboard with Ctrlc:
vnoremap <C-c> "+y
If you also want to use clipboard by default for classic vim yanking/pasting (y/p) in normal mode, here is a config option that does it:
set clipboard=unnamedplus
With this configs you can e.g. yank first in normal mode and then paste with Ctrlv in insert mode. Also, you can paste text from different vim instances and different applications.
Another option is:
set clipboard=unnamed
Then you will be able to just select something by mouse dragging in your X environment and paste it into vim afterwards. But (for some reason) you won't be able to yank something (y) in Vim and shiftinsert it somewhere else afterwards, which is probably quite limiting.
Vim docs about this: http://vim.wikia.com/wiki/Accessing_the_system_clipboard
For pasting from custom registers you can follow the other answers :). This answer is mainly about integrating Vim with your system clipboard.
Note that for set clipboard=unnamedplus and set clipboard=unnamed to work, you need to use gvim or vimx (vim-X11): Those are compiled with +xterm_clipboard. You can optionally put this into your .bashrc to alias vim with vimx:
if [ -e /usr/bin/vimx ]; then
alias vim='/usr/bin/vimx'; # vim with +xterm_clipboard
fi
You can find out whether or not your vim has the +xterm_clipboard in the information provided by vim --version.
If you set Vim to use the system clipboard (:set clipboard=unnamed), then any text you copy in Vim can be pasted using Shift + Insert. Shift + Insert is simply an OS-wide paste key-combination (Ctrl + Insert is the corresponding 'copy').
You can also use the mouse middle button to paste in insert mode (Linux only).
You can enter -- INSERT (past) -- mode via:
Keyboard combo: y p
or
:set paste and entering insert mode (:set nopaste to disable)
once in -- INSERT (past) -- mode simply use your systems paste function (e.g. CtrlShiftv on Linux, Cmdv on Mac OS).
This strategy is very usefully when using vim over ssh.
Yes. In Windows Ctrl+V and in Linux pressing both mouse buttons nearly simultaneously.
In Windows I think this line in my _vimrc probably does it:
source $VIMRUNTIME/mswin.vim
In Linux I don't remember how I did it. It looks like I probably deleted some line from the default .vimrc file.
Just add map:
" ~/.vimrc
inoremap <c-p> <c-r>*
restart vim and when press Crtl+p in insert mode,
copied text will be pasted
Paste in Insert Mode
A custom map seems appropriate in this case. This is what I use to paste yanked items in insert mode:
inoremap <Leader>p <ESC>pa
My Leader key here is \; this means hitting \p in insert mode would paste the previously yanked items/lines.
Add this to vimrc or init file:
imap <silent> PP <ESC>pa
..to paste in insert mode with "PP" and stay in insert mode..

Resources