Search code examples
vimhistoryvim-plugin

How to Make Vim Different Documents Have Different Historical Records?


I use:history / Get Search Records, When I switch to another file, the resulting search records are superimposed on different files.How to Make Different Documents Have Different Historical Records


Solution

  • Vim's command-line histories are global. The technical reason for that is that histories are stored in a flat list in the :help viminfo-file. Also logically, it doesn't make sense to assign certain commands (like :bufdo) to a single buffer.

    So, there isn't a simple option that you can set that would make search history local to a buffer. With enough determination, you could implement a plugin for that, though. I see two possible approaches:

    • You multiplex one viminfo file (that stores the histories) per buffer into the global history, via :help :rviminfo and :wviminfo. That's relatively easy with :autocmds, and you can keep using the default (global) :history and related commands. But many other settings (marks, jumps, previous files, etc.) are stored inside the viminfo, and these effectively would be localized, too (unless you somehow just swap out the search history part).
    • You hook into search commands and regularly synchronize the global search history (via :help histget()) into a buffer-local storage, and then offer your own commands for search recall, e.g. by creating a scratch buffer that shows the local searches, just like the built-in q/ would do. That would probably result in much better usability, but it's a bit more effort to recreate your own history commands.