Vim mapping to copy all text in visual mode - vim

I would like to map ctrl+a in visual mode to select and copy all text from the current buffer.
The basic idea is to execute: %y* (copy all buffer to clipboard). So, the mapping should be: xmap :%y* (xmap for visual mode only)
However, whenever I run this mapping, this output is shown:
E492: Not an editor command: '<,'>%y*
And, I think Vim is right :-). When in visual mode, pressing the ":", makes the cmd-line show those surprising characters:
":'<,'>"
The only relevant piece of info where I could find this pattern ('<,'>) is that one: http://vimdoc.sourceforge.net/htmldoc/cmdline.html#v_:
(and it didn't help me).
Question: Am I doing something wrong (configuration...) ? Is there another answer to my need (copy all text to clipboard) ?
I am running Vim 7.3 and I only set nocompatible in my vimrc
Thanks for your help,
Tom

Use:
xnoremap <whatever> :<c-u>%y*<return>
The additional ctrl-u erases the command line till the cursor.
From vim reference:
*c_CTRL-U*
CTRL-U Remove all characters between the cursor position and
the beginning of the line. Previous versions of vim
deleted all characters on the line. If that is the
preferred behavior, add the following to your .vimrc: >
:cnoremap <C-U> <C-E><C-U>

Use the following mapping:
nmap <C-A> ggVGy
It yanks all the current file. By the way, I'm not sure it's exactly what you what. I don't understand why you want a visual mapping.

Related

Can't map Home button in vimrc

I want to map the Home button so vim goes to the first non blank character in vim. But mapping the home button doesn't do anything? If I map another key, then it works correctly.
See below my vimrc file:
map <Home> 0w
imap <Home> <ESC>0wi
The above doesn't work. While the following works (Ctrl-F for example)
map <C-f> 0w
imap <C-f> <ESC>0wi
Isn't there a way to map Home key to this? I really need it, because I got used to this when working with Notepad++, Sublime text 2, Visual Studio,...
I also tried the following, with no results. When using another key, it works again...
http://vim.wikia.com/wiki/Smart_home
From Vim FAQ (also available through this nice plugin):
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?
1) First make sure, the key is passed correctly to Vim. To determine if
this is the case, put Vim in Insert mode and then hit Ctrl-V (or
Ctrl-Q if your Ctrl-V is remapped to the paste operation (e.g. on
Windows if you are using the mswin.vim script file) followed by your
key.
If nothing appears in the buffer (and assuming that you have
'showcmd' on, ^V remains displayed near the bottom right of the Vim
screen), then Vim doesn't get your key correctly and there is nothing
to be done, other than selecting a different key for your mapping or
using GVim, which should recognise the key correctly.
This way you can check if the home key you are pressing is the same that Vim understand as <Home>.
Another possibility is that some other mapping is interfering with this one. You could try the following:
noremap <Home> 0w
inoremap <Home> <ESC>0wi
Edit:
It seems the problem is that your terminal is sending a home keycode that Vim isn't recognizing as <Home>.
I believe that the best solution is make Vim recognize that key correctly, so you can move your .vimrc to other terminals/systems without changes.
From :h xterm-end-home-keys:
On some systems (at least on FreeBSD with XFree86 3.1.2) the codes that the
<End> and <Home> keys send contain a <Nul> character. To make these keys send
the proper key code, add these lines to your ~/.Xdefaults file:
*VT100.Translations: #override \n\
<Key>Home: string("0x1b") string("[7~") \n\
<Key>End: string("0x1b") string("[8~")
If that doesn't work, you could try :set t_kh=^V^[[1~. If it work you can enclose it on a check of your terminal type.
Additional information can be found at :h terminal options
Edit 2:
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?
:
:
3) If the key is seen, but not as itself and not as some recognizable
key, then there is probably an error in the terminal library for the
current terminal (termcap or terminfo database). In that case >
:set term?
will tell you which termcap or terminfo Vim is using. You can try to
tell vim, what termcode to use in that terminal, by adding the
following to your vimrc: >
if &term == <termname>
set <C-Right>=<keycode>
endif
where <termname> above should be replaced by the value of 'term'
(with quotes around it) and <keycode> by what you get when hitting
Ctrl-V followed by Ctrl-Right in Insert mode (with nothing around
it). <C-Right> should be left as-is (9 characters). Don't forget that
in a :set command, white space is not allowed between the equal sign
and the value, and any space, double quote, vertical bar or backslash
present as part of the value must be backslash-escaped.
Now you should be able to see the keycode corresponding to the key
and you can create a mapping for the key using the following command: >
:map <C-Right> <your_command_to_be_mapped>
For more information, read
:h map-keys-fails
:h map-special-keys
:h key-codes

