Skip to content

Instantly share code, notes, and snippets.

@tanyuan
Last active January 23, 2024 17:41
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanyuan/a1a3c00b9c231c32c3613d4bbefa6652 to your computer and use it in GitHub Desktop.
Save tanyuan/a1a3c00b9c231c32c3613d4bbefa6652 to your computer and use it in GitHub Desktop.
📺 iTerm2 tips & tricks

iTerm2 (The awesome terminal for Mac)

Look and feel

  • Window theme: Preferences > Appearance Tab > Theme > Light/Dark
  • Show folder icon on window title: Preferences > Appearance Tab > Window: Show proxy icon in window title bar
  • Background transparancy and blur: Preferences > Profiles Tab > Window Tab > Transparency (a third) & Blur (Checked, full)
  • Color theme: Preferences > Profiles Tab > Colors Tab > Color Presets...

Window and tab title

Add the following to ~/.bashrc.

Change tab and window title to current directory name:

# iTerm2 tab and window title
if [ $ITERM_SESSION_ID ]; then
  # Set tab title to current directory
  # the $PROMPT_COMMAND environment variable is executed every time a command is run
  export PROMPT_COMMAND='echo -ne "\033]1;${PWD##*/}\007""; ':"$PROMPT_COMMAND";
fi

Or change tab title to current directory name, but window title to full path:

# iTerm2 tab and window title
if [ $ITERM_SESSION_ID ]; then
  # Set tab title to current directory and set window title to full path
  # the $PROMPT_COMMAND environment variable is executed every time a command is run
  export PROMPT_COMMAND='echo -ne "\033]1;${PWD##*/}\007" && echo -ne "\033]2;${PWD}\007"; ':"$PROMPT_COMMAND";
fi

For SSH, change window title to username@host:

export PROMPT_COMMAND="echo -ne '\033]0;${USER}@${HOSTNAME}\007';$PROMPT_COMMAND"

Vim

Add the following to ~/.vimrc.

Set tab title to buffer name:

if $TERM_PROGRAM =~ "iTerm"
  " Set the title of the Terminal to the currently open file
  function! SetTerminalTitle()
      let titleString = expand('%:t')
      if len(titleString) > 0
          let &titlestring = expand('%:t')
          " this is the format iTerm2 expects when setting the window title
          let args = "\033]1;".&titlestring."\007"
          let cmd = 'silent !echo -e "'.args.'"'
          execute cmd
          redraw!
      endif
  endfunction
autocmd BufEnter * call SetTerminalTitle()
endif

Change cursor shape according to mode:

let &t_SI = "\<Esc>]50;CursorShape=1\x7" " Vertical bar in insert mode
let &t_EI = "\<Esc>]50;CursorShape=0\x7" " Block in normal mode
let &t_SR = "\<esc>]50;CursorShape=2\x7" " Underline in replace mode

Magical usage

  • Set option key as meta (alt): Preferences > Profile Tab > Keys Tab > Left ⌥ Key & Right ⌥ Key to Esc+
  • Split panes (vertical or horizontal) shortcut: Preferences > Keys Tab > To switch split panes > ⌥ Number

Shortcuts

  • ⌘T: Open new tab.
  • ⌘D: Open new vertical split.
  • ⇧⌘D: Open new horizontal split.
  • ⌘ Number: Switch to window.
  • ⌥ Number: Switch to split pane.
  • ⌘C ⌘V: Copy and paste commands.
  • Force Touch on file names: trigger QuickLook 😎

tmux integration

Use tmux in iTerm2 interface and shortcuts, useful for SSH 😍

tmux -CC

Shell integration & utilities

Need additional install: iTerm2 > Install Shell Integration

Show image:

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