2

tl;dr

Is there a Vim plugin that allows me to save/load registers to/from files with minimal typing, preview of registers, and other amenities?


Long version

Vim has a lot of registers. However, the "only" ones that serve a "storage" purpose are registers a to z. Indeed

  • only the user can edit them and only by explicitly naming them (whereas numbered registers change, or actually shift, automatically),
  • they don't affect and are not affected by the "outside world" (+ and * do affect and are affected by what happens outside of Vim),
  • they have no special behavior (_ is the black hole, " is the last used register, and so on).

Now, for me it's natural to resort to a-z registers when I want to put stuff there that I might reuse several times during a workday. However, the various usages often depends on what I'm doing.

There are times when I'm debugging C++ code, that I often need to efficiently (from they keystroke standapoint) load the same, say, 4 or 5 dynamic libraries everytime I restart the debug session, so it's very useful to have those libraries (or the whole line for gdb -exec shared somelibrary.so) in the registers.

Much less often, but still not only once in a lifetime, there are times when I'm doing some text editing that requires that I run the same few macros I run a month ago.

It would be nice to have registers' contents saved in "databases". For instance, having a file named dynamicLibrariesLoadCmds.txt with content such as

a: -exec shared somelibrary.so
b: -exec shared someotherlibrary.so
q: -exec shared onemorelib.so

and then some command to load all those lines into the appropriate registers.

Is there a plugin that provides such a functionality?

1
  • It sounds like you actually need a clipboard manager with bookmarks/favorites instead of a vim plugin.
    – r_31415
    Jul 8 at 19:52

2 Answers 2

1

Here's a simple function that achieves your goal, but it assumes that the text to the right of : is properly formatted for Vim (so your examples all need quotes to make strings). It does incidentally mean that the content can be an arbitrary vimscript expression :) this latter could be a security bug, so only load databases you trust. Alternately, change parts[1] and {content} to string(parts[1]) and {string(content)}, respectively, avoiding the need for quotes or any security holes from evaluating the content. (Executing the register as a macro would still, obviously, be vulnerable.)

function ReadReg(file) abort
  eval readfile(a:file)
      \ ->map({_, line -> split(line, ': ')})
      \ ->map({_, parts -> execute(printf(':let @%s = %s', parts[0], parts[1]))})
endfunction

In vim9:

vim9script
def ReadReg(file: string)
  readfile(file)->mapnew((_, line) => {
    var [reg, content] = split(line, ': ')
    execute $'@{reg} = {content}'
  })
enddef

Then you could bind a :command if you wanted.

3
  • all need quotes to make strings Could make use of :h string() function to add proper quoting, like printf('let @%s = %s', parts[0], string(parts[1])). content can be an arbitrary vimscript expression A potential security bug?
    – Matt
    Jul 6 at 15:48
  • Agreed on both fronts; wasn't aware of string @Matt. I will make both more explicit.
    – D. Ben Knoble
    Jul 6 at 15:50
  • oof, forgot about setreg. The execute could probably be removed with that.
    – D. Ben Knoble
    Jul 6 at 17:49
1

Register contents already saved into viminfo file. But you can create some sort of "extra spare file" if you wish.

Here is (almost) one-liner:

call split('abcdefghijklmnopqrstuvwxyz', '\zs')
  \ ->filter('!empty(getreg(v:val))')
  \ ->map('printf("call setreg(%s, %s)",
  \     string(v:val), string(getreginfo(v:val)))')
  \ ->writefile('myregs.vim')

And then :source myregs.vim to restore them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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