Vim Pre-Exit (Esc Key) Command?

Right now in Vim when I go to a new line (or press 'p' or 'o' in normal mode) I get a lovely automatic indent, that also disappears if I exit insert mode without adding anything to it.
Is there a way to bind something to before I exit insert mode, such as inserting a phantom character then removing it?
Argh, I just read about this exact thing like two days ago but I can't remember where.
Anyway, the trick is to input a character right after <CR> and delete it immediately. There are a bunch of ways to do it:
<CR>a<Esc>x
<CR>a<C-w>
<CR>a<BS>
--EDIT--
Vim being Vim there are probably many other ways.
To automate these, you need to add a mapping to your .vimrc:
inoremap <CR> <CR>a<BS> " insert mode mapping for <CR>
nnoremap o oa<BS> " normal mode mapping for o
But I'm not sure you should overwrite defaults like that.
--EDIT--
However, what is annoying with Vim's default behaviour is that you may need to do some <Tab><Tab><Tab><Tab> before actually inputing some text on non-indented line or do == when you are done or rely on the automatic indentation rules for your language at the next <CR>.
All that can be skipped by using <S-S> which puts you in INSERT mode right at the correct indentation level.
Try either cc or S in normal mode to change a line with respect to indention. No need for phantom characters.
:h cc
:h S
A mapping like the following should do the trick:
imap <esc> <esc>:s/\s\+$//<CR>
This one deletes trailing characters when you press esc in insert mode.

Vim Dumb Indenting?

Is there a way to just have Vim copy the indent from the line above, whether it be spaces or tabs, oblivious of the file types?
:set ai
See :help autoindent
I assume you are going to paste something and adjust the indent.
Try ]p
If you are at the beginning of the line and want to copy all the indenting characters above the line that you are currenly on now you can use Ctrl+y. It copies the character from the line above one at a time. Ctrl+e does the same thing but it copies from the line below.
It seems what I've wanted isn't actually possible as Vim automatically removes whitespaces, and uses configuration settings for its indention.
I've avoided this put slapping these in to my vimrc:
:inoremap <CR> x<BS><CR>x<BS>
:inoremap <up> x<BS><up>
:inoremap <down> x<BS><down>
:nnoremap o ox<BS>
:nnoremap O Ox<BS>
It simply puts a character in place and then removes it before I exit the editing mode, so Vim doesn't remove the empty line. If this is the case then it may be simply Vim checking if any editing was done to the line, auto indenting not counted. Maybe someday I'll check out the source and poke around.
I also wanted to use the previous line's indent (so I'd get different indents for different files and not have to tamper with settings each time), but I've managed to compromise and use the lovely Vim plugin.

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

How to paste in a new line with vim?

I often have to paste some stuff on a new line in vim. What I usually do is:
o<Esc>p
Which inserts a new line and puts me in insertion mode, than quits insertion mode, and finally pastes.
Three keystrokes. Not very efficient. Any better ideas?
Shortly after :help p it says:
:[line]pu[t] [x] Put the text [from register x] after [line] (default
current line). This always works |linewise|, thus
this command can be used to put a yanked block as
new lines.
:[line]pu[t]! [x] Put the text [from register x] before [line]
(default current line).
Unfortunately it’s not shorter than your current solution unless you combined it with some keyboard map as suggested in a different answer. For instance, you can map it to any key (even p):
:nmap p :pu<CR>
Options:
1) Use yy to yank the whole line (including the end of line character). p will then paste the line on a new line after the current one and P (Shift-P) will paste above the current line.
2) Make a mapping: then it's only one or two keys:
:nmap ,p o<ESC>p
:nmap <F4> o<ESC>p
3) The function version of the mapping (unnecessary really, but just for completeness):
:nmap <F4> :call append(line('.'), #")<CR>
" This one may be a little better (strip the ending new-line before pasting)
:nmap <F4> :call append(line('.'), substitute(#", '\n$', '', ''))<CR>
:help let-register
:help :call
:help append()
:help line()
:help nmap
You can paste a buffer in insert mode using <C-R> followed by the name of the buffer to paste. The default buffer is ", so you would do
o<C-R>"
I found that I use <C-R>" very often and bound that to <C-F> in my vimrc:
inoremap <C-F> <C-R>"
This still uses three keystrokes, but I find it easier than Esc:
o<Alt-p>
Since you're in insert mode after hitting o, the Alt modifier will allow you to use a command as if you weren't.
Using this plugin: https://github.com/tpope/vim-unimpaired
]p pastes on the line below
[p pastes on the line above
advantages:
works on all yanked text (word, line, character, etc)
indents the pasted text to match the indentation of the text
around it
2 keystrokes instead of 3 and much "easier" strokes
fast
Personally I've nmapped Enter (CR) like this:
nmap <CR> o<Esc>k
...based on this Vim Wikia article.
This way I can make newlines directly from normal mode, and combining this with wanting to paste to a newline below I'd do:
<CR>jp
You could also skip k in the nmap above, depending on what functionality you prefer from Enter, so it would just be <CR>p.
I've also imapped jj to Esc, which would also assist in this case. Esc is way too far away from the home row for how significant it is in vim.
Not shorter than the other solutions, but I do think it feels less clunky than some of them, and it has other uses too.
If you wanted to stay in the insert mode, you can do o ctrl+o p
o – insert mode and go to the new line
ctrl+o – run a single command
like in normal mode
p – paste
It's three keystrokes but you stay in insert mode and also o ctrl+o is quite fast so I personally treat it as 2.5 keystrokes.
If you're copying a whole line then pasting a whole line, use Y to yank the line or lines, including line break, in the first place, and p to paste. You can also use V, which is visual line mode, in contrast with plain v for visual mode.
I have mapping inoremap jj <ESC>. So it is easy to insert new line with ojj and Ojj and then p.
so ojjp paste new a newline. it have one more stroke then o<esc>p but ojjp is easy for me.
I found an elegant solution to this. If you are putting the yank register in your OS's clipboard (which is great anyway), with
set clipboard+=unnamed
than you can do o<Ctl-v>.
Besides being fewer strokes, this improves on both o<Esc>p and :pu because it preserves indenting: both of the other options start you at character zero on the new line.
Caveat is that this may or may not be OS dependent. All I know is that it works on recent version of OS X, but clipboard is just one of many ways to get yank in the OS clipboard.
If you want to paste in a new line and still keep indentation, create this mapping:
nnoremap <leader>p oq<BS><Esc>p
Prerequisite: you have leader mapped and you have set autoindent in your .vimrc.
Explanation: a new line is created with 'o', 'q' is typed and then back-spaced on (to keep indentation), and 'esc' brings you back to normal mode where you finally paste.
If you also want to end in insert mode, it is possible to paste while in insert mode using CTRL-R ". https://stackoverflow.com/a/2861909/461834
Still three keystrokes, but no escape, and you save a keystroke if you want to end in insert anyway.
I use the following mapping in my Neovim config:
nnoremap <leader>p m`o<ESC>p``
nnoremap <leader>P m`O<ESC>p``
A little explanation:
m`: set a mark in the current cursor position.
o<Esc>p: create a new line below and paste the text in this line
O<Esc>P: create a new line above and paste the text in this line
``: put the cursor in the original position
See :h mark for more information about marks in Vim.
This solution only seems to apply when the block of copied text starts on a new line (as opposed to grabbing a snippet of text somewhere within a line), but you can always start your copy on the last character you want to grab, then navigate to the last character at the end of line prior to the start of your desired copy block. Then when you want to paste it, place the cursor at the end of the line under which you want your text to be pasted and hit p. If I haven't screwed up the explanation, this should provide the effect you're looking for.

Resources