skip to Main Content

I am trying to use Vim to locate and copy/paste some code I need to analyze and take notes on. I am using Debian, in a Windows WSL environment. That’s what makes this tricky.

The regular “yank and put to global register” commands "+y and "*y commands didn’t work.

On the other hand, the brute force approach where I just use the mouse to scrape the terminal text won’t work either. Strangely, WSL terminal has mouse support, and Vim can track its movements, select in visual mode, etc. So Vim intercepts the selection command, and then there is nothing selected for ctrl-shift-c to copy into the Windows clipboard.

I know the WSL terminal supports copy and paste, and I can successfully do it if I cat my file to the screen, and copy and paste that using ctrl-shift-c and ctrl-v. But then I lose out on ease of navigation.

What’s the best way to copy text out of Vim inside a WSL terminal and into the windows clipboard?

4

Answers


  1. On Linux, Vim’s clipboard support is intimately tied to X. If you want the same level of integration between WSL and the rest of Windows as you are used to in a proper Linux Box you will have to install a Windows X Server.

    1. On the Linux side, install a clipboard-enabled build of Vim. The vim-gtk package is fine.

    2. On the Windows side, install an X Server like VcXsrv (there are many alternatives, you are on your own to find the one that best suit your needs).

    3. You generally have to edit a couple of configuration files on the Linux side for your X clients to use the right X Server. What to do exactly will depend on the X Server you choose.

    4. In Vim, on the Linux side, use either "+ or "* as you would if you where on a genuine Linux box.

    Login or Signup to reply.
  2. Like romainl mentioned, clipboard is at X level. So the most important step is you need to have an X-server running on Windows, and you need to set DISPLAY variable on Linux to point to X-server. Then in neovim set clipboard=unnamedplus or vim set clipboard=unnamed to link to the system clipboard.

    Follow this nice gist should make things work.

    For me I use fish shell, the wsl specific logic becomes in your config.fish.

    if uname -r | grep 'microsoft' > /dev/null 
      set -l LOCAL_IP (cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
      set -xg DISPLAY $LOCAL_IP:0
    end
    
    
    Login or Signup to reply.
  3. Answer is, do a vim visual selection then do the command:

    :'<,'>w !clip.exe
    

    This pipes the current selection out to the shell command clip.exe, which utilizes WSL’s ability to execute Windows executables (even with a pipeline). Text piped to clip.exe goes to the Windows clipboard.

    Also, this command saves the whole file to the clipboard (not the requirement):

     :w !clip.exe
    
    Login or Signup to reply.
  4. I have "WSL Debian" installed and I installed a neovim package inside. PAckage installs a nvim text editor without a clipboard support. I can verify this like this:

    $ nvim --version
    
    NVIM 0.1.7
    Build type: None
    Compilation: /usr/bin/cc -g -O2 -fdebug-prefix-map=/build/neovim-HEl3mV/neovim-0.1.7=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -DDISABLE_LOG -Wconversion -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1  -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wvla -fstack-protector-strong -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -DHAVE_CONFIG_H -D_GNU_SOURCE -I/build/neovim-HEl3mV/neovim-0.1.7/build/config -I/build/neovim-HEl3mV/neovim-0.1.7/src -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/build/neovim-HEl3mV/neovim-0.1.7/build/src/nvim/auto -I/build/neovim-HEl3mV/neovim-0.1.7/build/include
    Compiled by [email protected]
    
    Optional features included (+) or not (-): +acl   +iconv    +jemalloc +tui
    For differences from Vim, see :help vim-differences
    
       system vimrc file: "$VIM/sysinit.vim"
      fall-back for $VIM: "/usr/share/nvim"
    

    Note that under Optional features included there is no +clipboard entry and this means that my nvim doesn’t support clipboard out of the box.

    Then I open the nvim editor and execute a command :CheckHealth to get this feedback:

    ## Clipboard
      - WARNING: No clipboard tool found. Using the system clipboard won't work.
        - SUGGESTIONS:
          - See |clipboard|
    

    This tells me (a) that clipboard is not working currently and (b) to use a command :help clipboard inside the nvim to get more info. So I execute this command and I can read this:

    Clipboard access is implicitly enabled if any of the following clipboard tools
    are found in your `$PATH`.
    
      - xclip
      - xsel (newer alternative to xclip)
      - pbcopy/pbpaste (Mac OS X)
      - lemonade (for SSH) https://github.com/pocke/lemonade
      - doitclient (for SSH) http://www.chiark.greenend.org.uk/~sgtatham/doit/
    
    The presence of a suitable clipboard tool implicitly enables the '+' and '*'
    registers.
    
    If you want to ALWAYS use the clipboard for ALL operations (as opposed
    to interacting with the '+' and/or '*' registers explicitly), set the
    following option:
    
        set clipboard+=unnamedplus
    

    This tells me that even though clipboard support was not compiled in, it is still possible to implicitly enable it if we only install e.g. xsel. So I do it:

    $ sudo apt install xsel
    

    This also tells me to put the below line in my ~/.config/nvim/init.vim:

    $ set clipboard+=unnamedplus
    

    It looks like this should already be solved, but at this point things will not yet work. Why is that? This is because xsel (as it’s name implies) is a graphical application that needs X server in order to run!

    So we install X server for Windows! One option is to simply install "VcXSrv"(link). This will create a "Xlaunch" launcher in the Windows start menu. We run this launcher and just click next until the "Extra settings" window. Here we make sure to check all the boxes like shown below and click "Next".

    enter image description here

    Now we will store our "Xlaunch" configuration by pressing "Save Configuration" and make sure to store our configuration as:

    C:Users<Username>AppDataRoamingMicrosoftWindowsStart MenuProgramsStartupconfig.xlaunch
    

    This will make sure that X server will start with the same configuration whenever Windows starts! Note that X server has to be running before starting the "WSL Debian".

    Now we click "Finish" and for this session X server will run.

    Now inside the "WSL Debian" we only have to export a DISPLAY enviromental variable which is where X server applications e.g xset will search for a working X server session. Our job is to point them to our Windows machine! So we can export DISPLAY like this:

    LOCAL_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
    export DISPLAY=$LOCAL_IP:0
    

    If you want you can add these two lines in the ~/.bashrc file on "WSL Debian" so that they will always be exported when you open an interactive terminal.

    Now everything should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search