shellzshterminfozsh-zle

Zsh zle shift selection


How to use shift to select part of the commandline (like in many text editors) ?


Solution

  • shift-arrow() {
      ((REGION_ACTIVE)) || zle set-mark-command
      zle $1
    }
    shift-left()  shift-arrow backward-char
    shift-right() shift-arrow forward-char
    shift-up()    shift-arrow up-line-or-history
    shift-down()  shift-arrow down-line-or-history
    zle -N shift-left
    zle -N shift-right
    zle -N shift-up
    zle -N shift-down
    
    bindkey $terminfo[kLFT] shift-left
    bindkey $terminfo[kRIT] shift-right
    bindkey $terminfo[kri]  shift-up
    bindkey $terminfo[kind] shift-down
    

    That assumes your terminal sends a different escape sequence upon Shift-Arrows from the one sent upon Arrow and that your terminfo database is properly populated with corresponding kLFT and kRIT capabilities, and that you're using emacs style key binding.

    Or, to factorize the code a bit:

    shift-arrow() {
      ((REGION_ACTIVE)) || zle set-mark-command
      zle $1
    }
    for key  kcap seq        widget (
        left  LFT $'\e[1;2D' backward-char
        right RIT $'\e[1;2C' forward-char
        up    ri  $'\e[1;2A' up-line-or-history
        down  ind $'\e[1;2B' down-line-or-history
      ) {
      functions[shift-$key]="shift-arrow $widget"
      zle -N shift-$key
      bindkey ${terminfo[k$kcap]-$seq} shift-$key
    }
    

    Above, hardcoded sequences for cases where the terminfo database doesn't have the information (using xterm sequences).