Skip to content

Instantly share code, notes, and snippets.

@otonii
Last active December 17, 2022 05:58
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otonii/e6bae9fabe0f19daa969f10e9047970d to your computer and use it in GitHub Desktop.
Save otonii/e6bae9fabe0f19daa969f10e9047970d to your computer and use it in GitHub Desktop.

MSYS2 + Zsh + Oh My Zsh + Theme (windows)

Instalação do Zsh + Oh My Zsh no windows sem WSL.

Instalando o MSYS2

choco install msys2 -y

Configurando MSYS2 no Windows Terminal

Criar arquivo de inicialização do MSYS2 wt.cmd

@echo off
set MSYSTEM=MINGW64
set MSYS=winsymlinks:nativestrict
set MSYS2_PATH_TYPE=inherit
set CHERE_INVOKING=1
C:/tools/msys64/usr/bin/bash.exe --login -i

Baixar a fonte Fira Code https://github.com/tonsky/FiraCode

Configurações do Windows Terminal

{
  "defaultProfile": "{847b4d1a-bcdf-4592-be3f-d2376c4aa299}",
  "tabWidthMode": "titleLength",
  // ...
  "profiles": {
    "defaults": {
      "useAcrylic": true,
      "acrylicOpacity": 0.7,
      "cursorShape": "filledBox",
      "colorScheme": "Dracula",
      "fontFace": "Fira Code",
      "startingDirectory": ".",
      "fontSize": 14,
      "padding": "8, 8, 8, 8"
    },
    "list": [
      //...
      // CMDER
      // {
      //   "guid": "{738e577c-5a3d-44a1-b1d7-bbbc9f488323}",
      //   "name": "Cmder",
      //   "commandline": "cmd.exe /k %cmder_root%/vendor/init.bat",
      //   "icon": "%cmder_root%/icons/cmder.ico",
      //   "hidden": false
      // },
      // MSYS2
      {
        "guid": "{847b4d1a-bcdf-4592-be3f-d2376c4aa299}",
        "name": "MSYS",
        "commandline": "cmd.exe /k c:\\tools\\msys64\\wt.cmd",
        "icon": "C:\\tools\\msys64\\msys2.ico",
        "hidden": false,
        "snapOnInput": true
      }
    ]
  },
  "schemes": [
    {
      "name": "Dracula",
      "background": "#272935",
      "black": "#21222C",
      "blue": "#BD93F9",
      "cyan": "#8BE9FD",
      "foreground": "#F8F8F2",
      "green": "#50FA7B",
      "purple": "#FF79C6",
      "red": "#FF5555",
      "white": "#F8F8F2",
      "yellow": "#FFB86C",
      "brightBlack": "#6272A4",
      "brightBlue": "#D6ACFF",
      "brightCyan": "#A4FFFF",
      "brightGreen": "#69FF94",
      "brightPurple": "#FF92DF",
      "brightRed": "#FF6E6E",
      "brightWhite": "#F8F8F2",
      "brightYellow": "#FFFFA5"
    },
    {
      "name": "OneDark",
      "black": "#1e2127",
      "red": "#e06c75",
      "green": "#7BC379",
      "yellow": "#E5C064",
      "blue": "#286AD1",
      "purple": "#986CDD",
      "cyan": "#56b6c2",
      "white": "#abb2bf",
      "brightBlack": "#5c6370",
      "brightRed": "#e06c75",
      "brightGreen": "#7BC379",
      "brightYellow": "#E5C064",
      "brightBlue": "#286AD1",
      "brightPurple": "#986CDD",
      "brightCyan": "#56b6c2",
      "brightWhite": "#ffffff",
      "background": "#1e2127",
      "foreground": "#abb2bf"
    }
  ]
  // ...
}

Atualizando bibliotecas MSYS2

pacman -Syu
pacman -Su

Essential utilities

pacman -S man vim nano
pacman -S openssh rsync make
pacman -S zip unzip
pacman -S mingw64/mingw-w64-x86_64-jq
pacman -S diffutils

Zsh

Instalando o Zsh

pacman -S zsh

Configurando o .bashrc

# Execute o comando
nano ~/.bashrc

# Adicione o código no final do arquivo
# Launch Zsh
if [ -t 1 ]; then
  exec zsh
fi

Reinicie o terminal

Instalando o Oh My Zsh

cd ~
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Instalando o Antigen

cd ~
curl -L git.io/antigen > antigen.zsh

Adicionar a configuração no final do arquivo .zshrc

source /path/to/antigen.zsh

# Load the oh-my-zsh's library.
antigen use oh-my-zsh

# Bundles from the default repo (robbyrussell's oh-my-zsh).
antigen bundle git
antigen bundle heroku
antigen bundle pip
antigen bundle lein
antigen bundle command-not-found

# Syntax highlighting bundle.
# antigen bundle zdharma/fast-syntax-highlighting

# Outros plugins
antigen bundle zsh-users/zsh-autosuggestions
antigen bundle zsh-users/zsh-completions

# Tell Antigen that you're done.
antigen apply

Reinicie o terminal como administrador para que ele possa instalar todas as dependências.

Pronto Zsh Funcionando e configurado

Meu Tema

Peguei um tema padrão e modifiquei ele para ficar parecido com o spaceship. Então vamos lá crie um arquivo otonii.zsh-theme na pasta path/to/.oh-my-zsh/custom/themes.

