7

After copying text to the system keyboard clipboard in vim, I'm unable to paste the text after exiting vim.

I find this behavior frustrating, since I often copy text in files to paste it on the command-line, and this is currently not possible without opening two tabs in the terminal or using a terminal multiplexer (e.g. tmux).

" system clipboard: CLIPBOARD (+)
noremap <leader>y "+y

How can I prevent vim from clearing the system clipboard on exit?

vim --version:

$ vim --version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Jun 15 2019 16:41:15)
Included patches: 1-875, 878, 884, 948, 1046, 1365-1368, 1382, 1401
Modified by [email protected]
Compiled by [email protected]
Huge version with GTK3 GUI.  Features included (+) or not (-):
+acl               +extra_search      +mouse_netterm     +tag_old_static
+arabic            +farsi             +mouse_sgr         -tag_any_white
+autocmd           +file_in_path      -mouse_sysmouse    +tcl
+autochdir         +find_in_path      +mouse_urxvt       +termguicolors
-autoservername    +float             +mouse_xterm       +terminal
+balloon_eval      +folding           +multi_byte        +terminfo
+balloon_eval_term -footer            +multi_lang        +termresponse
+browse            +fork()            -mzscheme          +textobjects
++builtin_terms    +gettext           +netbeans_intg     +textprop
+byte_offset       -hangul_input      +num64             +timers
+channel           +iconv             +packages          +title
+cindent           +insert_expand     +path_extra        +toolbar
+clientserver      +job               +perl              +user_commands
+clipboard         +jumplist          +persistent_undo   +vartabs
+cmdline_compl     +keymap            +postscript        +vertsplit
+cmdline_hist      +lambda            +printer           +virtualedit
+cmdline_info      +langmap           +profile           +visual
+comments          +libcall           -python            +visualextra
+conceal           +linebreak         +python3           +viminfo
+cryptv            +lispindent        +quickfix          +vreplace
+cscope            +listcmds          +reltime           +wildignore
+cursorbind        +localmap          +rightleft         +wildmenu
+cursorshape       +lua               +ruby              +windows
+dialog_con_gui    +menu              +scrollbind        +writebackup
+diff              +mksession         +signs             +X11
+digraphs          +modify_fname      +smartindent       -xfontset
+dnd               +mouse             +startuptime       +xim
-ebcdic            +mouseshape        +statusline        +xpm
+emacs_tags        +mouse_dec         -sun_workshop      +xsmp_interact
+eval              +mouse_gpm         +syntax            +xterm_clipboard
+ex_extra          -mouse_jsbterm     +tag_binary        -xterm_save
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cairo -I/usr/include/libdrm -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/fribidi -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/uuid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -Wdate-time  -g -O2 -fdebug-prefix-map=/build/vim-4Pursk/vim-8.1.0875=. -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -L. -Wl,-z,relro -Wl,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E  -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim   -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE  -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl  -L/usr/lib -llua5.2 -Wl,-E  -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/x86_64-linux-gnu/perl/5.28/CORE -lperl -ldl -lm -lpthread -lcrypt  -L/usr/lib/python3.7/config-3.7m-x86_64-linux-gnu -lpython3.7m -lcrypt -lpthread -ldl -lutil -lm -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -ldl -lz -lpthread -lm -lruby-2.5 -lpthread -lgmp -ldl -lcrypt -lm
6
  • Are you running an X server? Vim is supposed to copy register + to the X server's "cut buffer" before exiting thus preserving the selection (see How do registers + and * interact with suspending Vim via Ctrl-Z?). Without X I'm not sure how it would be retained.
    – B Layer
    Oct 15, 2021 at 12:37
  • I use a standard desktop version of Debian with tmux, so I use an X server for graphics, right?
    – Shuzheng
    Oct 15, 2021 at 12:38
  • tmux just requires a terminal emulator, not any graphics per-se. I run tmux without X all the time. With a desktop manager, though, I'd think you have X but this is out of my realm of experience. Try How to check if X server is running?
    – B Layer
    Oct 15, 2021 at 12:45
  • @BLayer I'd say yes. From wiki.archlinux.org/title/Clipboard#History, "In X10, cut buffers were introduced. These were limited buffers that stored arbitrary text and were used by most applications. However, they were inefficient and implementation of them varied, so selections were introduced. Cut buffers are long deprecated, and although some applications (such as xterm) may have legacy support for them, it is both not likely and not recommended that they be used."
    – Quasímodo
    Oct 15, 2021 at 12:56
  • also, I believe cut buffers only support ascii text Oct 15, 2021 at 14:14

1 Answer 1

9

Solution

Install a clipboard manager.

For example, I use clipmenu. I start it whenever X is started by putting this

CM_OWN_CLIPBOARD=1 clipmenud &

in my ~/.xinitrc.

Why

Yours is a common misunderstanding when talking about X selections.

From Arch Wiki: Clipboard:

According to the selection protocols, nothing is copied until it is pasted. For example, if you select some word in a terminal window, close the terminal and then want to paste it somewhere else, it will not work because the terminal is gone and the text has not been copied anywhere. If you want the word to be preserved after closing terminal window, consider installing a clipboard manager.

From Ubuntu Wiki: ClipboardPersistence:

The problem happens because Xorg takes a conservative approach to copying. It copies only a reference to the original data when the user performs a select or copy. It doesn't go and retrieve the actual data from the source program until the user requests a paste. It saves a lot of unneeded transfer of data this way, at the expense of having no way of retrieving data from a closed program that hasn't saved its clipboard somewhere else.

14
  • Thank you. But why is Xorg in play, when running Vim inside Tmux? I’m by no means an Xorg expert, but isn’t Tmux starting a terminal, not a graphical application rendered by Xorg?
    – Shuzheng
    Oct 16, 2021 at 8:45
  • @Shuzheng Tmux isn't relevant here: Vim is talking directly to the X11 server when you're using the "* and "+ registers, not to or through the terminal process. (As with any X11 application, it uses the DISPLAY environment variable to determine how to contact the X11 server. An X11 application doing this need not be rendering anything on the screen; xmodmap is another example of a program that talks to the X11 server but uses only console/terminal I/O for its user interface.)
    – cjs
    Oct 16, 2021 at 10:43
  • @cjs Thanks, so Vim is not talking to Tmux (terminal process). However, Vim is rendered by X11, since its enclosing terminal (e.g. gnome-terminal) is rendered by it?
    – Shuzheng
    Oct 16, 2021 at 17:03
  • 1
    @Shuzheng All you need to understand is that a terminal Vim, as a standalone program, may or may not connect to the X Window System if one is running. The possibility of connecting or not depends on how it was compiled and if you invoked with with the -X flag. If it does connect to X, then it does not matter if inside Tmux, Screen or a bare Xterm, since Vim will directly talk to X (e.g. for owning clipboard/primary), even though it is not responsible for drawing it's own X window.
    – Quasímodo
    Oct 16, 2021 at 17:14
  • 1
    With a quick search, I don't think MacOS uses the X11 protocol, so it is no surprise if it behaves differently.
    – Quasímodo
    Oct 16, 2021 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.