7

I have the following workflow:

  1. Open Vim
  2. Use Visual mode linewise (V), select some lines, and yank them (y)
    • I get a message "151 lines yanked" or something like this
  3. Quit Vim
  4. Start another instance of Vim
  5. Put with p. Only half of the previously copied lines are inserted!

What happens? I recently learned about the registers " and *. I guess I need to extend my default register to more lines? Maybe it is limited somehow.

Or, since I start another Vim, should I automatically copy to the system clipboard? I don't want to type any extra keys. Can it be configured somehow?

4
  • 1
    Didn't you post this on SO earlier? Why create a new account for this site?
    – Friedrich
    Commented Jul 2 at 10:26
  • no idea - I am in hurry someone point me here I just logged in using google , maybe wrong account :facepalm:
    – user51883
    Commented Jul 2 at 11:14
  • 1
    You can reach out to a moderator to have your accounts merged.
    – Friedrich
    Commented Jul 2 at 11:15
  • 1
    I just did it @Friedrich thanks !
    – user51883
    Commented Jul 2 at 11:20

4 Answers 4

12

When you quit Vim, your registers' contents will be saved to a viminfo file. See :help viminfo for all the details.

What and how much is saved is controlled by the 'viminfo' option, which is documented in :help 'viminfo'.

As it happens, the default number of lines to be saved for each register is 50. That's the item after the angle bracket: <50. Anything beyond that will be chopped off.

This is the documented default. You can change it by putting the following in your vimrc:

set viminfo='100,<5000,s10,h

This will potentially result in a much bigger viminfo file. In practice, this will be irrelevant.

Anyhow, I'd recommend a different workflow instead of relying on viminfo (which might not be available for a number of reasons).

Some alternatives:

  • The best solution in my opinion is to simply open two buffers in the same Vim instance. Vim is great at managing several buffers at the same time. Open your file1, visual-select the text and y. Then, :edit file2 and p.

  • You might be interested in :help clipboard-unnamed to use the clipboard register by default for all yank/put operations. Note, however, that you'll also need a clipboard manager for this to work on X11. A brief but very comprehensive resource on X11 clipboards is this nice article I keep putting in my answers.

  • You could (in theory) also save the selection as a temporary file with '<,'>:write /tmp/my_selection and then :read /tmp/my_selection in your second file. This is more cumbersome than editing two buffers but there may be edge cases where this is the way to go.

2
  • 1
    Thanks! I just saw your answer - I found viminfo myself finally and wrote my own answer just before realizing you posted yours
    – user51883
    Commented Jul 2 at 11:13
  • 1
    Thanks for the complete answer (just upvoted it !) and advices
    – user51883
    Commented Jul 3 at 9:11
2

So, it turns out vim register is limited to 50 lines, see https://www.reddit.com/r/vim/comments/ydq1h/setting_vim_register_size/ or Viminfo truncates named registers. Setting to increase limit?

And it can be extended, for example to 1000 lines, by using the following configuration in .vimrc:

set viminfo=%,<1000,'10,/50,:100,h,f0,n~/.viminfo

(now register will contain 1000 lines instead of 50)

Found explanation of the cryptic fields in this answer: https://stackoverflow.com/a/23036077/2753095

3
  • You mean you don't have any clipboard manager to hold the selection after Vim has been closed?
    – Matt
    Commented Jul 2 at 11:30
  • see answer by @Friedrich - vim uses its own clipboard (aka register) by default, and it saved in .viminfo file and limited to 50 lines but it can be extended (which I want)
    – user51883
    Commented Jul 2 at 11:44
  • You said that you did "+y but got back 50 lines anyway. That's why I ask. If you copy into OS clipboard then the clipman (whichever you have) should hold the selection, so it persists between Vim instances. No matter what viminfo settings are.
    – Matt
    Commented Jul 2 at 11:58
0

Vim has more than one internal "clipboard" aka "register". It is convenient as most of the time you don't want to share your text between different applications in your system but keep editing inside Vim only.

And when you really need to share some piece of text you should better specify "using plus register" manually. I.e. "+y and "+p instead of plain y and p. And so on.

8
  • What if I want to share with another vim like in my workflow described above? It kind of work - only pasting half of the lines...
    – user51883
    Commented Jul 2 at 9:49
  • in fact I just tried "+y, "+p and it gives the same problem, only 50 lines over 150+ are pasted
    – user51883
    Commented Jul 2 at 9:51
  • @user51883 OS, Vim version, size of copied file?
    – Matt
    Commented Jul 2 at 9:52
  • Arch linux, vim 9.1.496, file 5 KB but I only take for example 115 lines of Python code from it
    – user51883
    Commented Jul 2 at 9:54
  • @user51883 Reproduce with clean config; make sure that's not your local system problem (OS clipman, Wayland<->X11, etc.) and raise an issue at Vim's github page. (Though, it feels strange when everyone is able to copy/paste 5K through OS clipboard while you are not, don't you think so?).
    – Matt
    Commented Jul 2 at 10:11
0

This is a surprising problem that may be specific to Vim (as opposed to classical vi). I have never personally come across it—I have often enough yanked and pasted hundreds of lines without problems; but I never use Vim's fancy features. One of the first things I do is to set compatible mode in .vimrc:

set t_ti= t_te=
set compatible
set expandtab ts=4 sw=4 ai
set esckeys
set ttimeoutlen=100

The other lines are just what I find useful (eg. set t_ti= t_te= ensures that the screen isn't blanked when you leave vi).

The way I copy/paste is:

  • Mark the start or end of what you want to copy with ma (other letter than a are available)
  • Move to the other end of the text area
  • Yank with 'ay
  • Paste with p (after) or P (before)
3
  • 3
    What you're missing is that the OP yanks, quits vim and starts another instance, and then puts. I don't think classical vi was capable of that at all.
    – D. Ben Knoble
    Commented Jul 3 at 17:28
  • I tested with :set compatible: no viminfo is written when quitting, none restored -> registers are wiped out.
    – Friedrich
    Commented Jul 3 at 18:05
  • @D.BenKnoble You're right, +1 - I didn't notice that, and classical vi doesn't do that (unless you do some magic with running external commands from vi)
    – j4nd3r53n
    Commented Jul 4 at 13:07

Your Answer

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

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