Search code examples
vimclipboardroot

root unable to use system clipboard in vim 8.2


I have Vim version 8.2 installed (gvim package on Arch Linux). I have both +clipboard and +xterm_clipboard features. In my ~/.vimrc I have set clipboard=unnamedplus and created a symbolic link to this file in /root so that I only need to edit one config. The problem is that yanking and pasting works only for my regular user account but not for root: if I run vim as root I can't even copy to system clipboard using "+y command: both PRIMARY and CLIPBOARD registers stay unchanged.

My terminal emulator is Konsole 20.04.0. Any thoughs as to what might be causing this weird behavior? It might be worth noting that I use su -l to get a root shell.


Solution

  • In order to access the X11 clipboard, it's required that the DISPLAY variable be set to the proper value and the magic cookie (that is, the authentication token) be accessible.

    Usually, if you're using sudo or su to run as root, the environment variables are cleared for security reasons, so you're probably missing the DISPLAY environment variable. You can either set the DISPLAY environment variable explicitly when changing shells (e.g., export DISPLAY=:0.0) or use some script to dump and reload certain environment variable to and from a file, or you can skip the -l flag for su or use the -E flag for sudo.

    Note that these latter two options are poor for security, but in general it's not a good idea to let root access the same X11 session as an unprivileged user, so what you want to do is not great for security in the first place. If you're the only user on the system it might be okay, depending on your level of comfort and threat model.