使用/维护两个单独的 bash_profile 和 vimrc 的聪明方法是什么

iva*_*van 6 vimrc bashrc

我喜欢在终端和 vim 中使用两种不同的设置。一种是在 vim 中使用浅色背景和有点花哨的航空公司状态栏。另一个使用深色背景和更准系统的 vim 外观。我要么出于优柔寡断,要么偶尔在两者之间摇摆不定。

随意在这两种配置之间轻松切换的智能方法是什么?现在我基本上有两个略有不同的.bash_profiles 和.vimrcs。当我想要变暗时,我手动获取暗配置文件,并定义了一个 bash 别名以使用备用 vimrc 启动 vim。我相信有更好的方法,我会对建议感兴趣。

更新:我采纳了设置主题环境变量以在配置文件中引用的极好建议。奇迹般有效。还发现了这个gem(不是 Ruby 意义上的),它让我同时将 iTerm 配置文件切换为深色配置文件。警告:将 bash 函数定义为这样的单行代码会给我一个语法错误,所以我不得不将它分成多行。

it2prof() {
  echo -e "\033]50;SetProfile=$1\a"
}

alias dark="export THEME=dark && it2prof black && . ~/.bash_profile"
alias light="unset THEME && it2prof white && . ~/.bash_profile"
Run Code Online (Sandbox Code Playgroud)

更好的是,事实证明 iTerm2 有一堆可用于动态更改设置的转义码。

另一个更新:iTerm2 文档警告转义序列可能在 tmux 和 screen 中不起作用,实际上它们不起作用。为了让它们工作,你需要告诉多路复用器将转义序列发送到底层终端,而不是试图解释它。它有点毛茸茸,但现在在 tmux、屏幕和正常的 shell 会话中对我有用:

darken() {
  if [ -n "$ITERM_PROFILE" ]; then
    export THEME=dark
    it2prof black
    reload_profile
  fi
}

lighten() {
  if [ -n "$ITERM_PROFILE" ]; then
    unset THEME
    it2prof white
    reload_profile
  fi
}

reload_profile() {
  if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
  fi
}

it2prof() {
  if [[ "$TERM" =~ "screen" ]]; then
    scrn_prof "$1"
  else
    # send escape sequence to change iTerm2 profile
    echo -e "\033]50;SetProfile=$1\007"
  fi
}

scrn_prof() {
  if [ -n "$TMUX" ]; then
    # tell tmux to send escape sequence to underlying terminal
    echo -e "\033Ptmux;\033\033]50;SetProfile=$1\007\033\\"
  else
    # tell gnu screen to send escape sequence to underlying terminal
    echo -e "\033P\033]50;SetProfile=$1\007\033\\"
  fi
}
Run Code Online (Sandbox Code Playgroud)

Gil*_*il' 6

使用环境变量。这样,您可以在 shell 中设置THEME=dark或THEME=light,并且该 shell 启动的所有程序都将使用所需的方案。

在 bash 或任何其他 shell 中:

case $THEME in
  light)
    PS1='\[\e05m\]fancy stuff\[\e0m\]';;
  *)
    PS1='\w\$ ';;
esac
Run Code Online (Sandbox Code Playgroud)

在您的.vimrc:

if $THEME == "light"
  …
else
  …
endif
Run Code Online (Sandbox Code Playgroud)