Creating "sessions" like Vim and Emacs

Hey people.

So in my forever deep dive into UNIX text editors, I saw that Vim can have sessions for currently open files, and you can open them up again with vim -S <session-file-name>.

Emacs can do this as well with desktop or persp-mode.

I was curious as to how I could do this with Kakoune. I know “session” for Kakoune technically refers to a running instance of Kakoune, so I dunno let’s call it a “workspace” or something.

I’d have to get all of the currently opening buffers (%val{buflist} minus *debug* and *scratch*), put that in a file, having the one my cursor is currently in be at the top or bottom of the file (or have some special mark by it), and have kakoune open each file in that list, opening the most recent focused buffer last.

To me, it makes sense to make it a text file (why bother make it some special file type) with a simple format that can be parse with a shell script.

What do y’all think?

A Vim session is a bit more complex than that - it’s not just what files are open, but the size, position, and configuration of tabs and panes, which buffers are visible in which panes, the runtime state of mappings, the runtime state of options, the current directory, and other stuff. Some of those things could be included in a Kakoune session file (like the list of buffers), some can’t be accessed by scripts (mappings and options) and some are meaningless for Kakoune (the sizes and positions of windows).

My kakoune-state-save plugin is more like Vim’s viminfo file - it restores the last cursor position when you open a file, and can restore your command/shell command/search history when you start Kakoune.

A “save-session” plugin might be useful to have, if you want to try writing one. I’d recommend:

  • Walk through %val{buflist}, and ignore buffers whose %val{buffile} does not begin with / (it’s possible for a scratch buffer’s fake filename to begin with /, but generally they begin with *)
  • It’s possible for “the cursor” to be in multiple buffers (if you have multiple windows open) or no buffers (if you have a session with no connected clients), so you’d need to figure out a sensible way to handle that
  • Might as well include the server’s current directory in the session data, for completeness

Rather than a single text-file, I’d probably store a session as a directory, so I could store filenames contaning arbitrary bytes without having to worry about escaping them; plus, I could write the files with :echo -to-file and them with Kakoune’s %file{} expansion. I’d imagine something like:

  • working-directory contains the value of %sh{printf %s "$PWD"}
  • active-buffer contains the value of %val{buffile} (if any)
  • buf-0buf-N contain the individual items in %val{buflist}