vim: alias yank and copy to xclip - vim

I've got a cntrl c and cntrl v mapped to xclip, however its a hassle to have to remember to use instead of regular y and p. Is there a way to alias the two or send contents of y and p to xclip, so I can just use y and p for all copy and pasting?
vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
nmap <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p")")")"))

Are you trying to use the X clipboard for all copy and pastes? If so, a good alternative to xclip is to make sure you're using a vim with X support (it's really easy to compile Vim if your version doesn't have it) and then add the following to your vimrc:
set clipboard=unnamed
All yanks and deletes will then automatically go to the * register (which is the X selection register).
Instead of setting clipboard=unnamed, you can also use the X selection register for a single operation by using (e.g.)
"*yw
"*yy
"*ya(
or whatever.
Obviously, this doesn't answer your question as to how to use xclip, but hopefully it offers an alternative approach.

I don't know if this is an ideal solution, but it works.
set clipboard=unnamedplus
function! ClipboardYank()
call system('xclip -i -selection clipboard', ##)
endfunction
let vlcb = 0
let vlce = 0
function! ClipboardPaste(mode)
if (a:mode == "v")
call cursor(g:vlcb[0], g:vlcb[1]) | execute "normal! v" | call cursor(g:vlce[0], g:vlce[1])
endif
let ## = system('xclip -o -selection clipboard')
endfunction
" replace currently selected text with default register without yanking it
vnoremap <silent>p "_dP
vnoremap <silent>y y:call ClipboardYank()<CR>
vnoremap <silent>d d:call ClipboardYank()<CR>
nnoremap <silent>dd dd:call ClipboardYank()<CR>
nnoremap <silent>p :call ClipboardPaste("n")<CR>p
vnoremap p :<C-U>let vlcb = getpos("'<")[1:2] \| let vlce = getpos("'>")[1:2] \| call ClipboardPaste("v")<CR>p

You can also set clipboard=unnamedplus to use the “+” register by default. Using it, I did yy in my .vimrc and then pasted that line here by ctrl-V ;)

Related

Go to next character even if on a later row? [duplicate]

Anyone knows how to quickly find the next occurrence of a character (like the f command) but multi-line? I.e. to quickly jump to the next occurrence of some character in a file?
Isn't that what "/" does?
If you're looking for the next "x" then do /x while in command mode.
Then you can hit "n" to advance to the next x, and then the next x, etc.
There are lots of vim cheat sheets out there with all kinds of tips.
There's an example of redefining f to ignore case in the 'eval.txt' help file.
:h eval.txt
(search for "ignore case" a couple times)
We can modify that to do what you want. Add the following function to your ~/.vimrc file or better yet, create a plugin file: ~/.vim/plugin/find-char.vim (create the directories if you do not already have them).
function FindChar()
let c = nr2char( getchar() )
let match = search('\V' . c)
endfunction
Then, in your ~/.vimrc add the following line:
nmap f :call FindChar()<CR>
Now, f should work like you want it to.
BTW, this was tested with Vim 7.2 on Ubuntu 10.04.
Christian Brabandt's ft_improved plugin extends the built-in f / t commands to search in following lines, too.
Having wanted to know exactly the same thing I looked through the answers here. None of are exactly what I wanted so I cobbled together a few of them.
q335's answer was the closest because it handles omaps properly which is necessary to do something like dt} (delete everything up to, but not including the next curly brace) but Curt's answer handles special character searching and uses a single function which to me is much more preferable so I'm not adding too much to my .vimrc
Here is my result:
"Makes f and t work across multiple lines
nmap <silent> f :call FindChar(0, 0, 0)<cr>
omap <silent> f :call FindChar(0, 1, 0)<cr>
nmap <silent> F :call FindChar(1, 0, 0)<cr>
omap <silent> F :call FindChar(1, 1, 0)<cr>
nmap <silent> t :call FindChar(0, 0, 1)<cr>
omap <silent> t :call FindChar(0, 0, 0)<cr>
nmap <silent> T :call FindChar(1, 0, 1)<cr>
omap <silent> T :call FindChar(1, 0, 0)<cr>
"Functions
fun! FindChar(back, inclusive, exclusive)
let flag = 'W'
if a:back
let flag = 'Wb'
endif
if search('\V' . nr2char(getchar()), flag)
if a:inclusive
norm! l
endif
if a:exclusive
norm! h
endif
endif
endfun
Aaaa, vimscript. It takes about 2 years to write 20 lines well :D. Here is the latest, sleekest version: works for ALL modes: visual, operator pending, normal.
let prvft='f'
let prvftc=32
fun! MLvF(c,...)
let [g:prvftc,g:prvft]=[a:c,a:0? 'f':'F']
let pos=searchpos('\C\V'.nr2char(g:prvftc),'bW')
call setpos("'x", pos==[0,0]? [0,line('.'),col('.'),0] : [0,pos[0],pos[1],0])
return "`x"
endfun
fun! MLvf(c,...)
let [g:prvftc,g:prvft]=[a:c,a:0? 'F':'f']
let pos=searchpos('\C\V'.nr2char(g:prvftc).(mode(1)=='no'? '\zs' : ''),'W')
call setpos("'x", pos==[0,0]? [0,line('.'),col('.'),0] : [0,pos[0],pos[1],0])
return "`x"
endfun
fun! MLvT(c,...)
let [g:prvftc,g:prvft]=[a:c,a:0? 't':'T']
let pos=searchpos('\C\V'.nr2char(g:prvftc).'\zs','bW')
call setpos("'x", pos==[0,0]? [0,line('.'),col('.'),0] : [0,pos[0],pos[1],0])
return "`x"
endfun
fun! MLvt(c,...)
let [g:prvftc,g:prvft]=[a:c,a:0? 'T':'t']
let pos=searchpos('\C\V\_.'.(mode(1)=='no'? '\zs' : '').nr2char(g:prvftc),'W')
call setpos("'x", pos==[0,0]? [0,line('.'),col('.'),0] : [0,pos[0],pos[1],0])
return "`x"
endfun
no <expr> F MLvF(getchar())
no <expr> f MLvf(getchar())
no <expr> T MLvT(getchar())
no <expr> t MLvt(getchar())
no <expr> ; MLv{prvft}(prvftc)
no <expr> , MLv{prvft<#'Z'? tolower(prvft) : toupper(prvft)}(prvftc,1)
Or the super garbled:
let [pvft,pvftc]=[1,32]
fun! Multift(x,c,i)
let [g:pvftc,g:pvft]=[a:c,a:i]
let pos=searchpos((a:x==2? mode(1)=='no'? '\C\V\_.\zs' : '\C\V\_.' : '\C\V').(a:x==1 && mode(1)=='no' || a:x==-2? nr2char(g:pvftc).'\zs' : nr2char(g:pvftc)),a:x<0? 'bW':'W')
call setpos("'x", pos[0]? [0,pos[0],pos[1],0] : [0,line('.'),col('.'),0])
return "`x"
endfun
no <expr> F Multift(-1,getchar(),-1)
no <expr> f Multift(1,getchar(),1)
no <expr> T Multift(-2,getchar(),-2)
no <expr> t Multift(2,getchar(),2)
no <expr> ; Multift(pvft,pvftc,pvft)
no <expr> , Multift(-pvft,pvftc,pvft)
One approach to this problem is to use the easymotion plugin.
This lets you use motions such as f across the entire visible window of text. You trigger the plugin, then enter f and the character you are looking for. It highlights each position that the character appears on screen in a highlight color (e.g. red), and shows the position using a letter (a, b, c, d, ...). You simply press the letter corresponding to the position you wish to jump to.
The plugin's README on Github includes an animation that visually demonstrates how it works.
You could make a mapping to go to the next character under the cursor:
:map f yl/\V<c-r>"<c-m>
the \V will make sure symbols are matched literally.
Also, you may want to look at the * and # commands, which are already defined for you, although they might not be what you want.
The most reasonable way of doing this at the moment is hidden in this comment.
You just have to install two plugins: vim-repeat and vim-fanfingtastic.
Personally, I use Vundle to manage my vim plugins.
So this is the way you can make f, F, etc. work as desired:
Install Vundle.
Add these lines to your .vimrc:
Plugin 'tpope/vim-repeat'
Plugin 'dahu/vim-fanfingtastic'
Run $ vim +PluginInstall +qall in your shell.
Voila!

Vim: how to make a macro/command that will wait for a key and use it?

For example, I want to temporarily map to fxsj. That is, when I press q, VIM will perform fxsqj. When I press k, VIM will perform fxskj. And so on.
You can use getchar(), for example:
nnoremap <F2> :call Fun()<CR>
function! Fun()
let c = nr2char(getchar())
if c=='q' || c=='k'
exec 'normal fxs'.c.'j'
endif
endfunction

How to use vim variables in an external filter command in visual mode?

I'm trying to make a code pretty printer filter (e.g. perltidy) accept arbitrary options depending on vim variables. My goal is to pass project specific options to an external command used as a filter (:!) in visual mode.
The following expresses my intention (the last line is problematic):
" set b:perltidy_options based on dirname of the currently edited file
function! SetProjectVars()
if match(expand("%:p:h"), "/project-foo/") >= 0
let b:perltidy_options = "--profile=$HOME/.perltidyrc-foo --quiet"
elseif match(expand("%:p:h"), "/project-bar/") >= 0
let b:perltidy_options = "--profile=$HOME/.perltidyrc-bar --quiet"
else
let b:perltidy_options = "--quiet"
endif
endfunction
" first set the project specific stuff
autocmd BufRead,BufNewFile * call SetProjectVars()
" then use it
vnoremap ,t :execute "!perltidy " . b:perltidy_options<Enter>
However, the last line (vnoremap) is an error in vim, because it expands to:
:'<,'>execute "!perltidy " . b:perltidy_options
and the execute command cannot accept a range.
But I'd like to have this:
:execute "'<,'>!perltidy " . b:perltidy_options
How can I do this?
p.s. My perltidy is configured to act like a unix filter and I use vim 7.3.
You can use <C-\>e and getcmdline() to preserve command-line contents:
vnoremap ,t :<C-\>e'execute '.string(getcmdline()).'."!perltidy " . b:perltidy_options'<CR><CR>
, but in this case I would suggest simpler <C-r>= which purges out the need for :execute:
vnoremap ,t :!perltidy <C-r>=b:perltidy_options<CR><CR>
If you ever want to get rid of a range in command (ex) mode,
CRL-u will do just that.
vnoremap ,t :execute "!perltidy " . b:perltidy_options<Enter>
becomes
vnoremap ,t :<C-u>execute "!perltidy " . b:perltidy_options<CR>
:h c_CTRL-u
Happy vimming,
-Luke

How to fold to a specific level in Vim across an entire file at once?

I want to push one button in Vim and fold all the code so only code up to a specific (and variable) indent level is showing. Very useful when I want to only see method names for example and not their indented routines.
The “Vim: Fold top level folds only” question has a solution to an indent level, but it requires an environment set each time you change levels.
When my cursor is at an indent level (say level 2), I want the entire file to fold to that indent level across all methods.
Is this built into Vim somewhere? Does anyone know of a good plugin that does this?
Configure folding to be defined by indentation:
:setl foldmethod=indent
and try the following command:
:let &l:foldlevel = indent('.') / &shiftwidth
To quickly access this command, create a mapping for it as follows:
:nnoremap <silent> <leader>z :let&l:fdl=indent('.')/&sw<cr>
Because foldnestmax doesn't apply when foldmethod is expr, I looked for something else when I came across your question. Here is what I came up with, which doubtless can be improved:
function! <sid>CloseFoldOpens(opens_level)
let lineno = 2
let last = line("$")
while lineno < last
if foldclosed(lineno) != -1
let lineno = foldclosedend(lineno) + 1
elseif foldlevel(lineno) > foldlevel(lineno - 1)
\ && foldlevel(lineno) == a:opens_level
execute lineno."foldclose"
let lineno = foldclosedend(lineno) + 1
else
let lineno = lineno + 1
end
endwhile
endfunction
nnoremap <silent> z1 :%foldclose<cr>
nnoremap <silent> z2 :call <sid>CloseFoldOpens(2)<cr>
nnoremap <silent> z3 :call <sid>CloseFoldOpens(3)<cr>
nnoremap <silent> z4 :call <sid>CloseFoldOpens(4)<cr>
nnoremap <silent> z5 :call <sid>CloseFoldOpens(5)<cr>
I prefer the numbered maps, but for yours based on indentation of the current line, something along these lines:
nnoremap <silent> z. :call <sid>CloseFoldOpens(foldlevel('.'))<cr>zv
No need of a plugin, it is builtin in Vim.
'foldlevel' (or shorter 'fdl') and 'foldnestmax' ('fdn') seems to be what we were looking for. You only have to set the 'foldmethod' (or shorter 'fdm') and a 'foldnestmax' (or 'fdn') in you .vimrc file:
set foldmethod=indent foldlevelstart=2 foldnestmax=2
OR the shorter version:
set fdm=indent fdls=2 fdn=2
Then you can change the fold level with direct commands: zm or zr.

How to paste over without overwriting register

Does anyone know of a way to paste over a visually selected area without having the selection placed in the default register?
I know I can solve the problem by always pasting from an explicit register. But it's a pain in the neck to type "xp instead of just p
Use the following:
xnoremap p pgvy
this will reselect and re-yank any text that is pasted in visual mode.
Edit: in order this to work with "xp you can do:
xnoremap p pgv"#=v:register.'y'<cr>
v:register expands to the last register name used in a normal mode command.
I don't like the default vim behavior of copying all text deleted with d, D, c, or C into the default register.
I've gotten around it by mapping d to "_d, c to "_c, and so on.
From my .vimrc:
"These are to cancel the default behavior of d, D, c, C
" to put the text they delete in the default register.
" Note that this means e.g. "ad won't copy the text into
" register a anymore. You have to explicitly yank it.
nnoremap d "_d
vnoremap d "_d
nnoremap D "_D
vnoremap D "_D
nnoremap c "_c
vnoremap c "_c
nnoremap C "_C
vnoremap C "_C
"{register}p won't work as you describe. It will replace the selection with the content of the register. You will have instead to do something like:
" I haven't found how to hide this function (yet)
function! RestoreRegister()
let #" = s:restore_reg
return ''
endfunction
function! s:Repl()
let s:restore_reg = #"
return "p#=RestoreRegister()\<cr>"
endfunction
" NB: this supports "rp that replaces the selection by the contents of #r
vnoremap <silent> <expr> p <sid>Repl()
Which should be fine as long as you don't use a plugin that has a non-nore vmap to p, and that expects a register to be overwritten.
This code is available as a script there. Ingo Karkat also defined a plugin solving the same issue.
In your .vimrc
xnoremap p "_dP
I found this from a response on a similar thread, but the original source was http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text. It mentions some drawbacks, however it works fine for me.
Luc Hermitte's solution works like a charm. I was using it for about a week or so. Then I discovered a solution from Steve Losh's .vimrc that works nicely if YankRing is part of your plugin/bundle lineup:
function! YRRunAfterMaps()
" From Steve Losh, Preserve the yank post selection/put.
vnoremap p :<c-u>YRPaste 'p', 'v'<cr>gv:YRYankRange 'v'<cr>
endfunction
Try this in your ~/.vimrc:
xnoremap <expr> p 'pgv"'.v:register.'y'
xnoremap means that this is only for Visual mode, not Visual + Select modes.
<expr> means that {rhs} of the xnoremap {lhs} {rhs} setting is evaluated as an expression.
In this case, our expression of 'pgv"'.v:register.'y' is using . for concatenation.
v:register is evaluated to the register being used during the fulfillment of the mapping.
The result of "xp would evaluate to pgv"xy, where x is the register.
I was helped by an answer to this stackoverflow question: Vim - mapping with an optional register prefix
in conjunction with Benoit's answer on this page
Luc's function worked well for me after I made a change to support the fact that I have clipboard=unnamed set:
function! RestoreRegister()
let #" = s:restore_reg
if &clipboard == "unnamed"
let #* = s:restore_reg
endif
return ''
endfunction
Use P to paste without yanking the deleted text.
:help v_P
With P the unnamed register is not changed (and neither the selection or clipboard), you can repeat the same change.
This behavior was introduced in v8.2.4242 (2022-01-28) and refined in v8.2.4881 (2022-05-06).
Or if your muscle memory is too strong:
xnoremap p P
Luc Hermitte's did the trick! Really good. Here's his solution put in a toggle function, so you can switch between normal behavior and no-replace-register put.
the command ,u toggles the behavior
let s:putSwap = 1
function TogglePutSwap()
if s:putSwap
vnoremap <silent> <expr> p <sid>Repl()
let s:putSwap = 0
echo 'noreplace put'
else
vnoremap <silent> <expr> p p
let s:putSwap = 1
echo 'replace put'
endif
return
endfunction
noremap ,p :call TogglePutSwap()<cr>
This is my solution.
vnoremap p p:let #+=#0<CR>
vnoremap P P:let #+=#0<CR>
I find out after paste, the old content is still stored in "0 register.
Just restore it to current clipboard by
:let #+=#0
duct-tape programming, but works for me:
nmap viwp viwpyiw
nmap vi'p vi'pyi'
nmap vi"p vi"pyi"
nmap vi(p vi(pyi(
nmap vi[p vi[pyi[
nmap vi<p vi<pyi<
Select the text and paste by P(uppercase).
Example:
viwP
See h: v_P for more infomation.
try -
:set guioptions-=a
:set guioptions-=A

Resources