Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
Shells typically don't have this functionality. If it were available, it might be more likely to be provided by the terminal instead, but I'm not familiar with any that do. –  Dennis Williamson Mar 24 '11 at 1:56
1  
@Dennis Williamson Surprisingly I got the same kind of comments on SO. It is just wrong. Zsh implements selection in ZLE (with marks though). And in fact command line editing has almost nothing to do with the terminal... –  Ugo Mar 24 '11 at 12:02
    
You asked about using shift. You didn't ask how text could be selected generally. –  Dennis Williamson Mar 25 '11 at 0:57
add comment

1 Answer

up vote 3 down vote accepted

One way to do it is to

  1. define new widgets that activate marking on shifted motion keys,
  2. redefine all action widgets to operate on the region (if there is one), and
  3. redefine all the default motion widgets to deactivate marking.

Here's a proof of concept (for xterm), working for:

  • Shift-Left and Shift-Right: start selecting to the left/right, respectively,
  • Left, Right: cancel selecting and move left/right, respectively,
  • Delete: delete selection if there is one, otherwise character under the cursor.

The usual disclaimers apply (there may be other and better ways, your cat may eat your toaster, etc.).

bindkey -e

function zle-line-init {
    marking=0
}
zle -N zle-line-init

function select-char-right {
    if (( $marking != 1 )) 
    then
        marking=1
        zle set-mark-command
    fi
    zle .forward-char
}
zle -N select-char-right

function select-char-left {
    if (( $marking != 1 )) 
    then
        marking=1
        zle set-mark-command
    fi
    zle .backward-char
}
zle -N select-char-left

function forward-char {
    if (( $marking == 1 ))
    then
        marking=0
        NUMERIC=-1 zle set-mark-command
    fi
    zle .forward-char
}
zle -N forward-char

function backward-char {
    if (( $marking == 1 ))
    then
        marking=0
        NUMERIC=-1 zle set-mark-command
    fi
    zle .backward-char
}
zle -N backward-char

function delete-char {
    if (( $marking == 1 ))
    then
        zle kill-region
        marking=0
    else
        zle .delete-char
    fi
}
zle -N delete-char

bindkey '^[[1;2D' select-char-left   # assuming xterm
bindkey '^[[1;2C' select-char-right  # assuming xterm
share|improve this answer
    
It makes a nice example of zle customization. Not so common on the Internet. –  Ugo May 4 '11 at 11:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.