优秀的编程知识分享平台

网站首页 > 技术文章 正文

如何在你的在线IDE中配置zsh作为默认shell

nanyue 2024-07-22 14:01:56 技术文章 10 ℃

趁热记录下,给未来的自己

0. 前言

我们在做在线IDE的时候,会使用开源的 vscode 和 jupyterlab 这两种 IDE。这两个 IDE 都自带一个 terminal,vscode 默认的 shell(ubuntu环境下)是 bash,jupyterlab terminal 自带的 shell 是 sh。但是这两种 shell 其实都不太好用,尤其是 sh,属于古老的产物。于是我们考虑将 zsh 配合 oh-my-zsh 作为 IDE terminal 的默认 shell。

具体的需求就是:通过在线 IDE 打开一个 terminal 的时候,默认的 shell 是 zsh。

为了实现这个需求,我们需要:

  • 先新建一个基础镜像安装 zsh 和 oh-my-zsh。
  • 然后基于这个镜像,分别安装 vscode 和 jupyterlab,
  • 最后分别配置对应的 zsh 环境。
  1. Dockerfile构建zsh

具体 vscode 和 jupyterlab 在Dockerfile中的构建不再赘述,这里主要聚焦 zsh 的安装。

在dockerfile中安装zsh和oh-my-zsh:

RUN apt-get install -y zsh
RUN git clone https://github.com/robbyrussell/oh-my-zsh.git /opt/.oh-my-zsh
RUN echo $FIND_STR0 >> /opt/.oh-my-zsh/templates/zshrc.zsh-template \
    && echo $FIND_STR1 >> /opt/.oh-my-zsh/templates/zshrc.zsh-template \
    && sed -i '1i\ZSH_DISABLE_COMPFIX=true' /opt/.oh-my-zsh/templates/zshrc.zsh-template # 
    
ENTRYPOINT ["/start.sh"]

说明

  • ZSH_DISABLE_COMPFIX=true: 被添加到 zshrc.zsh-template 是为了避免每次打开terminal的时候,出现:zsh compinit: insecure directories and files, run compaudit for list.
  • zshrc.zsh-template: 会在start.sh中被重命名为.zshrc,拷贝到用户家目录下。
  • start.sh: 脚本和具体使用的IDE有关系,会在下面的章节具体展开。
  1. 在线IDE配置

2.1 VS Code

start.sh:

#!/bin/zsh
/opt/code-server/bin/code-server /home/user --disable-telemetry --auth none --bind-addr 0.0.0.0:8080 --cert false &

if [ ! -f "~/.zshrc" ];then
    sudo cp -r /opt/.oh-my-zsh ~ 
    sudo chown -R user:aide ~/.oh-my-zsh
    cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
fi

if [ ! -f "~/.bashrc" ];then
    touch ~/.bashrc
fi

if [ `grep -c "zsh" ~/.bashrc` -eq '0' ];then
    echo "zsh" >> ~/.bashrc # 这一步是为了保证启动terminal时,即使默认shell是bash,也可以自动切换到zsh
fi
tail -f /dev/null # 这一步是为了防止start.sh启动完成后,容器退出

2.2 JupyterLab

Jupyterlab设置默认shell和vscode设置的方式有一定的区别。

  1. 首先需要通过命令 jupyter notebook --generate-config 在用户的家目录下生成 .jupyter/jupyter_notebook_config.py 配置文件。
  2. 然后需要添加配置 c.NotebookApp.terminado_settings = {'shell_command': ['/bin/zsh']} 到~/.jupyter/jupyter_notebook_config.py 里。
  3. 最后再启动 jupyterlab 应用。

start.sh

#!/bin/zsh
JUPYTERLAB_SHELL_COMMAND="c.NotebookApp.terminado_settings = {'shell_command': ['/bin/zsh']}"
JUPYTERLAB_CONFIG="/home/user/.jupyter/jupyter_notebook_config.py"
echo "init jupyterlab teminal to use zsh."
if [ ! -f "$JUPYTERLAB_CONFIG" ];then
    echo "There is no jupyter_notebook_config.py, create one."
    jupyter notebook --generate-config
    echo "jupyter_notebook_config.py is created successfully."
    echo $JUPYTERLAB_SHELL_COMMAND >> $JUPYTERLAB_CONFIG
    echo "set shell_command as /bin/zsh done."
else
    echo "jupyter_notebook_config.py is already existed. check shell_command = /bin/zsh or not."
    if [ `grep -c $JUPYTERLAB_SHELL_COMMAND $JUPYTERLAB_CONFIG` -eq '0' ];then
        echo "shell_command is not set as /bin/zsh"
        echo $JUPYTERLAB_SHELL_COMMAND >> $JUPYTERLAB_CONFIG
        echo "set shell_command as /bin/zsh done."
    fi
fi
jupyter lab --ip="0.0.0.0" --port 8080 --notebook-dir=/home/user --no-browser --ServerApp.base_url=$JUPYTERLAB_BASE_URL --ServerApp.token="" &

if [ ! -f "~/.zshrc" ];then
    sudo cp -r /opt/.oh-my-zsh ~ 
    sudo chown -R user:aide ~/.oh-my-zsh
    cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
fi

if [ ! -f "~/.bashrc" ];then
    touch ~/.bashrc
fi

if [ `grep -c "zsh" ~/.bashrc` -eq '0' ];then
    echo "zsh" >> ~/.bashrc
fi

tail -f /dev/null
最近发表
标签列表