2

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.

3
  • You should look around at your disk partitions. Some may be full.
    – user556625
    Feb 20, 2016 at 13:16
  • Yes, that was likely a source of the problem. My root partition was full, I wanted to clean it up, found some tool, installed it, then I wanted to add alias for conveniance of using it to .zshrc.... and then it disappeared. Now I have cleaned up 25% of root partition, but there is no .zshrc... I wonder how it happened. Because partition being full should maybe cause impossibility of editing this file... but deleting it?
    – Ezo
    Feb 20, 2016 at 13:38
  • it can't be written or something I don't think that vi complained by saying literally or something. Please paste next time the exact eror message. If you have accidentally deleted your file, you have to create it from your backup. You do make regular backups, don't you? Nov 24, 2021 at 13:01

3 Answers 3

13

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. With alias -L you actually can get a list of calls to alias, 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 and RPROMPT2), 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 with zstyle as list of commands (just like alias -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 with vi- your shell probably ran in emacs mode, in which case putting bindkey -e into your new zshrc will probably restore most settings. For the rest you can diff the outputs of bindkey -L in the running and a new shell. If you utilized the vicmd mode those settings can be retrieved with bindkey -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.
2
  • Looks like I'm getting to this pretty late, but having previously been in need of this information myself, I can say that you deserve a damn sight more upvotes than it looks like you currently have. You're doing God's work, sir. :)
    – Jon Carter
    Aug 24, 2020 at 20:56
  • I totally agree with you @JonCarter. I've lost my .zshrc and this answer has helped me to restore the majority of the file. Congratulations @Adaephon
    – xserrat
    Nov 20, 2021 at 16:19
0

Another useful command is typeset to retrieve all declared variables from the current session, so you can find for example the list of plugins you had installed: typeset | grep plugins=

0
history | grep ".zshrc"

Look for the commands or output related to your .zshrc file. The contents of the file might have been displayed in your terminal at some point.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 6, 2023 at 17:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .