Skip to main content

Terminal settings

zsh

# dont open zsh before adding `.zshrc`
sudo pacman -S zsh zsh-theme-powerlevel10k zsh-syntax-highlighting zsh-history-substring-search zsh-autosuggestions ripgrep bat exa skim

You have to add a font so the icons will work. I like the JetBrains Mono Nerd Font from Nerd Font. And install it from the fonts manager.

Then add the following to nano .zshrc

## Options section
setopt correct                                                  # Auto correct mistakes
setopt extendedglob                                             # Extended globbing. Allows using regular expressions with *
setopt nocaseglob                                               # Case insensitive globbing
setopt rcexpandparam                                            # Array expension with parameters
setopt nocheckjobs                                              # Don't warn about running processes when exiting
setopt numericglobsort                                          # Sort filenames numerically when it makes sense
setopt nobeep                                                   # No beep
setopt appendhistory                                            # Immediately append history instead of overwriting
setopt histignorealldups                                        # If a new command is a duplicate, remove the older one
setopt autocd                                                   # if only directory path is entered, cd there.

zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'       # Case insensitive tab completion
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"         # Colored completion (different colors for dirs/files/etc)
zstyle ':completion:*' rehash true                              # automatically find new executables in path
# Speed up completions
zstyle ':completion:*' accept-exact '*(N)'
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache
HISTFILE=~/.zhistory
HISTSIZE=10000
SAVEHIST=10000

export EDITOR=/usr/bin/vim
export VISUAL=/usr/bin/vim

WORDCHARS=${WORDCHARS//\/[&.;]}                                 # Don't consider certain characters part of the word

## Keybindings section
bindkey -e
bindkey '^[[7~' beginning-of-line                               # Home key
bindkey '^[[H' beginning-of-line                                # Home key
if [[ "${terminfo[khome]}" != "" ]]; then
  bindkey "${terminfo[khome]}" beginning-of-line                # [Home] - Go to beginning of line
fi
bindkey '^[[8~' end-of-line                                     # End key
bindkey '^[[F' end-of-line                                     # End key
if [[ "${terminfo[kend]}" != "" ]]; then
  bindkey "${terminfo[kend]}" end-of-line                       # [End] - Go to end of line
fi
bindkey '^[[2~' overwrite-mode                                  # Insert key
bindkey '^[[3~' delete-char                                     # Delete key
bindkey '^[[C'  forward-char                                    # Right key
bindkey '^[[D'  backward-char                                   # Left key
bindkey '^[[5~' history-beginning-search-backward               # Page up key
bindkey '^[[6~' history-beginning-search-forward                # Page down key

# Navigate words with ctrl+arrow keys
bindkey '^[Oc' forward-word                                     #
bindkey '^[Od' backward-word                                    #
bindkey '^[[1;5D' backward-word                                 #
bindkey '^[[1;5C' forward-word                                  #
bindkey '^H' backward-kill-word                                 # delete previous word with ctrl+backspace
bindkey '^[[Z' undo                                             # Shift+tab undo last action

## Alias section
alias cp="cp -i"                                                # Confirm before overwriting something
alias rm="rm -l"                                                #
#alias df='df -h'                                               # Human-readable sizes
#alias free='free -m'                                           # Show sizes in MB
#alias gitu='git add . && git commit && git push'
alias cs="cd && clear"                                          # Clear screen and reset to cd

# Theming section
autoload -U compinit colors zcalc
compinit -d
colors

# Color man pages
export LESS_TERMCAP_mb=$'\E[01;32m'
export LESS_TERMCAP_md=$'\E[01;32m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;47;34m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;36m'
export LESS=-r


## Plugins section

# use powerlevel
source /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme

# Use syntax highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

# Use history substring search
source /usr/share/zsh/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh

# bind UP and DOWN arrow keys to history substring search
zmodload zsh/terminfo
bindkey "$terminfo[kcuu1]" history-substring-search-up
bindkey "$terminfo[kcud1]" history-substring-search-down
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down


# Use autosuggestion
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'

# dotnet hot reload :: https://github.com/dotnet/aspnetcore/issues/38798#issuecomment-1006827825
export DOTNET_WATCH_RESTART_ON_RUDE_EDIT=1

# Search and kill. For when a program is unresponsive.
alias fk="search_and_kill"
search_and_kill() {
  local pid=$(ps -ef | grep "$1" | awk '{print $2}' | head -1)
  if [ -n "$pid" ]; then
    local process_name=$(ps -p "$pid" -o comm=)
    echo -n "Do you want to kill the process \"$process_name\" with PID $pid? (Y/n) "
    read confirmation
    if [[ $confirmation =~ ^[Yy]$ || -z $confirmation ]]; then
      kill -9 "$pid"
      echo "Process \"$process_name\" with PID $pid killed."
    else
      echo "Process not killed."
    fi
  else
    echo "Process not found."
  fi
}

# sf meaning search inside file
alias sf="sk --ansi -i -c 'rg --color=always --line-number "{}"'"
# fs meaning search for file
alias fs="rg --files | sk --preview=\"bat {} --color=always\""
# search for file and open in nano
fse() {
  local selected_file
  selected_file=$(rg --files | sk --preview="bat {} --color=always")
  if [[ -n "$selected_file" ]]; then
    nano "$selected_file"  # Open the selected file in nano
  fi
}
alias fse=fse

ripgrep a better grip.

sudo pacman -S ripgrep
echo "alias grip=\"rg\"" >> .zshrc

bat a better cat clone with syntax highlighting and Git integration

sudo pacman -S bat
echo "alias cat=\"bat\"" >> .zshrc

exa a modern replacement for ls

sudo pacman -S exa
echo "alias ls=\"exa\"" >> .zshrc
echo "alias ll=\"exa -l\"" >> .zshrc

skim a good Fuzzy Finder

sudo pacman -S skim
# this is using ripgrep

# sf meaning search for file
echo "alias sf=\"sk --ansi -i -c 'rg --color=always --line-number \"{}\"'\"" >> .zshrc
# fs meaning search inside file
echo "alias fs=\"rg --files | sk --preview=\\\"bat {} --color=always\\\"\"" >> .zshrc