18

I copied a part of the HTML out of a web page and wanted to save it in a file. For that I started a new vim session in a terminal window, with a (new) filename specified on the commandline, hit i to get to insert mode and then CtrlShift+V and waited while [-- INSERT --] showed at the bottom and waited...

As vim was non-responsive after several seconds, I opened 'Text Editor' from the Applications→Accessoiries menu pasted the text (which showed up within a fraction of a second, saved it under a new name, closed, and killed the Vim session that still was not done, 1.5 minutes later. The amount of text was 186K in 3200 lines, not excessive I would say, nor with overly long lines.

Is there a way to speed up these kind of insertions in vim and/or is there an explanation why this is so slow compared to using the, otherwise horrible and mouse oriented, Text Editor?

(The %CPU according to top doesn't come above 5%, although I have some processors free in the system, so it might be some I/O bound problem, that doesn't exist when reading the same text from a file)

Version info:
Ubuntu 12.04
Vim: 7.3, with patches as supplied by Ubuntu 12.04
bash: 4.2.25
gnome-terminal: 3.4.1.1

7
  • 1
    I think it's slow b/c it's having to parse the input and try to format it etc. I'm not sure of the option to do this but would assume you could temporarily disable that feature w/in vim, do the pasting, and then re-enable it. Also try using the :set paste feature to see if it improves things. See :help paste it describes how vim makes no distinction b/w typing and pasting.
    – slm
    Jul 9, 2014 at 12:49
  • I would expect vi to try to parse the file when reading in (after saving the way @l0b0 suggested), as well. But that works fast.
    – Anthon
    Jul 9, 2014 at 12:52
  • True, but I'm imagining that it has to reparse everything it's already parsed + the new character as you're pasting it in. That seems to be what's going on when you edit a file too, to a degree.
    – slm
    Jul 9, 2014 at 13:01
  • 1
    the :set paste doesn't seem to really make a difference (I am not using a stopwatch, just the wall-clock).
    – Anthon
    Jul 9, 2014 at 13:02
  • OK, was just a thought...
    – slm
    Jul 9, 2014 at 13:03

3 Answers 3

21

To save a lot of clipboard text to file quickly, you can run cat > file.txt, paste the contents, then press Ctrl-d.

If you have xsel installed, you can do :r !xsel to insert the "primary" (aka. "mouse") selection in Vim, or :r !xsel -b to insert the "clipboard" (Ctrl-c) buffer. You can also save the selection directly to a file with xsel >file.txt or xsel -b >file.txt. This removes the need for separate pasting + EOF actions, and avoids printing the entire copy buffer in the terminal.

If you have no xsel but xclip, the corresponding commands are xclip -out for the primary selection, or xclip -out -selection clipboard for the clipboard buffer.

6
  • 1
    Thanks, that at least works without having to revert to the mouse, and inserting the file into vim (e.g. when I don't start with an empty file) is quick enough.
    – Anthon
    Jul 9, 2014 at 12:48
  • I don't understand why this couldn't be binded to ctrl+shift+v there is absolutely no sense in all of this.
    – mchid
    Nov 13, 2016 at 5:12
  • BEWARE!!! If the text you're pasting contains any shell variables (starting with $), the shell (bash, zsh, etc.) will try to expand it, and if the variable by that name does not exist, the corresponding text will be lost. For example, if the pasted content has a $xyz, the shell will replace that with the value of the shell variable named xyz, and if the shell variable xyz does not exist, your resulting text will NOT contain the $xyz. Feb 2, 2022 at 18:06
  • This answer is dangerous, and can result in loss of data. See the sibling comment that starts with BEWARE. Feb 2, 2022 at 18:08
  • cat > file.txt, :r !xsel and xclip -out -selection clipboard all work for me with the string echo $foo. I don't know exactly what you're doing @GurjeetSingh, but please be more specific if you're going to say there's information loss.
    – l0b0
    Feb 2, 2022 at 18:49
5

This is a buffer flush-to-disk problem. Vim tries to keep your work safe and doesn't assume you can type several thousand characters per second. Read :help swap-file for some details on the buffering. The solution to your problem is this:

Turn off vim's swapfile either with:

vim -n <your file>

or from within vim before the paste:

:set noswapfile

See :help swapfile for more details.

Another option is to simply turn off the syncing to disk of the swap file with :set swapsync= but this option takes more keystrokes to undo and I'm lazy. :)

Turning off swap is not safe for normal operations! Immediately after the paste, either use :set swapfile or :set swapsync=fsync to revert back to normal behavior (though technically, normal behavior might have been sync and not fsync, check with :set swapsync? beforehand if you want to go this route).

1
  • 5
    this did not speed up things for me...
    – elig
    Feb 15, 2020 at 4:28
1

If you have xterm_clipboard feature, you can use the * and + registers. These registers interface with the X11 primary selection buffer, and clipboard (respectively).

Thus if you've copied something via CTRL+c, you can paste it in vim with "+p.

If you've simply highlighted it without copying, you can paste it with "*p.
You can also make the * buffer the default buffer by doing :set clipboard=unnamed. Then any yank (y), paste (p), etc, that doesn't specify a register will use the * register. Vim 7.3.74 also added clipboard=unnamedplus, which will use the + register by default.

You can also copy things into the primary selection & clipboard buffers by yanking, for example: "+yy.

 

Note, that as mentioned, these capabilities all need the xterm_clipboard feature. You can see if you have this feature by doing :version inside vim, or vim --version from the shell, and look for +xterm_clipboard. If it says -xterm_clipboard, you do not have it and would have to recompile vim.

4
  • I don't have that feature. Would recompiling help even vim is not running under xterm? (I am using gnome-terminal).
    – Anthon
    Jul 9, 2014 at 13:53
  • @Anthon If you're using GUI VIM, then I believe the registers are also enabled. xterm_clipboard is only if running vim from terminal.
    – phemmer
    Jul 9, 2014 at 13:55
  • This is text only, no GUI VIM, but not running from xterm.
    – Anthon
    Jul 9, 2014 at 13:56
  • Oh, sorry I misread. Yes xterm_clipboard applies to any terminal emulator, not just xterm. I myself use urxvt.
    – phemmer
    Jul 9, 2014 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .