You, sir, are looking for mode hooks: a set of functions that run every time you enter a buffer in a given mode.
Note that mode hooks will not persist state between sessions, but your question really appears to be about running the same command every time you enter a buffer in a given mode.
Now: for hideshow, the default binding C-c @ C-l
is for hs-hide-level
, whose docstring reads:
(hs-hide-level ARG)
Hide all blocks ARG levels below this block...
Rather than pick an ARG
level (you'd probably want 1, I'd guess), you might consider hs-hide-all
:
(hs-hide-all)
Hide all top level blocks, displaying only first and last lines...
Let's say you want to hide everything every time you open an elisp
buffer. Because the latter function does not require arguments, you could add it directly to a hook like so:
(add-hook 'emacs-lisp-mode-hook #'hs-hide-all)
If you wanted to use the former, you would need to write a specific function (the preferred method), or else wrap it in an anonymous lambda
function:
(add-hook 'emacs-lisp-mode-hook (lambda () (hs-hide-level 1))) ; not preferred
(defun my-hs-hide-stuff ()
(hs-hide-level 1))
(add-hook 'emacs-lisp-mode-hook #'my-hs-hide-stuff)