zsh-shift-select
zsh-shift-select copied to clipboard
Select text in Zsh command line using Shift, as in many text editors and GUI programs
= Zsh Shift Select Mode :proj-name: zsh-shift-select :gh-name: jirutka/{proj-name} // Enable kbd:[] macro :experimental:
Emacs https://www.gnu.org/software/emacs/manual/html_node/emacs/Shift-Selection.html[shift-select mode] for Zsh – select text in the command line using kbd:[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 https://github.com/zsh-users/zsh-autosuggestions[zsh-autosuggestions] without any change).
Key bindings for moving the cursor without selection (e.g. kbd:[Ctrl] + arrows, kbd:[Home] etc.) are out of scope of this plugin.footnote:[You can take inspiration from https://github.com/jirutka/alpine-zsh-config/blob/master/zshrc.d/50-key-bindings.zsh[key bindings in alpine-zsh-config].] So is synchronization with the system clipboard.footnote:[For an example, see https://github.com/jirutka/alpine-zsh-config/blob/master/zshrc.d/70-clipboard.zsh[clipboard integration in alpine-zsh-config].]
image::media/demo.gif[Example of using zsh-shift-select]
== Key Bindings
=== List of keys defined in the emacs and shift-select keymap:
- kbd:[Shift + Left Arrow] – select one character to the left
- kbd:[Shift + Right Arrow] – select one character to the right
- kbd:[Shift + Up Arrow] – select one line up
- kbd:[Shift + Down Arrow] – select one line down
- kbd:[Shift + Home] or kbd:[Shift + Ctrl + A] – select to the beginning of a line
- kbd:[Shift + End] or kbd:[Shift + Ctrl + E] – select to the end of a line
- kbd:[Shift + Ctrl + Left Arrow] ^<
>^ – select to the beginning of a word - kbd:[Shift + Ctrl + Right Arrow] ^<
>^ – select to the end of a word - kbd:[Shift + Ctrl + Home] ^<
>^ – select to the beginning of an input buffer - kbd:[Shift + Ctrl + End] ^<
>^ – select to the end of an input buffer
[[macos, ]] ^^ The last four actions use kbd:[Shift + Option] instead of kbd:[Shift + Ctrl] on macOS.
=== List of keys defined in the shift-select keymap only:
- kbd:[Delete] or kbd:[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 kbd:[Alt + W] or kbd:[Ctrl + W] key bindings to copy (copy-region-as-kill) or cut (backward-kill-word) selected text to the “kill buffer”, and kbd:[Ctrl + Y] to paste (yank) the contents of the “kill buffer”.
== Terminals Compatibility
Some keys may not work in your terminal by default.
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.
This plugin has been tested in the following terminals:
|=== | Terminal | State
| Alacritty | ✔️ works out-of-box | Kitty | ⚙️ kbd:[Shift + Ctrl] doesn’t work out-of-box, apply <<Kitty, fix>> | WezTerm | ⚙️ kbd:[Shift + Ctrl + arrows] doesn’t work out-of-box, apply <<WezTerm, fix>> |===
=== Kitty
https://sw.kovidgoyal.net/kitty/[Kitty] uses kbd:[Shift + Ctrl] as the modifier for all its shortcuts (https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.kitty_mod[kitty_mod]) by default.
Add the following snippet to your kitty.conf to unmap the key strokes used by this plugin.
[source]
Don't intercept the following key strokes to make zsh-shift-select work.
map ctrl+shift+left no_op map ctrl+shift+right no_op map ctrl+shift+home no_op map ctrl+shift+end no_op
=== WezTerm
https://wezfurlong.org/wezterm/[WezTerm] uses kbd:[Shift + Ctrl + Left Arrow] and kbd:[Shift + Ctrl + Right Arrow] to activate a pane in the left and right direction, respectively (see https://wezfurlong.org/wezterm/config/default-keys.html[Default Key Assignments] in the docs).
If you want to use these keys in Zsh instead, you can disable the default assignments in your wezterm.lua:
[source, lua]
return { keys = { { key = 'LeftArrow', mods = 'CTRL|SHIFT', action = 'DisableDefaultAssignment' }, { key = 'RightArrow', mods = 'CTRL|SHIFT', action = 'DisableDefaultAssignment' }, }, }
== Installation
=== Using sheldon
If you use https://github.com/rossmacarthur/sheldon[sheldon] plugin manager, run the following command:
[source, sh, subs="+attributes"] sheldon add {proj-name} --github {gh-name}
=== Using zgenom
If you use https://github.com/jandamm/zgenom[zgenom] (successor of https://github.com/tarjoilija/zgen[zgen]) plugin manager, add zgenom load "{gh-name}" to your .zshrc.
=== Using Oh My Zsh
If you use https://github.com/ohmyzsh/ohmyzsh[Oh My Zsh] framework:
. Clone this repository into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins):
+
[source, sh, subs="+attributes"]
git clone https://github.com/{gh-name}.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/{proj-name}
. Add the plugin to the list of plugins for Oh My Zsh to load (inside $ZDOTDIR/.zshrc, by default ~/.zshrc):
+
[source, sh, subs="+attributes"]
plugins=(
# other plugins...
{proj-name}
)
. Start a new terminal session.
=== On Alpine Linux
If you use https://alpinelinux.org/[Alpine Linux] v3.16+ or Edge, you can install {proj-name} via apk and load it using the plugin loader provided in the default Zsh configuration on Alpine (see /etc/zsh/zshrc).
. Install {proj-name} package (as root). [source, sh, subs="+attributes"] apk add {proj-name}
. Symlink {proj-name} plugin to your Zsh plugins directory footnote:[Alternatively, you can add ZSH_LOAD_SYSTEM_PLUGINS=yes to your .zshenv to automatically load all Zsh plugins installed from Alpine packages.]:
[source, sh, subs="+attributes"]
mkdir -p ~/.local/share/zsh/plugins
ln -s /usr/share/zsh/plugins/{proj-name} ~/.local/share/zsh/plugins/
=== Manually (Git Clone) :plugin-dir: ~/.local/share/zsh/plugins/{proj-name}
. Clone this repository somewhere on your machine. This guide will assume {plugin-dir}.
+
[source, sh, subs="+attributes"]
git clone https://github.com/{gh-name} {plugin-dir}
. Add the following to your $ZDOTDIR/.zshrc (by default ~/.zshrc):
+
[source, sh, subs="+attributes"]
source {plugin-dir}/{proj-name}.plugin.zsh
. Start a new terminal session.
== References
- https://stackoverflow.com/questions/5407916/zsh-zle-shift-selection[Zsh zle shift selection – StackOverflow] (the first inspiration, but the used approach is different)
- https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html[Zsh Line Editor]
== License
This project is licensed under http://opensource.org/licenses/MIT/[MIT License]. For the full text of the license, see the link:LICENSE[LICENSE] file.