Selecting text with the keyboard

420 views
Skip to first unread message

Andy Koppe

unread,
Sep 6, 2010, 2:13:53 AM9/6/10
to mintty
A user has expressed an interest in implementing enhancement issue 84
("Selecting text with the keyboard") and wondered how I'd go about
implementing it if I had the time. There are a few design issues here,
so I thought I'd reply via the mailing list in case others have inputs
on these.

I think there needs to be a separate keyboard selection mode, where
the arrow keys can be used to move around the screen and scrollback
buffer, whereas of course normally they're sent to the application.
This raises two questions: how do you get into selection mode, and how
is it indicated visually?

I have sort of reserved Alt+Enter as the selection mode key (which is
why Alt+F11 rather than Alt+Enter is the "official" fullscreen
shortcut shown in the context menu). Alt+Enter is used by some
applications as well though, so might not be ideal. Ctrl+Enter is
another possibility, since that doesn't actually have a well-defined
keycode across different terminals. Does anything else spring to mind?

As for indicating selection mode, changing cursor type or colour seems
the most obvious thing to do. Question is, how exactly? Adding
something like "(selecting)" to the window title is another
possibility, but wouldn't work in fullscreen mode or if issue 7 ("Add
option to remove title bar") got implemented.

Next question is, how the selection should actually be done once
you're in selection mode. I think it should primarily follow the
standard CUA/Windows approach: move the cursor around with the arrow
keys, select by holding down Shift while moving the cursor. The emacs
and vi approaches could be implemented in addition to that.

Then finally, how do you finish the selection and copy it? The
standard shortcuts seem the obvious choice: Ctrl+Insert and Ctrl+C,
although the latter is a bit risky if accidentally hit outside
selection mode. The Escape key could be used to leave selection mode
without copying.

A couple more questions. What happens if you use the mouse in keyboard
selection mode? How does selection mode interact with 'Flip Screen'?

The starting point for an implementation of all this would be
win_key_down() in wininput.c. Unlike the normal keyboard handling
though, it needs to access a lot of terminal data, therefore I think
win_key_down() should hand selection mode key handling over to a term_
function. termclip.c looks like an appropriate place. A flag in struct
term would need to indicate selection mode, and there'd also need to
be a field for the selection cursor position. The extent of the
selection is indicated by sel_start and sel_end, and I'd aim to reuse
as much as possible of the existing selection infrastructure.

Hope this helps,
Andy

Mario Pareja

unread,
Sep 23, 2010, 7:29:40 AM9/23/10
to mintty
You also need to consider normal and block selection modes. In normal
mode, you wrap at the end of lines just like a text editor would and
select entire lines when selecting downward. In block mode, you can
select a rectangular snippet of text.

In general, I find normal mode to be more useful in the terminal, but
I could see the need for block mode too.

Anyone else have thoughts on this?

Regards,

Mario

Andy Koppe

unread,
Sep 24, 2010, 1:13:11 AM9/24/10
to mintty
On Sep 23, 12:29 pm, Mario Pareja wrote:
> On Sep 6, 2:13 am, Andy Koppe wrote:
> > Next question is, how the selection should actually be done once
> > you're in selection mode. I think it should primarily follow the
> > standard CUA/Windows approach: move the cursor around with the arrow
> > keys, select by holding down Shift while moving the cursor. The emacs
> > and vi approaches could be implemented in addition to that.
>
> You also need to consider normal and block selection modes.  In normal
> mode, you wrap at the end of lines just like a text editor would and
> select entire lines when selecting downward.  In block mode, you can
> select a rectangular snippet of text.
>
> In general, I find normal mode to be more useful in the terminal, but
> I could see the need for block mode too.

Block mode is supported for mouse selection by holding down Alt while
dragging, so it would indeed make sense to do the same for keyboard
selection, i.e. Shift+Alt+arrow would do block selection there.
Various editors including the one in Visual Studio and those based on
Scintilla do that.

Andy

timothy...@gmail.com

unread,
Mar 13, 2012, 11:54:47 AM3/13/12
to mintty-...@googlegroups.com
On Monday, September 6, 2010 1:13:53 AM UTC-5, Andy Koppe wrote:

I think there needs to be a separate keyboard selection mode, where
the arrow keys can be used to move around the screen and scrollback
buffer, whereas of course normally they're sent to the application.
...
Hope this helps,
Andy

I have a more modest feature / approach related to keyboard-based text selection that I'd like to suggest, if it isn't already possible today.

I was looking around to see if there was an existing way to highlight/ select text with the keyboard in MinTTY, but the whole separate- keyboard-mode-and-selecting-in-scrollback idea is really more than I was looking for.  I'd be satisfied if I could just highlight text on the command line with Shift+ cursor motion keys (arrows, home, end, etc) and have copying done (or not) according to the preferences set for mouse-drag highlighting and copying (i.e., copy on select y/n).

Perhaps there is a way to accomplish this by mapping keys in MinTTY today, but I'm not sure how to accomplish it.  I'd guess that this type of keyboard selection feature would not conflict with the more ambitious change sketched out above, since it would only affect the normal editing mode.

Thanks for your time.

-Tim

* I do use the emacs/readline yank-and-kill/point-and-mark methods for grabbing text, but there are times when switching back and forth with Windows programs that I would love to not have to perform the mental context switch, and adding this capability seems in line with the MinTTY philosophy of supporting both Windows and Linux idioms when feasible.  Also, I have non-technical, Windows-using coworkers who we coerce into using Cygwin, and this would be helpful to them as well. 

Andy Koppe

unread,
Mar 28, 2012, 12:57:51 AM3/28/12
to mintty
Yep, I'd find that useful too. The right place to implement it would
be the shell rather than the terminal though. However, bash certainly
can't do it at the moment. Zsh at least has highlighting of the
selected text, and it also has a programmable line editor, via so-
called zle widgets that are bound to keys. Hence I expect it would be
possible to implement Windows (née CUA) style selection there. I've
had a look around for a solution like that in the past, but
unfortunately I didn't find anything.

I've also wondered previously whether a hackish solution in mintty
would be possible. The first left/right arrow key press with Shift
down could set the selection start, and such keypresses would then be
sent to the shell without the Shift. The selection would extend
between the start point and wherever the shell moves the cursor.
Keypresses other than Shift+arrow would end the selection. I haven't
so far got round to try this, and can't make any promises on when I
might be able to.

Meanwhile, there are some tips on how to get bash/readline to behave
in a more Windows-like way here (and a few more in the section above):

http://code.google.com/p/mintty/wiki/Tips#Readline_configuration

You might also want to enable the "Clicks place command line cursor"
mode on the Mouse page of mintty's option.

Regards,
Andy
Reply all
Reply to author
Forward
0 new messages