使用自定义 zshrc 启动 zsh

使用自定义 zshrc 启动 zsh

我希望能够使用类似于命令的自定义 rc 文件启动 zsh:bash --rc-file /path/to/file

如果这是不可能的,那么是否可以启动 zsh、运行source /path/to/file,然后留在同一个 zsh 会话中?

注意:该命令zsh --rcs /path/to/file不起作用,至少对我来说不起作用......

编辑:总的来说,我希望能够执行以下操作: ssh到远程服务器“example.com”,运行zshsource我的配置位于/path/to/file,全部用1个命令。这是我一直在努力的地方,特别是因为我不想覆盖远程计算机上的任何配置文件。

答案1

从手册页:

STARTUP/SHUTDOWN FILES
       Commands are first read from /etc/zshenv; this cannot be overridden.  Subsequent  be‐
       haviour is modified by the RCS and GLOBAL_RCS options; the former affects all startup
       files, while the second only affects global startup files (those shown here  with  an
       path starting with a /).  If one of the options is unset at any point, any subsequent
       startup file(s) of the corresponding type will not be read.  It is also possible  for
       a  file  in  $ZDOTDIR  to  re-enable  GLOBAL_RCS.  Both RCS and GLOBAL_RCS are set by
       default.

       Commands are then read from $ZDOTDIR/.zshenv.  If the shell is a  login  shell,  com‐
       mands are read from /etc/zprofile and then $ZDOTDIR/.zprofile.  Then, if the shell is
       interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc.  Finally, if
       the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are read.

       When a login shell exits, the files $ZDOTDIR/.zlogout and then /etc/zlogout are read.
       This happens with either an explicit exit via the exit  or  logout  commands,  or  an
       implicit exit by reading end-of-file from the terminal.  However, if the shell termi‐
       nates due to exec'ing another process, the logout files are not read.  These are also
       affected  by  the  RCS and GLOBAL_RCS options.  Note also that the RCS option affects
       the saving of history files, i.e. if RCS is unset when the shell  exits,  no  history
       file will be saved.

       If  ZDOTDIR  is unset, HOME is used instead.  Files listed above as being in /etc may
       be in another directory, depending on the installation.

       As /etc/zshenv is run for all instances of zsh, it is important that it  be  kept  as
       small  as  possible.  In particular, it is a good idea to put code that does not need
       to be run for every single shell behind a test of the form `if [[  -o  rcs  ]];  then
       ...' so that it will not be executed when zsh is invoked with the `-f' option.

因此您应该能够将环境变量设置ZDOTDIR为新目录,以使 zsh 查找一组不同的点文件。

正如手册页所示,RCSGLOBAL_RCS不是 rc 文件的路径(当您尝试使用它们时),而是您可以启用或禁用的选项。例如,该标志--rcs将启用该RCS选项,导致 zsh 从 rc 文件中读取。您可以使用 zsh 的以下命令行标志来启用或禁用RCSGLOBAL_RCS

  --globalrcs
  --rcs
  -d    equivalent to --no-globalrcs
  -f    equivalent to --no-rcs

回答你的另一个问题:

是否可以启动 zsh,运行“source /path/to/file”,然后保留在同一个 zsh 会话中?

是的,根据上述说明,这非常容易。就跑zsh -d -f然后source /path/to/zshrc

答案2

而使用 ZDOTDIR,您可以告诉zsh解释在您选择的任何目录中调用的文件.zshrc,让它解释您选择的任何文件(不一定称为.zshrc)证明是相当困难的。

shksh仿真中,zsh评估$ENV;所以你可以emulate zsh在你的顶部添加/path/to/file并执行以下操作:

ssh -t host 'zsh -c "ARGV0=sh ENV=/path/to/file exec zsh"'

另一种非常复杂的方法可能是:

ssh -t host 'PS1='\''${${functions[zsh_directory_name]::="
    set +o promptsubst
    unset -f zsh_directory_name
    unset PS1
    . /path/to/file
 "}+}${(D):-}${PS1=%m%# }'\' exec zsh -o promptsubst -f

这个问题值得稍微解释一下。

${foo::=value}是一个变量扩展,实际上 $foo$functions是一个特殊的关联数组,它将函数名称映射到它们的定义。

使用该promptsubst选项,变量 in$PS1会被扩展。因此,在第一次提示时,PS1 中的变量将被扩展。

zsh_directory_name函数是一个特殊的函数,有助于扩展~footo/path/to/something和反向。例如,它%~在提示中使用,这样如果当前目录是,您可以通过映射<=>/opt/myproj/proj/x显示它。参数扩展标志也使用它。因此,如果展开,该函数将被调用。~proj:xzsh_directory_nameproj:x/opt/myproj/proj/xD${(D)somevar}zsh_directory_name

在这里,我们使用${(D):-}, ${:-},即${no_var:-nothing}展开为nothingif$no_var为空,因此${(D):-}在调用 时展开为空zsh_directory_namezsh_directory_name之前被定义为:

zsh_directory_name() {
  set +o promptsubst
  unset -f zsh_directory_name
  unset PS1; . /path/to/file
}

也就是说,在第一次 PS1 扩展时(在第一个提示时),${(D):-}将导致promptsubst选项被取消设置(以取消-o promptsubst)、zsh_directory_name()未定义(因为我们只想运行它一次)$PS1、取消设置并/path/to/file获取来源。

${PS1=%m%# }扩展(并分配$PS1)为 ,%m%#除非 PS1 已被定义(例如在/path/to/fileunset),并且%m%#恰好是 的默认值PS1

相关内容