logo

Shell - Tips

Last Updated: 2023-12-06

iTerm2

  • Go to iTerm2 Preferences.
  • Show Additional Info - Preferences -> Profiles -> Session -> Status bar enabled -> Configure Status Bar
  • Adjust Additional Info Location - Preferences -> Apperance -> Status bar location.
  • Adjust Blinking Cursor - Preferences -> Profiles -> Text -> Blinking cursor. You can also select Cursor type between Underline / Vertical bar / Box.
  • Choose a Font - After Powerlevel10k installation, Preferences -> Profiles -> Text, click Change under Font and select MesloLGS NF family.
  • Choose a color scheme - Preferences -> Profiles -> Colors -> Color Presets. For customization, please go to iTerm2-color, choose a color scheme you prefer, click the name and right click to save the file as .itermcolors (you may need to delete .txt). Then, you can import at Color Presets.

Zsh

Install Oh My Zsh

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

Powerlevel10k

$ git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k

modify ~/.zshrc

ZSH_THEME="powerlevel10k/powerlevel10k"

Plugins

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

Add the following to ~/.zshrc

plugins=(
    # other plugins...
    zsh-autosuggestions
    zsh-syntax-highlighting
)

Essential Command line Tools

  • bash scripting: for hacky-small tools as well as command line use learn how to use && and || -- much more concise than if/then/else/fi
  • find + xargs
  • sed, grep (-A,-B,-C,-v), awk for extracting columns
  • use && to concat commands instead of ; so right hand command will only run if left hand command succeeds
  • Query JSON: https://stedolan.github.io/jq/download/
  • Query YAML: https://github.com/mikefarah/yq
  • htop: A better top
  • ranger: A better file browser
  • Copy files: pv (pipe view)
  • fundamental eight: Learn grep, sed, cut, sort, uniq, head, tail, and xargs. These eight tools make up the commands you'll be running most often on the command line when doing text processing.

Use Vim keystrokes on the command line

"vi editing mode" (the default is "emacs mode")

set -o vi

add this to ~/.bashrc

# Enable Vi key bindings
set -o vi

Tell tools to save your history

echo HISTSIZE=130000 HISTFILESIZE=-1 >> ~/.bashrc
echo set -g history-limit 30000 >> ~/.tmux.conf

Avoid unnecessary prompts (avoid slow-down and risk of wrong answer)

Use --backup options to cp, ln, mv:

alias cp='cp --backup=numbered'
alias ln='ln --backup=numbered'
alias mv='mv -f --backup=numbered'

This option is only supported by GNU coreutils, not by the BSD versions that come with OS X. It’s possible to install GNU coreutils, e.g. with brew install coreutils. To override the builtins, follow the instructions printed by brew info coreutils.

Use the env prefix to avoid the alias and instead invoke the bare program.

Use incremental search (^R, aka Control-R)

  • in your editor
  • when searching shell history
  • when searching through contents of scrolled screen log
  • in gdb
  • in any other readline-based tool (they all honor ~/.inputrc)

If you use vi/vim, run this and restart any existing bash shell:

echo set editing-mode vi >> ~/.inputrc

If you use zsh in vi mode, add this to ensure ^R still does what we expect:

echo 'bindkey "^R" history-incremental-search-backward' >> ~/.zshrc

Some custom key bindings

~/.inputrc is the config file for readline, which is used in many interactive programs such as bash and gdb. The following key bindings will make your life easier. Remember to restart bash after modifying ~/.inputrc.

Cycle through commands to a partial match

Add the following two lines to your ~/.inputrc (note all the quotation marks, don't echo it!).

echo '"\e[A": history-search-backward' >> ~/.inputrc
echo '"\e[B": history-search-forward' >> ~/.inputrc

Type in a few chars of a command in history and use arrow (up/down) keys to cycle through all commands that start with the typed in chars.

Delete word for bash command line (^W, aka Control-W)

echo '"\C-w": backward-delete-word' >> ~/.inputrc

Setting vim as your default

Many command-line utilities check the $EDITOR and $VISUAL environment variables to determine how to open files for editing or display.

export EDITOR=vim
export VISUAL=vim

to your ~/.profile or ~/.bashrc.

To use vim for git, add

[core]
  editor=vim

to your ~/.gitconfig.