Dumping current .zshrc from terminal
I've wanted to make quick edit in a .zshrc, but vim said that it can't be written or something. So I quit, with :q!. Then I wanted to reopen this file... and it was empty.
I still have terminal open which works with previous configuration. How can I restore complete .zshrc(or as close to complete) from it? I've already saved my aliases with 'alias' command.
First of, there is no way to get the actual ~/.zshrc
from a running shell session. This is mainly due to the fact that ~/.zshrc
is essentially a shell script and only the settings it does make it into the shell session, but not the logic behind the settings.
Nevertheless, you actually can get some information from a running shell session, that may help you in recreating some of the settings that were in your lost .zshrc:
- As you already noted, you can get your aliases from the shell builtin
alias
. Withalias -L
you actually can get a list of calls toalias
, which you can put into your new~/.zshrc
just as-is. - Calling
setopt
gives you a list of the shell options that are not set to the default values. -
set
will print a list of all parameters and their values. A lot of it was probably not set explicitly in your configuration, but some of it was. Settings like the prompts (PROMPT
,PROMPT2
,PROMPT3
,PROMPT4
,RPROMPT
andRPROMPT2
), history (HISTSIZE
,SAVEHIST
,HISTFILE
, etc.), default editor (EDITOR
) and pager (PAGER
) can be found there, so having a look may be worth it. -
autoload
will give at least some indication what modules were loaded. You can ignore any function declaration starting with an underscore as they are most likely loaded by the completion system. Although the rest may not necessarily have been loaded explicitly, it will at least give some indication. -
zstyle -L
will print a list of the settings made withzstyle
as list of commands (just likealias -L
). -
bindkey -L
will print your current keymap as a list of commands. If the list is pretty long and does not have (a lot of) commands starting withvi-
your shell probably ran in emacs mode, in which case puttingbindkey -e
into your newzshrc
will probably restore most settings. For the rest you can diff the outputs ofbindkey -L
in the running and a new shell. If you utilized thevicmd
mode those settings can be retrieved withbindkey -aL
. -
functions
gives you the listings of the functions defined in your session. Again, not all of them may have been actually set in your configuration, but it is a starting point.