Skip to main content

Terminal

UNIX Tools (and their alternatives)

Bash/Zsh Syntax

Environment variables

  • PATH • Julia Evans 📖

Configuring Zsh

Configuring kitty

Searching Kitty Terminal Output with Vim

  • use nvim as scrollback pager
  • tl;dr
# kitty.conf

# update nvim -u path to point to your own config (if using a custom one)
scrollback_pager nvim --noplugin -u ~/.config/kitty/scrollback/init.lua -c "silent write! /tmp/kitty_scrollback_buffer | te cat /tmp/kitty_scrollback_buffer - "

# change cmd+f and cmd+o to anything you like
# search scrollback in a pager (mapped to nvim above)
# see: https://sw.kovidgoyal.net/kitty/actions/#action-show_scrollback
map cmd+f show_scrollback
map cmd+o show_last_command_output
  • why use nvim to search command output?
    • if you’re a vim user, you already know how to quickly jump to the beginning/end and up/down through text
    • you also already know how to search and navigate back and forth through your results
    • you also already know how to easily copy text
    • compared to the default less pager or other options like tmux’s copy mode, nvim gives you the ability to actually alter the output by running set modifiable and then deleting any lines you don’t want to see
    • you also have full control over the configuration of your nvim instance to customize the appearance and behavior of your search environment however you like
  • to keep this pager speedy, you can point to a simplified config that loads quickly and removes everything irrelevant if you like
  • I added this to mine to allow deleting lines (to reduce noise) and starting at the bottom by default:
-- Allow deleting lines and start at the bottom
vim.api.nvim_create_autocmd('VimEnter', {
  command = [[
    setlocal modifiable
    normal G
  ]]
})

Filtering kitty terminal output with fzf

  • tl;dr:
    # kitty.conf
    map cmd+f launch --type=overlay --stdin-source=@screen_scrollback /bin/sh -c "/opt/homebrew/bin/fzf --no-sort --no-mouse --exact --tac | kitty +kitten clipboard"
    
    • This command will open an fzf overlay that allows you to fuzzy find individual lines of output in your terminal scrollback
    • It works both for completed and running processes
  • How to set up:
    • If you haven’t already, install kitty and fzf (this workflow only works in kitty)
    • Add the map above to your kitty.conf , replacing cmd+f with your preferred keyboard shortcut
    • Consider increasing the number of output lines kitty includes in its scrollback from 2000 to something larger (e.g. 10000) by adding scrollback_lines 10000 (or whatever number you prefer) to your kitty.conf as well
    • Reload (cmd-option-, on mac) or restart kitty (i.e. just quit and reopen the app) to apply your configuration changes
  • How to use:
    • Run a command that produces multiple lines of output (e.g. top)
    • Press your keymap, either after you’ve stopped the process or while its still running
    • You should see your terminal output open inside an fzf overlay, where each output line represents one fzf result
    • Enter a search pattern (see the fzf guide here) to filter the output to only includes lines that match your fuzzy
    • Press escape to exit fzf
  • creating, resizing and navigating windows and tabs
  • tl;dr:
# Create windows
map ctrl+\ new_window_with_cwd

# Navigate windows (i.e. splits/panes)
map ctrl+h neighboring_window left
map ctrl+l neighboring_window right
# map ctrl+j neighboring_window bottom # FIXME: interferes with menu up/down (e.g. for fzf)
# map ctrl+k neighboring_window top # FIXME: interferes with menu up/down (e.g. for fzf)

# Navigate tabs
map ctrl+] next_tab
map ctrl+[ previous_tab

# toggle zoom current window
map ctrl+m toggle_layout stack
  • explain how layouts work
    • unlike most terminals, most kitty layouts won’t let you decide if you want to split a window vertically or horizontally
    • the new window will position itself automatically based on the active layout
    • (examples)
    • if you want more control over which direction your next split will occur in, you need to activate the splits layout
  • zoom a single window by activating the stacks layout
    • add keymap to toggle the zooming
  • keymaps to quickly move your cursor between windows
    • I prefer the following
    • watch out for conflicts with similar navigation keymaps in running terminal apps (e.g. fzf)
      • how to use ctrl+j/k in both…?
  • Add resize mappings too
  • Add layout swap mappings too?
  • Inbox:
    • add maps for navigating back and forth to previous command prompts
      • terminal shortcut to scroll to the beginning of the output for the last command?
    • how to scroll with the keyboard (no mouse)? - Overview - kitty: https://sw.kovidgoyal.net/kitty/overview/#scrolling
    • shortcuts: add same split creation + navigation ones as in tmux
    • best prefix for custom maps? command-key?
    • define better auto splits and manual split mappings (via the “splits” layout?): https://sw.kovidgoyal.net/kitty/layouts/

Stopping the process running on a port

  • Problem:
    • you try to start a process in your terminal (e.g. start a local dev server) and see an error that the port is already in use
    • you check all your open terminal windows and don’t see a process to stop
  • Identify what the process is like this:
lsof -i -tcp:<port>
  • If that process is safe to stop, use the “PID” value from the output above or isolate that value like this:
lsof -t -i:<port>
  • Then kill that process using its PID like this:
kill -9 <PID>
  • To make it easier to free up a port in the future, you could also add a zsh function like this to your .zshrc (or equivalent terminal config file):
# kill process running on given port
k() { lsof -t -i:$1 | xargs kill -9; }

Switch to bash on Mac to get access to shellcheck?

Creating CLI tools

Make a file executable

Generate any number of test files in a directory

Searching command history

Inbox