shift-select - Emacs shift-select mode for ZSH - select text in the command line using Shift, as in many text editors, browsers and other GUI programs.

shift-select - Emacs shift-select mode for ZSH - select text in the command line using Shift, as in many text editors, browsers and other GUI programs.

Zsh Shift Select Mode

Emacs shift-select mode for Zsh – select text in the command line using Shift as in many text editors, browsers and other GUI programs.

This plugin does not override any existing widgets and binds only shifted keys (see Key Bindings). It creates a new shift-select keymap that is automatically activated when the shift selection is invoked (using any of the defined shifted keys) and deactivated (the current keymap switches back to main) on any key that is not defined in the shift-select keymap. Thanks to this approach, it does not interfere with other plugins (for example, it works with zsh-autosuggestions without any change).

Key bindings for moving the cursor without selection (e.g. Ctrl + arrows, Home etc.) are out of scope of this plugin.

Example of using zsh-shift-select

Key Bindings

List of keys defined in the emacs and shift-select keymap:

  • Shift+Left Arrow – select one character to the left

  • Shift+Right Arrow – select one character to the right

  • Shift+Up Arrow – select one line up

  • Shift+Down Arrow – select one line down

  • Shift+Home or Shift+Ctrl+A – select to the beginning of a line

  • Shift+End or Shift+Ctrl+E – select to the end of a line

  • Shift+Ctrl+Left Arrow – select to the beginning of a word

  • Shift+Ctrl+Right Arrow – select to the end of a word

  • Shift+Ctrl+Home – select to the beginning of an input buffer

  • Shift+Ctrl+End – select to the end of an input buffer

The last four actions use Shift+Option instead of Shift+Ctrl on macOS.

List of keys defined in the shift-select keymap only:

  • Delete or Backspace – delete selected text

If you want to add custom key bindings to manipulate with active selection, define them in the shift-select keymap (bindkey -M shift-select …​).

Tip
You can use default Alt+W or Ctrl+W key bindings to copy (copy-region-as-kill) or cut (backward-kill-word) selected text to the “kill buffer”, and Ctrl+Y to paste (yank) the contents of the “kill buffer”.

Known Issues

Some keys may not work in your terminal by default. For example, kitty uses Shift+Ctrl as the modifier for all kitty’s shortcuts (this can be changed with kitty_mod). To find out if this is the case, run cat (without any arguments) in your terminal and press the key sequence in question. If nothing is printed, it means that your terminal or operating system has intercepted the key sequence.

Installation

Using sheldon

If you use sheldon plugin manager, run the following command:

sheldon add zsh-shift-select --github jirutka/zsh-shift-select

Using zgenom

If you use zgenom (successor of zgen) plugin manager, add zgenom load "jirutka/zsh-shift-select" to your .zshrc.

Using Oh My Zsh

If you use Oh My Zsh framework:

  1. Clone this repository into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins):

    git clone https://github.com/jirutka/zsh-shift-select.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-shift-select
  2. Add the plugin to the list of plugins for Oh My Zsh to load (inside $ZDOTDIR/.zshrc, by default ~/.zshrc):

    plugins=(
        # other plugins...
        zsh-shift-select
    )
  3. Start a new terminal session.

On Alpine Linux

If you use Alpine Linux v3.16+ or Edge, you can install zsh-shift-select via apk and load it using the plugin loader provided in the default Zsh configuration on Alpine (see /etc/zsh/zshrc).

  1. Install zsh-shift-select package (as root).

    apk add zsh-shift-select
  2. Symlink zsh-shift-select plugin to your Zsh plugins directory [1]:

    mkdir -p ~/.local/share/zsh/plugins
    ln -s /usr/share/zsh/plugins/zsh-shift-select ~/.local/share/zsh/plugins/

Manually (Git Clone)

  1. Clone this repository somewhere on your machine. This guide will assume ~/.local/share/zsh/plugins/zsh-shift-select.

    git clone https://github.com/jirutka/zsh-shift-select ~/.local/share/zsh/plugins/zsh-shift-select
  2. Add the following to your $ZDOTDIR/.zshrc (by default ~/.zshrc):

    source ~/.local/share/zsh/plugins/zsh-shift-select/zsh-shift-select.plugin.zsh
  3. Start a new terminal session.

References

License

This project is licensed under MIT License. For the full text of the license, see the LICENSE file.


1. Alternatively, you can add ZSH_LOAD_SYSTEM_PLUGINS=yes to your .zshenv to automatically load all Zsh plugins installed from Alpine packages.

Download Source Code

Download ZIP
Apr 5, 2022