Vim copy-paste to system buffer not behaving as expected - vim

I'm having a headache trying to figure out why vim isn't copying to a system buffer.
Here's my workflow:
vim asd
y1y
:q
vim qwe
p
On computerA and computerB, this works as I want it to: the line yanked from the file asd is put into the file qwe.
On computerC, this doesn't work.
All systems are running Ubuntu 8.04. computerA has the vim-full package installed, computerB and computerC have the vim package installed. computerA has xorg installed, is using the fluxbox window manager, and is accessed locally. computerB and computerC don't have X, and I'm sshing into both of them.
I've done a lot of reading and thought it was because computerC was compiled with -clipboard, but I ran vim --version on all three computers and only computerA was compiled with +clipboard.
Am I missing something obvious? I believe the user's .vimrc and the global vimrc files are the same. I can post output of vim --version and contents of vimrc files if that would help.

Vim by default doesn't copy to a system buffer. The only way that it would remember the contents is if the multiple instances of vim use the same .viminfo file. It's possible that the .viminfo file isn't being written due to file permissions or due to a different setting in 'viminfo' (the option).
For more information on the viminfo configuration, see
:help 'viminfo'
To look at your current configuration on each computer, do:
:set viminfo?
As an aside, if you want to use the system clipboard (which must be present, so you'd need to do ssh -X), you can use:
:set clipboard+=unnamed
Then all copy and paste operations will use the X11 selection buffer. Of course, you need vim compiled with +clipboard for this to work, so it won't solve your immediate problem. See:
:help 'clipboard'
for more information.
Perhaps you could post the result of the following on each computer?
:set viminfo?
:set clipboard?
This would help us to diagnose the problem in more detail. Could you also try:
vim asd
"ayy
:q
vim qwe
"ap
This will use register a instead of the unnamed register.

Why not use "+y and "+p, they work directly with the system's clipboard buffer
vim asd
"+y
:q
vim qwe
"+p

Take a look in .viminfo
if the last thing you yanked was hi it should contain:
Registers:
""0 LINE 0
hi
Maybe the file permissions are messed up?

It doesn't exactly solve your problem, but a perfectly fine workaround would be
vim asd
y1y
:sp qwe
p
:q
Which will open asd and yank from it (obviously), then split-open qwe, put your just-yanked item, and close the split-opened file. With this approach, there's no need to close the document you're working on a start a new instance of Vim.
And if you don't like the horizontal split, :vsp makes a vertical split, which can sometimes be easier to read/use.

OK, I don't know about the system buffer issue, but what about simply opening the new file with :e filename, then pasting, then saving and :e #ing your way back to the original file if needed? Yeah I know, tabs and splits are cool, but simple can do the job too.
You could also use ex, either from the target file to read the content to be included via the :r command supplying a line range, or from the source file to append the selected text (either via simple line number/range or the almighty g command) with :w >> filename.

Try this:
set clipboard=unnamed
It allows copying and pasting through the system clipboard.

Related

vim and .vimrc won't open after .vimrc modification. Need to force open

How do I force open my .vimrc?
I recently made a modification, not certain exactly what I did, but I think I accidentally typed an "x" in my plugins when trying to close. Suddenly neither vim or .vimrc will open.
I get a pause, several blank lines, and then terminal just returns to default command line.
Use vim -u NONE .vimrc to skip reading all startup files and edit .vimrc.

vim system register * and + not working

:echo has('clipboard') returns 1, but whenever I execute "+yy" or "*yy" nothing seems to be in those registers. If I use regular yy to copy another line of text, then try to paste from the register using CONTROL+V nothing happens. If I try "+p vim pastes the line of text I copied using the regular yy command.
What's going on here? I'm on FreeBSD by the way.
Your vim version may not be compiled with X11 clipboard integration.
In vim run the :version command and look for xterm_clipboard in the output. It will be prefixed with a + (supported) or - (unsupported) sign.
What worked for me in Ubuntu 20.04 and Vim 8.1.2269
sudo apt install vim-gtk3
Explanation
This package adds support for the x_term_clipboard in vim if not present already.
For more info: Click Here, #blankblank's answer.
Another thing that could be going on is your DISPLAY environment variable is not being set correctly. This could happen sometimes if you're running vim from tmux or screen.
Try opening a new terminal, running echo $DISPLAY, and then from the terminal running vim, leave vim, execute export DISPLAY=:0 (but replace :0 with the output from your other terminal), and then re-enter vim and see if clipboard works by doing "+p.
If you have copy something to clipboard and paste that in vim, you can use "+p.
+ is a quoteplus, which means CLIPBOARD documented X selection. :help quoteplus for more information.
If you want to copy something in vim to clipboard and paste the content in the other place by Ctrl+v, you can do "+yy, this will copy the current line to clipboard.
more information about vim register:
:help registers
:help quotestar
I also met this problem.
My case is that DISPLAY is not set properly in tmux.
And I found a script to automatically update tmux DISPLAY.
My problem was my input source English (US, intl., with dead keys), in addition to vim not being compiled with x-clipboard.
I don't face the same issue on windows, but on Ubuntu 20.04 I have to press "<space>+y with this input source.
Edit:
Seems I can fix this by switching the input source to English (Intl. with AltGr dead keys)

Trouble with vimrc file recognition with MacVim

I'm having difficulty getting MacVim (7.3-64) to recognize my .vimrc and .gvimrc files since upgrading to OS X 10.7.3. Previous, I've simply symlinked my .vimrc and .gvimrc using these commmands:
$ ln -s ~/.vim/vimrc /usr/local/Cellar/macvim/7.3-57/MacVim.app/Contents/Resources/vim/.vimrc
$ ln -s ~/.vim/gvimrc /usr/local/Cellar/macvim/7.3-57/MacVim.app/Contents/Resources/vim/.gvimrc
However, when I currently symlink my rc files, I can not get MacVim to recognize them. I've installed MacVim via Homebrew. Does anyone have any suggests as to what the problem could be?
The ~/.vim/ folder (for colorschemes and scripts) and the ~/.vimrc file (for custom settings and mappings) are guaranteed to work on every UNIX-like systems but neither of these are required for Vim (and MacVim) to work properly.
Start without symlinks or whatever : no .vim folder and no .vimrc or .gvimrc in your home folder. Does MacVim work? You are supposed to see a window with some introductory text, do you see that?
Quit MacVim and turn to the original .(g)vimrc files you want to use: where are they located? Where did you get them from? What is their content? Do you actually, really need them? Do their names actually start with a dot (do you see them or not in the Finder)? What is their encoding (in vim, :set fileencoding? and in the terminal, $ file /path/to/original/.vimrc)? Please, paste the content of the .vimrc file you want to use in your question.
If you are absolutely certain you need these files to work efficiently in Vim and assuming you actually have a custom .vimrc somewhere on your Mac, open Terminal.app and type this command (without the $):
$ cp /path/to/original/.vimrc ~/.vimrc
to copy your .vimrc to the canonical location.
Now launch MacVim. What do you see? You are supposed to see a window with some introductory text, is that what you see?
If MacVim doesn't work correctly with your ~/.vimrc you might want to comment its content, relaunch MacVim, uncomment a few lines and so on until you eventually find a bad setting.
Just put your .vimrc and .gvimrc into your home directory, i.e. ~/ It's picked up there allright.
BTW, you don't need to link to the homebrew dir. Those rc files are default files which you can override with your local user files.

Vim :e starting directory?

I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins

reuse vim instance from linux terminal

By making use of the remote feature in vim, is it possible to reuse an instance of vim to load up multiple files as needed.
It will be nice to have that facility from within the same terminal instance.
I am more keen to have a tab based interface, which available in vim 7+
The scenario should be
Open terminal
vim file1.cpp
Edit - Save - Ctrl+Z to get to prompt
open another file
vim file2.cpp
Now we have file1.cpp and file2.cpp open in the same editor
Is that possible?!
I'm not sure if this can be done in exactly the manner that you're specifying, but something very similar can probably be done using a vim server running on your local machine.
Look into the :help remote.txt in Vim.
If your version of vim was compiled with +clientserver you can use vim to create a vim server, and then execute commands on it, e.g. opening another file.
The --servername switch can create a new server, and the --remote switch can send additional commands or files to it.
e.g.
vim --servername test file1.txt
vim --servername test --remote file2.txt
I've had a look, and the vim I'm using as standard on xubuntu on one of my computers doesn't have it, but there are some instructions here that may help if yours has it compiled. If it isn't, installing gvim and symlinking is apparently an option (as gvim has it included by default), or compiling the binaries from source.
Edit:
I've had more of a play with gvim and this doesn't look possible to do this within the terminal. Control-Z suspends the job at the process level. I thought it might work with screen, but no communication seems to take place unless gvim has launched in a graphical window,
This is easy to do if you compiled VIM with +clientserver, as Andy suggested in a previous answer.
I did the following:
I started up VIM as a server:
vim --servername abc
I suspended it with CTRL+Z and did:
vim --servername abc --remote ~/.cshrc
fg
Now VIM had ~/.cshrc open.
Then I did:
vim --servername abc --remote ~/.aliases
fg
Now VIM had one buffer with ~/.cshrc and another with ~/.aliases.
Then I did:
vim --servername abc --remote-tab ~/foo_bar
fg
And now I VIM had one tab with the two previous buffers open and another tab with ~/foo_bar open.
In call cases VIM was running in the terminal, not as a GUI.
I have a couple suggestions for you, though neither is exactly what you're talking about. The first is NERD Tree, which gives you a nice tree-based file browser for opening other files. Very handy. I also set up a hot key (ctrl+o) to open NERD Tree for me. I keep an alias of whatever project I'm on in ~/curr/trunk, so this always works for me:
map <C-O> :NERDTreeToggle ~/curr/trunk/<CR>
The other thing that I would suggest is to do away with ctrl+z. It's somewhat clunky, and everyone I know who uses that method tends to get lost and end up with 3 or 4 vim's running in the background. Take a look at how to open a shell for yourself. I use a map for ;s to execute a shell script:
map ;s :!~/.vim/shell.sh<CR>
Which executes:
#!/bin/sh
/usr/local/bin/bash -l;
I also have a bit of magic in my .profile for making it obvious I'm in VIM:
if [ "$VIMRUNTIME" != "" ] ; then
export PS1="\u#\h \W \t$ vim> "
fi
</2 cents>
You can split the current screen and open two (or more) files in the following way:
For a horizontal split, do:
:sp 'filename'
and for a vertical split, do:
:vsp 'filename'
To tab between the two, hit ctrl+w, then use an arrow key to navigate to whichever file you want to edit.
Also, if you want to just switch files (and only have one open), you can do this:
:e 'filename'
G'day,
Or if you want to have multiple files but use the whole vim window for one file at a time you can just enter
:e 'filename'
to open the new file. You can do this multiple times. To see what you've currently got open, enter
:ls
To bounce between the files you've got open you can use cntl-^ (shift-cnt-6) and that will alternate between the main and secondary files (shown with a % and # in the file list)
Or you can enter
:n b
where n is the number at the beginning of the file you want in the list shown by the 'ls'command.
HTH
cheers,
You can set the hidden feature on vim, to allow it to have multiple files open:
:set hidden
Then you can open as many files as you want, without bothering to save them every time you want to switch a "buffer":
:e 'filename'
You have several commands to navigate the buffers:
:bnext
:bprev
:buffers
Of course, as mentioned by samoz, you can split your screen to see multiple buffers on the same window.
I'd suggest to read this for a good introduction about vim, it will save you a lot of time.
Good luck!

Resources