I recently started use :imap jj and am trying to unlearn Ctrl+[.
However, imap doesn't work when paste mode is on. How do I make it work in paste mode?
You simply can not have mappings work when 'paste' is enabled, since that is the whole point of Vim's paste mode.
Vim's paste mode is meant to allow you to paste stuff even in an instance of console Vim in a terminal—where Vim may not be aware you're using the mouse to paste—and you want to insert literal text form your paste buffer without triggering mappings, or auto/smart/expression indenting, etc.
I suggest you take a look at:
:help 'paste'
:help 'pastetoggle'
Add following snippet to your .vimrc to trigger paste mode automatically when pasting via the terminal:
let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
function! XTermPasteBegin()
set pastetoggle=<Esc>[201~
set paste
return ""
endfunction
From:
https://coderwall.com/p/if9mda
Related
I want to use <S-Insert> to paste the content of clipboard in vim irrespective me being in normal or insert mode
I am using [Fedora 23, Gnome terminal 3.18.3, vim 7.4 +xterm_clipboard]
The usual problem I have is:
I go on the web and Ctrl-C on multiple lines
Back to vim in Insert or Normal mode I want to paste WITHOUT indentation modification
And I get a paste with a messed up indentation but only when the file being copied to as a filetype like r/python (I guess because an indent is defined)
What I have found
I found this post (amongst others) but that's not working.
How I can make it work
When I use the paste mode it works
For example if I do
I go on the web and Ctrl-C on multiple lines
In vim :set paste
I do <S-Insert>
Then it works irrespective of mode OR filetype and I can :set nopaste
Another way is to paste from clipboard "+p which works (without messing with paste mode)
Leads ??
Thing is I am not even sure this is vim related, I think I cannot map <S-Insert> as if I do
nnoremap <S-Insert> <NOP>
inoremap <S-Insert> <NOP>
vnoremap <S-Insert> <NOP>
And start new terminal then vim and try <S-Insert> it still copies
I get a paste with a messed up indentation
This is because vim assumes the copied text as a stream of characters typed in to it.
If you can use system clipboard in vim, use these mappings
:map <silent> <S-Insert> "+p
:imap <silent> <S-Insert> <Esc>"+pa
You can find if vim has clipboard support using
vim --version
If you see "+xterm_clipboard", you are good to go. Use it with
set clipboard=unnamed " or
set clipboard=unnamedplus
If using system keyboard is out of the picture, my suggestion would be to write a function that takes you to paste mode with set paste, pastes the copied text, takes you back to normal mode with set nopaste
I want to automatically set the paste mode on when I open a new file (an empty file that doesn't exist).
autocmd BufNewFile * :set paste
in the vimrc does the trick with vim newfile or :e newfile, but not with :enew.
How can I run an autocommand on :enew?
set paste will have many side effects, e.g. disable indention. I would imagine the requested behavior would become annoying quickly.
Alternatives:
Use 'pastetoggle' setting to setup a key to toggle 'paste',
unimpaired.vim's yo mappings which put Vim in insert mode with 'paste' set and disables 'paste' upon leaving insert mode
vim-bracketed-paste enables transparent pasting into Vim for certain terminals.
For more help see:
:h 'paste'
:h 'pastetoggle'
To answer the question, the following will run an autocommand on :enew:
autocmd BufCreate * if '' == expand("<afile>") | set paste | endif
You can replace BufCreate with BufAdd or BufNew, it will work all the same. The important part is the if '' == expand("<afile>") that will only execute the command if it the new buffer has no name.
NB:
For the specific use-case of pasting text in new buffer, auto-switching to paste mode is not a great solution though, I wouldn't recommend using this.
As the paste is maintained even when you switch buffer. You'd need additional autocommands to unset the paste mode when you switch buffer/window (autocmd WinEnter * set nopaste will work for windows, but for some reason autocmd BufEnter * set nopaste doesn't help when switching buffer).
To ease pasting stuff in new buffers (or otherwise), I'd recommend either:
typing "*p or "+p in normal mode, as to use vim's native mechanism to paste from the clipboard registers, as suggested by #dartNNN (assuming you're on your local machine)
using one of the plugins recommended in #peter-rincker's answer
I used classic vim, but recently i moved to gtk-vim on ubuntu, but emmet plugin don't working, it is because when I press <C-Y> it copies above char to cursor position, i failed to disable it. When i set unmap or iunmap <C-Y> it writes that there is no maping, also this shortcut is not noted in imap.
EDIT:
I find out that i'm unable to imap, when i press any shortcut it just print it out, for instance
map <F2> :echo 'Current time is ' . strftime('%c')<CR>
is working well but
imap <F2> :echo 'Current time is ' . strftime('%c')<CR>
is printing <F2> to text
It looks like you've :set paste, which disables insert mode mappings and abbreviations. This option is meant for pasting text from the terminal (e.g. via middle mouse button); it should only be set during the paste itself, not permanently (and you can easily toggle this via the 'pastetoggle' option).
So, check via :verbose set paste?, then find the place where this is set, and disable it.
PS: Your demo imap needs <C-o> before it; else (if imaps are working, that is), you'd just insert the :echo ... into the buffer.
imap <F2> <C-o>:echo 'Current time is ' . strftime('%c')<CR>
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.
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..