Expand the % register - vim

In insert or command mode typing CTRL+r % insert the name of the current file.
But is it possible to expand the % register so that it also contains the full path to the file?

CTRL-R allows =expression "register". To insert full path use expand('%:p'):
CTRL-R =expand('%:p')
Insert mode mapping:
:imap <C-R>p <C-R>=expand('%:p')<CR>
Press <C-R>p in Insert mode.

Related

VIM : word completion in command-line mode?

When replacing a word with another in a buffer in command-line mode, :%s/verylongword/newword/g, is there a way to word-complete 'verylongword' somewhow from the contents of the buffer ?
Failing that, is there a way of copy-pasting 'verylongword' from the buffer when typing :%s/verylongword/newword/g ?
Command-line window
While typing the command press CtrlF to open the command-line window, see :h c_CTRL-f.
In the command-line window you can use all the usual vim commands to edit your command line.
Paste from register
To paste from a buffer in command-line mode press CtrlR followed by the register name.
For example press CtrlR0 to paste from register 0 to paste the last yanked text.
See :h c_CTRL-R for more info.
Object under cursor
CtrlR also allows the following completions (quoted from vim help):
CTRL-R CTRL-F c_CTRL-R_CTRL-F c_<C-R>_<C-F>
CTRL-R CTRL-P c_CTRL-R_CTRL-P c_<C-R>_<C-P>
CTRL-R CTRL-W c_CTRL-R_CTRL-W c_<C-R>_<C-W>
CTRL-R CTRL-A c_CTRL-R_CTRL-A c_<C-R>_<C-A>
Insert the object under the cursor:
CTRL-F the Filename under the cursor
CTRL-P the Filename under the cursor, expanded with
'path' as in gf
CTRL-W the Word under the cursor
CTRL-A the WORD under the cursor; see WORD

Weird symbols being inserted when I press Ctrl + V?

gVim 8.0
When I press Ctrl+V in insert mode (or in Ctrl+: mode) I got this v-like symbols inserted instead of my current clipboard text.
Why does it happen? How do I get it back to normal?
Thank you.
CTRL-V means "Insert next non-digit literally." I.e. if you press Ctrl-V twice vim inserts "CTRL-V" into the text and displays it as "^V".
To paste a buffer switch to normal mode and use p or P. You can temporary switch to normal mode with Ctrl-O; e.g. Ctrl-Op.
The problem was caused by the omission of this line: source $VIMRUNTIME/mswin.vim from _vimrc file.
If you are on insert mode use Ctrl-r + .
Looking at :h registers you will see that + means clipboard register and Ctrl-r helps us to insert any register on insert mode.
In normal mode you can just type: "+p
As being said Ctrl-v on insert mode is used to insert special chars. To learn more :h i_Ctrl-v
*i_CTRL-V*
CTRL-V Insert next non-digit literally. For special keys, the
terminal code is inserted. It's also possible to enter the
decimal, octal or hexadecimal value of a character
|i_CTRL-V_digit|.
The characters typed right after CTRL-V are not considered for
mapping.
Note: When CTRL-V is mapped (e.g., to paste text) you can
often use CTRL-Q instead |i_CTRL-Q|.

nmap to simulate shift-insert in insert mode

In vim I would like to create a key map for gp in normal mode so that it switches to insert mode and simulate the shift+insert key press and goes back to normal mode.
here is what I tried:
nmap gp i<S-Insert><esc>
All it does is insert the text <S-Insert> instead of pressing executing shift+insert.
I've looked at Paste in insert mode? but the I can't get the contents of what I'm pasting from a buffer.
You have to enter the control characters directly. You do this by pressing <C-V><Wanted Character> in insert mode.
For more on this see i_CTRL-V.

How to use command + V to enter insert mode and copy in Vim?

Is there a method in OS X such that Vim will enter insert mode automatically and copy text, instead of having to enter insert mode first and press command + V?
Or more generically, how to map command keys in Vim in OS X? I use command line Vim, not MacVim.
Add this to your vimrc:
nnoremap <c-v> p:startinsert!<cr>
This will insert your clipboard content and place the cursor after the inserted text and change to insert mode.
But why you want insert mode altogether? Just press p in normal mode!

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