OTO_THEME_NVM_PROMPT_PREFIX="%B⬡%b "
OTO_THEME_NVM_PROMPT_SUFFIX=""

### Git  master ▾●

OTO_THEME_GIT_PROMPT_PREFIX="%{$fg[white]%}on%{$reset_color%} %{$fg[magenta]%}\uE0A0 "
OTO_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
OTO_THEME_GIT_PROMPT_CLEAN="%{$fg[green]%}✓%{$reset_color%}"
OTO_THEME_GIT_PROMPT_AHEAD="%{$fg[cyan]%}▴%{$reset_color%}"
OTO_THEME_GIT_PROMPT_BEHIND="%{$fg[magenta]%}▾%{$reset_color%}"
OTO_THEME_GIT_PROMPT_STAGED="%{$fg[green]%}●%{$reset_color%}"
OTO_THEME_GIT_PROMPT_UNSTAGED="%{$fg[yellow]%}●%{$reset_color%}"
OTO_THEME_GIT_PROMPT_UNTRACKED="%{$fg[red]%}●%{$reset_color%}"

oto_git_branch () {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || \
  ref=$(git rev-parse --short HEAD 2> /dev/null) || return
  echo "${ref#refs/heads/}"
}

oto_git_status() {
  _STATUS=""

  # check status of files
  _INDEX=$(command git status --porcelain 2> /dev/null)
  if [[ -n "$_INDEX" ]]; then
    if $(echo "$_INDEX" | command grep -q '^[AMRD]. '); then
      _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_STAGED"
    fi
    if $(echo "$_INDEX" | command grep -q '^.[MTD] '); then
      _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_UNSTAGED"
    fi
    if $(echo "$_INDEX" | command grep -q -E '^\?\? '); then
      _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_UNTRACKED"
    fi
    if $(echo "$_INDEX" | command grep -q '^UU '); then
      _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_UNMERGED"
    fi
  else
    _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_CLEAN"
  fi

  # check status of local repository
  _INDEX=$(command git status --porcelain -b 2> /dev/null)
  if $(echo "$_INDEX" | command grep -q '^## .*ahead'); then
    _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_AHEAD"
  fi
  if $(echo "$_INDEX" | command grep -q '^## .*behind'); then
    _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_BEHIND"
  fi
  if $(echo "$_INDEX" | command grep -q '^## .*diverged'); then
    _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_DIVERGED"
  fi

  if $(command git rev-parse --verify refs/stash &> /dev/null); then
    _STATUS="$_STATUS$OTO_THEME_GIT_PROMPT_STASHED"
  fi

  echo $_STATUS
}

oto_git_prompt () {
  local _branch=$(oto_git_branch)
  local _status=$(oto_git_status)
  local _result=""
  if [[ "${_branch}x" != "x" ]]; then
    _result="$OTO_THEME_GIT_PROMPT_PREFIX$_branch"
    if [[ "${_status}x" != "x" ]]; then
      _result="$_result $_status"
    fi
    _result="$_result$OTO_THEME_GIT_PROMPT_SUFFIX"
  fi
  echo $_result
}

oto_path() {

  filename="package.json"
  local _result=""

  if [[ -e $filename ]]; then
    local _name=$(sed -nE 's/^\s*"name": "(.*?)",$/\1/p' package.json) || ''
    local _version=$(sed -nE 's/^\s*"version": "(.*?)",$/\1/p' package.json)
    if [[ "${_name}x" != "x" ]]; then
      _result="%{$fg[blue]%}$_name"
      if [[ "${_version}x" != "x" ]]; then
        _result+="%{$fg[cyan]%} @$_version%{$reset_color%}"
      fi
    fi
  fi

  if [[ -z "$_result" ]]; then
    _result="%{$fg[blue]%}%~"
  fi

  echo "$_result%{$reset_color%}"
}

if [[ $EUID -eq 0 ]]; then
  _USERNAME="%{$fg[red]%}%n"
  _LIBERTY="%{$fg[red]%}#"
else
  _USERNAME="%{$fg[white]%}%n"
  _LIBERTY="%{$fg[green]%}❯"
fi
_USERNAME="$_USERNAME%{$reset_color%}@%m"
_LIBERTY="$_LIBERTY%{$reset_color%}"

setopt prompt_subst
PROMPT='$(oto_path) $(oto_git_prompt) $_LIBERTY '

Depois disso basta configurar a variável ZSH_THEME="otonii" no arquivo ~/.zshrc.

E pronto o seu terminal está totalmente configurado.

Integrando o terminal com o VSCode

{
  //...
  "terminal.integrated.shell.windows": "cmd.exe",
  "terminal.integrated.shellArgs.windows": ["/k c:\\tools\\msys64\\wt.cmd"]
}
@otonii
Copy link
Author

otonii commented May 25, 2020

Eu testei o WSL2 no começo de 2020 por causa do terminal e não gostei muito da performance, demorava pra iniciar e tive problemas pra poder acessar alguns aquivos e programas do windows por ele. Já tinha desistido quando encontrei um post sobre instalar o zsh no msys2 e cá estamos. Devo dar outra chance pro WSL2 em breve quando a versão 20h1 de fato sair (estou usando a insider) e tiver mais conteúdo na internet. Até lá, deixarei salvo essa colinha aqui que já me atendendo.

@otonii
Copy link
Author

otonii commented Jan 22, 2021

Recomendo os usuários do windows a utilizar o oh-my-posh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment