Mapping select-all, copy, paste in vim - vim

I have the following map in my vimrc:
nnoremap <C-a> ggVG
nnoremap <C-c> "*yy (might be because I'm in visual mode here?)
nnoremap <C-v> "*p
The select-all (ctrl-a) and the paste (ctrl-p) works, but the (ctrl-c) does not work with that shortcut, though it works if I manually type in the command after doing ctrl-c.
What needs to be fixed here?

The first issue I would like to address is that your mapping for copying the text, nnoremap <C-c> "*yy, will only work in normal mode. When you select text in Vim, you enter visual mode, and the first n of nnoremap makes the mapping work in normal mode only.
You can make your mapping work by using noremap (all modes), vnoremap (visual and select mode), or xnoremap (visual mode only), like this:
vnoremap <C-c> "*y
You can find more information about mappings in the documentation.
Another thing to note is that the default function of Ctrl-c is to cancel/interrupt the current command. For example, if you enter insert mode and press Ctrl-c, you will exit insert mode and go back to normal mode. With your original mappings, it will cancel the selection (exits visual mode) without copying anything.

This works for me in Neovim but I believe it should also work in Vim as well. To yank all the content I have the following mapping in my configuration:
nnoremap <leader>ya ggVGy<C-O>
Details:
gg: go to the first line
V: select the first line
G: go to the last line
y: yank selection
<C-O>: go to the previous cursor position

Related

why is `ctrl -h` not work in insert mode?(version gvim 8.1.1)

In "Practical Vim" (second edition), the writer says <c-h> can delete back a character. I've tried in git bash, and it worked. However, it doesn't work in gvim in win10 as expected.
When I enter something in insert mode, I can use <c-h>, <c-w> and <c-u> before I leave insert mode. When I switch to insert mode without entering anything, <c-h>, <c-w> and <c-u> don't work.
Note that <c-h> is not mapped (:map <c-h> prints No mapping found).
Vim, by default, does only allow to delete characters that you typed since you entered insert mode. It does not allow to delete chars before the point where you started inserting. This behavior is inherited from the original Vi.
To change that, Vim has the option 'backspace'. It configures how <Backspace>, <Del>, <c-w> and <c-u> work. Add the following to your _vimrc and Vim will behave as you expect:
set backspace=indent,eol,start
Then you can backspace over autoindent, start of insert and end-of-lines.
See :help 'backspace'.

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).

Assigning keys to function in insert mode also in vim

I am trying to bind control-F to find dialog box in vim. Following code in .vimrc works:
:map <C-F> :promptfind<CR>
However, it works only in the command mode. How can I set it up so that it also works in insert mode?
In insertmode, your binding will just insert :promptfind and then move to the new line. Use <C-O> to execute a single command-mode operation before going back to insert mode (:help i_ctrl-o), or <Esc> in its place to go to command mode and stay there afterwards.
:nnoremap <C-F> :promptfind<CR>
:inoremap <C-F> <C-O>:promptfind<CR>

vim: copy into system clipboard not working

In my .vimrc I have those two lines for copy and paste into the system clipboard:
nnoremap <Leader>p "+p
nnoremap <Leader>y "+y
Weirdly the remapping of pasting works fine, but I can't copy any lines. If I press the buttons "+y it works and the highlighted text is in my system clipboard, but if I use < Leader> y it is only copied into a register inside vim, but not into the system clipboard.
Some help would be great!
If you use your mapping in Visual mode, you have to use vnoremap or noremap, instead of nnoremap (which is for Normal mode only).
See :h map-overview for details.
Your yanking mapping is incorrect. "+y lacks a motion. If you want to yank the whole line use upper Y ("+Y) or or yy ("+yy).
:help y
*y* *yank*
["x]y{motion} Yank {motion} text [into register x]. When no
characters are to be yanked (e.g., "y0" in column 1),
this is an error when 'cpoptions' includes the 'E'
flag.
Note that, if you type "+y, vim hangs in "operation-penging mode" (:help Operator-pending-mode).

vim: how to select pasted block

Is there a vim command to directly select a block of text which has just been pasted?
ps. I know about gv to reselect a block after exiting visual mode. It doesn't apply to this case.
If you want to select it just after paste (before you change anything else), use
nnoremap <expr> gV "`[".getregtype(v:register)[0]."`]"
. [ and ] marks point to start and end of the last change, v:register is set to the last register used (which is register used for the paste command unless you, for example, yank something), [0] selects only first byte of register type (it is required because for blockwise register it returns <C-v>{width}) and register type is one byte which is just the same as the keystroke you should use in normal mode to invoke visual mode.
I saw this solution somewhere on SO, you may want to search for it in order to get some alternatives.
In my case I have this map:
:nnoremap gp `[v`]
After more research I think the better solution is:
" https://vim.fandom.com/wiki/Selecting_your_pasted_text
nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
I have had the following maps in my vimrc forever:
nnoremap <leader>p `[V`]
nnoremap <leader>[ `[V`]<
nnoremap <leader>] `[V`]>
They do the following:
visually select the recently pasted block
de-indent the recently pasted block
indent the recently pasted block
I probably use the indent ones even more than the selection one.

Resources