Thi Notes
AboutNotesBlogTopicsToolsReading
About|Sketches |Cooking |Cafe icon Support Thi

Terminal + ZSH

Terminal + ZSH

Anh-Thi Dinh
Skills
Linux
Data Engineering
Tools
A collection of console terminals in serveral operation systems.

No need to remember commands using AI

I’m using Claude Code, if you use another Coding CLI service, modify the codes. Insert below codes in .bashrc or .zshrc and then source ~/.zshrc:
1claude_execute() {
2  emulate -L zsh
3  setopt NO_GLOB
4  local query="$*"
5  local prompt="You are a command line expert. The user wants to run a command but they don't know how. Here is what they asked: ${query}. Return ONLY the exact shell command needed. Do not prepend with an explanation, no markdown, no code blocks - just return the raw command you think will solve their query."
6  local cmd
7  # use Claude Code
8  cmd=$(claude --dangerously-skip-permissions --disallowedTools "Bash(*)" --model default -p "$prompt" --output-format text | tr -d '\000-\037' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
9  if [[ -z "$cmd" ]]; then
10    echo "claude_execute: No command found"
11    return 1
12  fi
13  echo -e "$ \033[0;36m$cmd\033[0m"
14  eval "$cmd"
15}
16alias ask="noglob claude_execute"
1# Usage
2ask "List all conda env in this computer"

Useful shortcuts with texts in the terminal

Only for macOS (Windows/Linux isn’t checked yet)
1Ctrl+A # go to the beginning of line
2Ctrl+E # go to the end of line
3Ctrl+R # search prompt history
4Option+Left # back one word
5Option+Right # forward one word
6Ctrl+U # remove the entire line containing the cursor
7Ctrl+K # delete from cursor to end of line
8Crtl+W # delete a word backward (left of cursor)
9Alt+D # delete a word forward (right of cursor)

Terminals

Windows

Install cmder (drop-down by Ctr + \) and I use this personal setting files.
👉 Note: WSL on Windows

Linux

  • Download guake terminal
  • Install Zsh, check the previous section.

MacOS

👉 Using iTerms2. Download its settings.

Remember the last command lines for each project

Zsh

(MacOS) Install Homebrew first.
👉 Note: WSL on Windows

Install Zsh + oh-my-zsh

On MacOS, Zsh is already installed and default.
1# check is installed
2zsh --version
1# install (linux)
2sudo apt install zsh
3
4# install (macos - integrated)
1# make zsh default bash
2chsh -s $(which zsh) # log out & log in
1# check
2echo $SHELL # /bin/zsh  or similar
Install oh-my-zsh.

Plugins

Install below plugins (using oh-my-zsh way) first:
  • zsh-autosuggestions
  • zsh-syntax-highlighting
After installing plugins, put them in ~/.zshrc
1plugins=(git docker docker-compose zsh-syntax-highlighting dnf npm)
2# for me
3# plugins=(git docker docker-compose npm ruby python emoji)

spaceship-prompt theme

Install using oh-my-zsh way: check here:
Set ZSH_THEME="spaceship" in your .zshrc.

Fonts

Install font Source Code Pro:
  1. Install wget and fontconfig first using brew
    1. 1brew install wget
      2brew install fontconfig
  1. Follow instruction here (step 1-4). If some folders don't exist, create them!
  1. Install Source Code Pro for Powerline and then
    1. 1fc-cache -f -v
Powerline font,
1sudo apt-get install fonts-powerline
In terminal, choose the corresponding installed fonts.
Add alias to ~/.zshrc (search "alias" to find the place to put).

Display execution time below to command

The following tip will display the execution time of a command. This can be useful, especially when you execute the same command multiple times and need to know when the last command was executed.
1# Push below in .zshrc
2preexec () {
3  local TIME=`date +"[%H:%M:%S] "`
4  local zero='%([BSUbfksu]|([FK]|){*})'
5  local PROMPTLEN=${#${(S%%)PROMPT//$~zero/}}
6  echo "\033[1A\033[$(($(echo -n $1 | wc -m)+$PROMPTLEN))C $fg[blue]${TIME}$reset_color"
7}

Other plugins

  1. fzf -- search in terminal. (Use: fzf)
 
In this post
◆No need to remember commands using AI◆Useful shortcuts with texts in the terminal◆Terminals○Windows○Linux○MacOS◆Remember the last command lines for each project◆Zsh○Install Zsh + oh-my-zsh○Plugins○spaceship-prompt theme○Fonts○Display execution time below to command◆Other plugins