├── LICENSE ├── README.md └── togglecursor.plugin.zsh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Chitoku 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | zsh-togglecursor 2 | ================ 3 | 4 | This plugin toggles cursor when switching between the vi command (vicmd) and 5 | insert (usually main) keymaps. 6 | 7 | # Requirements 8 | 9 | ## zsh 10 | 11 | Version 5.3 or higher is required. 12 | 13 | ## Terminal 14 | 15 | The terminal application that understands `DECSCUSR`, which fixes the style of 16 | the cursor. Terminator, Konsole, iTerm2, Apple Terminal, and Konsole are known 17 | to support the feature. 18 | 19 | ## tmux 20 | 21 | This plugin also works inside tmux if it is properly configured. Add 22 | `terminal-overrides` to your `tmux.conf` as follows: 23 | 24 | ```tmux 25 | set -ga terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q' 26 | ``` 27 | 28 | Note: This was taken from [FAQ · neovim/neovim Wiki](https://github.com/neovim/neovim/wiki/FAQ) 29 | -------------------------------------------------------------------------------- /togglecursor.plugin.zsh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env zsh 2 | 3 | autoload -U is-at-least 4 | autoload -U add-zle-hook-widget 5 | 6 | _zsh_togglecursor() { 7 | local ret=$? 8 | 9 | case $KEYMAP in 10 | 'main') 11 | _zsh_togglecursor_apply_cursor 'line' 12 | ;; 13 | *) 14 | _zsh_togglecursor_apply_cursor 'block' 15 | ;; 16 | esac 17 | return $ret 18 | } 19 | 20 | _zsh_togglecursor_reset() { 21 | _zsh_togglecursor_apply_cursor 'block' 22 | } 23 | 24 | _zsh_togglecursor_supported() { 25 | [[ $TERM_PROGRAM =~ tmux\|iTerm\.app\|Apple_Terminal ]] || 26 | [[ -n $WT_SESSION ]] || 27 | [[ $VTE_VERSION -ge 3900 ]] || 28 | [[ $TERMINAL_EMULATOR = 'JetBrains-JediTerm' ]] 29 | } 30 | 31 | _zsh_togglecursor_apply_cursor() { 32 | _zsh_togglecursor_supported || return $ret 33 | 34 | local format='%b' 35 | 36 | case "$1" in 37 | 'block') 38 | printf $format "\e[1 q" 39 | ;; 40 | 'underline') 41 | printf $format "\e[3 q" 42 | ;; 43 | 'line') 44 | printf $format "\e[5 q" 45 | ;; 46 | esac 47 | } 48 | 49 | if is-at-least 5.3; then 50 | add-zle-hook-widget zle-line-init _zsh_togglecursor 51 | add-zle-hook-widget zle-line-finish _zsh_togglecursor_reset 52 | add-zle-hook-widget zle-keymap-select _zsh_togglecursor 53 | else 54 | print -r -- >&2 'zsh-togglecursor: add-zle-hook-widget is not supported on this version of zsh.' 55 | fi 56 | --------------------------------------------------------------------------------