Zsh for Humans

repository·master·Indexed 25 days ago

https://github.com/romkatv/zsh4humans

A turnkey, high-performance Zsh configuration that integrates Powerlevel10k, fzf, and zsh-autosuggestions. It features an interactive installer, SSH teleportation for remote environments, integrated tmux support, and advanced command history search.

Tokens
5.1K
Snippets
22
Records
31
Agent score
31%

What's inside zsh4humans

  1. Navigate directory history

    master

    Zsh for Humans maintains a persistent dirstack for directory navigation.

    Key Bindings

    • cd -<TAB>: View directory history via completion.
    • Alt+Left / Shift+Left (macOS): Go back in dirstack (like a web browser back button).
    • Alt+Right / Shift+Right (macOS): Go forward in dirstack.
    • Alt+Up (<kbd>Shift+Up</kbd> on macOS): Go to the parent directory.
    • Alt+Down (<kbd>Shift+Down</kbd> on macOS): Choose a subdirectory.
    • Alt+R: Invoke z4h-fzf-dir-history (fzf-based directory history search, similar to autojump).

    Customizing FZF Bindings

    You can rebind these widgets or change how Tab behaves in fzf-based widgets using zstyle.

    Example: Rebinding Alt+Down to the fzf directory history widget and setting Tab to repeat:

    zstyle ':z4h:fzf-dir-history' fzf-bindings tab:repeat
    zstyle ':z4h:cd-down'         fzf-bindings tab:repeat
    
    z4h bindkey z4h-fzf-dir-history Alt+Down
  2. Enable SSH teleportation

    master

    Zsh for Humans can automatically teleport your local shell environment to a remote host when connecting via SSH. This allows you to use your configuration on remote machines without requiring git, zsh, or sudo on the remote host.

    To enable and configure this, search for ssh in your ~/.zshrc file.

  3. Install Zsh for Humans

    master

    You can install Zsh for Humans using a single command in bash, zsh, or sh. The installer requires curl or wget. It will back up your existing Zsh startup files, create new ones, install necessary components, and configure your shell as a login shell. The process is interactive and asks for confirmation at each step.

    if command -v curl >/dev/null 2>&1; then
      sh -c "$(curl -fsSL https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)"
    else
      sh -c "$(wget -O- https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)"
    fi
  4. Complete commands with Tab and fzf

    master

    When you press <kbd>Tab</kbd>, suggestions are provided by Zsh completion functions and zsh-completions.

    • Ambiguous completions: If multiple options exist, fzf starts automatically to allow interactive selection.
    • Selection: Use <kbd>Enter</kbd> to accept a selection.
    • Multi-select: Use <kbd>Ctrl+Space</kbd> to select multiple items, or <kbd>Ctrl+A</kbd> to select all of them.
  5. Use custom hostnames in SSH prompt and terminal title

    master

    When connected via SSH, the prompt and terminal title often display the hostname reported by the remote machine. To use the hostname you passed to the ssh command instead, use the expression ${${${Z4H_SSH##*:}//\%/%%}:-%m} in your configuration.

    Configure Terminal Title

    zstyle ':z4h:term-title:ssh' preexec '%n@'${${${Z4H_SSH##*:}//\%/%%}:-%m}': ${1//\%/%%}'
    zstyle ':z4h:term-title:ssh' precmd  '%n@'${${${Z4H_SSH##*:}//\%/%%}:-%m}': %~'

    Configure Prompt (Powerlevel9k)

    Add this to your ~/.p10k.zsh file:

    typeset -g POWERLEVEL9K_CONTEXT_TEMPLATE=%n@${${${Z4H_SSH##*:}//\%/%%}:-%m}
  6. Move prompt to the bottom

    master

    To keep the prompt at a consistent location (the bottom of the screen) when Zsh starts or when pressing Ctrl+L, use the prompt-at-bottom option.

    Note: This feature requires that start-tmux is not set to no.

    If you prefer using the clear command instead of Ctrl+L, you can alias clear to the Zsh for Humans soft-clear function.

    # Move prompt to the bottom when zsh starts and on Ctrl+L.
    zstyle ':z4h:' prompt-at-bottom 'yes'
    
    # Alias clear to the soft-bottom clear function
    alias clear=z4h-clear-screen-soft-bottom
  7. Uninstall Zsh for Humans

    master

    To completely remove Zsh for Humans from your system, follow these steps:

    1. Remove configuration files: Delete or replace your ~/.zshenv and ~/.zshrc files. If you allowed the installer to back up your existing files during installation, they can be found in ~/zsh-backup.
    2. Restart your terminal: You must restart the terminal application itself; simply restarting the zsh shell is insufficient.
    3. Clear the cache: Delete the Zsh for Humans cache directory to ensure no residual data remains.
    rm -rf -- "${XDG_CACHE_HOME:-$HOME/.cache}/zsh4humans/v5"
  8. Enable and configure SSH teleportation

    master

    SSH teleportation allows your local Zsh for Humans environment to be transferred to a remote host when you connect via SSH. By default, teleportation is disabled for all hosts. You can enable it using a blacklist or whitelist approach via zstyle.

    Enable/Disable via Blacklist

    To enable teleportation by default but disable it for specific hosts:

    # Enable SSH teleportation by default.
    zstyle ':z4h:ssh:*'                   enable yes
    
    # Disable SSH teleportation for specific hosts.
    zstyle ':z4h:ssh:example-hostname1'   enable no
    zstyle ':z4h:ssh:*.example-hostname2' enable no

    Enable/Disable via Whitelist

    To disable teleportation by default and only enable it for specific hosts:

    # Disable SSH teleportation by default.
    zstyle ':z4h:ssh:*'                   enable no
    
    # Enable SSH teleportation for specific hosts.
    zstyle ':z4h:ssh:example-hostname1'   enable yes
    zstyle ':z4h:ssh:*.example-hostname2' enable yes
  9. Configure Prompt rendering and behavior

    master

    Use p10k configure to set up your prompt. For productivity, the Lean style with Few icons is recommended.

    Additional prompt customizations:

    • SSH Transient Prompt: To ensure transient prompts work correctly when closing SSH connections, bind z4h-eof to Ctrl+D and set ignore_eof.
    • Smooth Rendering: Use POSTEDIT to handle empty lines before or after the prompt for smoother rendering during command entry.
    • Secondary Prompt (PS2): You can bind Enter to z4h-accept-line to insert a newline instead of showing the secondary prompt when a command is incomplete.
  10. Enable Shell Integration (OSC 133)

    master

    Enabling term-shell-integration allows the shell to mark up output with semantic information using OSC 133 escape codes. This is supported by terminals like iTerm2 and kitty. It also helps prevent rendering issues when resizing the terminal window (provided integrated tmux is enabled).

    # Mark up shell's output with semantic information.
    zstyle ':z4h:' term-shell-integration 'yes'
  11. Accept command autosuggestions

    master

    Zsh for Humans uses zsh-autosuggestions to provide command completions based on your history.

    • Partial acceptance: Any key binding that moves the cursor can accept the suggested word (e.g., moving the cursor one word to the right accepts that word).
    • Full acceptance: Press <kbd>Alt+M</kbd> or <kbd>Option+M</kbd> to accept the entire autosuggestion without moving the cursor.
  12. Try Zsh for Humans in Docker

    master

    You can test the environment in a disposable Docker container. Once you exit the shell, the container is deleted.

    Alpine Linux:

    docker run -e TERM -e COLORTERM -e LC_ALL=C.UTF-8 -w /root -it --detach-keys="ctrl-^,ctrl@" --rm alpine sh -uec '
      apk add zsh curl tmux
      sh -c "$(curl -fsSL https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)"'

    Ubuntu:

    docker run -e TERM -e COLORTERM -w /root -it --detach-keys="ctrl-^,ctrl@" --rm ubuntu sh -uec '
      apt-get update
      apt-get install -y zsh curl tmux
      sh -c "$(curl -fsSL https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)"'