Skip to content

Conversation

@bezhermoso
Copy link
Owner

Summary

Widgets are functions that perform actions in the terminal and command-line buffer. You can define custom ones and bind them to key combinations.

What it achieves

  • Create custom keyboard shortcuts that manipulate the command line
  • Access and modify the command buffer programmatically
  • Build personalized command-line workflows

Zsh Configuration

# 1. Define function
prepend-sudo() {
    if [[ $BUFFER != "sudo "* ]]; then
        BUFFER="sudo $BUFFER"
        zle end-of-line
    fi
}
# 2. Create widget
zle -N prepend-sudo
# 3. Bind hotkey
bindkey '^Xs' prepend-sudo

Key variables: $BUFFER, $LBUFFER, $RBUFFER, $CURSOR

Reference

Additional Applications & Inspiration

@bezhermoso bezhermoso force-pushed the claude/zsh-custom-widgets-hack-UDTka branch 4 times, most recently from f9581cb to 50d364e Compare January 5, 2026 08:03
Widgets are functions that perform actions in the terminal and command-line
buffer. You can define custom ones and bind them to key combinations.

Included custom widgets:
- Clear history but keep command buffer (Ctrl+X l)
- Insert current date (Ctrl+X d)
- Prepend sudo to command (Ctrl+X s)
- Quote current command (Ctrl+X q)
- Clear screen keeping scrollback (Ctrl+X Ctrl+L)
- Copy command to clipboard (Ctrl+X c)

Process:
1. Define a function
2. Use zle -N to create a widget from it
3. Use bindkey to assign a hotkey

Includes documentation of useful widget variables ($BUFFER, $LBUFFER, etc.)
and commands (zle reset-prompt, zle -M, etc.)

Reference: https://www.youtube.com/watch?v=3fVAtaGhUyU (14:04-15:19)
@bezhermoso bezhermoso force-pushed the claude/zsh-custom-widgets-hack-UDTka branch from 50d364e to 9c7de87 Compare January 5, 2026 08:05
@bezhermoso
Copy link
Owner Author

bezhermoso commented Jan 5, 2026

  • Fix conflicts with existing key binds.

@bezhermoso bezhermoso merged commit 1e16e04 into master Jan 5, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants