zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: default vi-mode key bindings
@ 2014-12-04 18:28 Oliver Kiddle
  2014-12-05  7:17 ` Bart Schaefer
  0 siblings, 1 reply; 15+ messages in thread
From: Oliver Kiddle @ 2014-12-04 18:28 UTC (permalink / raw)
  To: Zsh workers

I would like to suggest the following changes to the default key
bindings in vi mode.

The undo bindings are a change to existing behaviour.
I think it is better that someone has to explicitly select the
vi-compatible single level undo/redo toggling and that the default is
multi-level undo with redo on Ctrl-R. Ctrl-R is currently redisplay.

The other keys are all vim bindings starting with a g prefix. gg is very
common and well known. I've bound it to beginning-of-buffer-or-history.
The beginning of history is really fairly useless but it probably needs
to be that for consistency with other keys: I only care about it going
to that start of the buffer. I'm slightly inclined to add a goto-line
to go to a line in the buffer based on numeric argument. Note that G
is bound to vi-fetch-history. That does go to the end of a buffer but
not if you're on a previous history line. I use a combination of it
with end-of-buffer-or-history in a custom widget. These should all be
line-based moves within a vi buffer so the patch also sets those flags.

Next is ga to what-cursor-position. ^G probably sooner comes to mind for
that purpose but that's currently list-expand. ga in vim gives you the
ASCII code of the character under the cursor, similarly to zsh's
what-cursor-position.

g~ is swap case (like ~) but with a movement command. I have vim's gu/gU
implemented as shell functions.

Finally, there are ge and gE for moving back to the end of the previous
word.

Any thoughts or other suggestions?

Oliver

diff --git a/Src/Zle/iwidgets.list b/Src/Zle/iwidgets.list
index 1a664e5..b3d1c92 100644
--- a/Src/Zle/iwidgets.list
+++ b/Src/Zle/iwidgets.list
@@ -24,7 +24,7 @@
 "backward-kill-word", backwardkillword, ZLE_KILL | ZLE_KEEPSUFFIX
 "backward-word", backwardword, 0
 "beep", handlefeep, 0
-"beginning-of-buffer-or-history", beginningofbufferorhistory, 0
+"beginning-of-buffer-or-history", beginningofbufferorhistory, ZLE_LINEMOVE
 "beginning-of-history", beginningofhistory, 0
 "beginning-of-line", beginningofline, 0
 "beginning-of-line-hist", beginningoflinehist, 0
@@ -46,7 +46,7 @@
 "down-line-or-search", downlineorsearch, ZLE_LINEMOVE | ZLE_LASTCOL
 "emacs-backward-word", emacsbackwardword, 0
 "emacs-forward-word", emacsforwardword, 0
-"end-of-buffer-or-history", endofbufferorhistory, 0
+"end-of-buffer-or-history", endofbufferorhistory, ZLE_LINEMOVE
 "end-of-history", endofhistory, 0
 "end-of-line", endofline, 0
 "end-of-line-hist", endoflinehist, 0
@@ -143,7 +143,7 @@
 "vi-digit-or-beginning-of-line", vidigitorbeginningofline, 0
 "vi-down-line-or-history", vidownlineorhistory, ZLE_LINEMOVE
 "vi-end-of-line", viendofline, ZLE_LASTCOL
-"vi-fetch-history", vifetchhistory, 0
+"vi-fetch-history", vifetchhistory, ZLE_LINEMOVE
 "vi-find-next-char", vifindnextchar, 0
 "vi-find-next-char-skip", vifindnextcharskip, 0
 "vi-find-prev-char", vifindprevchar, 0
diff --git a/Src/Zle/zle_bindings.c b/Src/Zle/zle_bindings.c
index 50a2955..e3337d0 100644
--- a/Src/Zle/zle_bindings.c
+++ b/Src/Zle/zle_bindings.c
@@ -308,7 +308,7 @@ int vicmdbind[128] = {
     /* ^O */ z_undefinedkey,
     /* ^P */ z_uphistory,
     /* ^Q */ z_undefinedkey,
-    /* ^R */ z_redisplay,
+    /* ^R */ z_redo,
     /* ^S */ z_undefinedkey,
     /* ^T */ z_undefinedkey,
     /* ^U */ z_undefinedkey,
@@ -407,7 +407,7 @@ int vicmdbind[128] = {
     /* r */ z_vireplacechars,
     /* s */ z_visubstitute,
     /* t */ z_vifindnextcharskip,
-    /* u */ z_viundochange,
+    /* u */ z_undo,
     /* v */ z_visualmode,
     /* w */ z_viforwardword,
     /* x */ z_videletechar,
diff --git a/Src/Zle/zle_keymap.c b/Src/Zle/zle_keymap.c
index 30d25eb..6957e9f 100644
--- a/Src/Zle/zle_keymap.c
+++ b/Src/Zle/zle_keymap.c
@@ -1357,6 +1357,13 @@ default_bindings(void)
     bindkey(vismap, "x", refthingy(t_videlete), NULL);
     bindkey(vismap, "~", refthingy(t_vioperswapcase), NULL);
 
+    /* vi mode: some common vim bindings */
+    bindkey(amap, "ga", refthingy(t_whatcursorposition), NULL);
+    bindkey(amap, "ge", refthingy(t_vibackwardwordend), NULL);
+    bindkey(amap, "gE", refthingy(t_vibackwardblankwordend), NULL);
+    bindkey(amap, "gg", refthingy(t_beginningofbufferorhistory), NULL);
+    bindkey(amap, "g~", refthingy(t_vioperswapcase), NULL);
+
     /* emacs mode: arrow keys */ 
     add_cursor_key(emap, TCUPCURSOR, t_uplineorhistory, 'A');
     add_cursor_key(emap, TCDOWNCURSOR, t_downlineorhistory, 'B');


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-04 18:28 PATCH: default vi-mode key bindings Oliver Kiddle
@ 2014-12-05  7:17 ` Bart Schaefer
  2014-12-05  7:35   ` Mikael Magnusson
  2014-12-05 13:26   ` Oliver Kiddle
  0 siblings, 2 replies; 15+ messages in thread
From: Bart Schaefer @ 2014-12-05  7:17 UTC (permalink / raw)
  To: Zsh workers

On Dec 4,  7:28pm, Oliver Kiddle wrote:
}
} The undo bindings are a change to existing behaviour.
} I think it is better that someone has to explicitly select the
} vi-compatible single level undo/redo toggling and that the default is
} multi-level undo with redo on Ctrl-R. Ctrl-R is currently redisplay.

I will bow to the majority opinion on this because I rarely use vi-mode
in zsh, but I will say that I have lost more work to vim's multi-level
undo than to practically any other catastrophe I can think of.

This is probably because my fingers are conditioned by so many years of
using vi before vim even existed.

} The other keys are all vim bindings starting with a g prefix.

Maybe the thing to do here is to create a new keymap with vim bindings?


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-05  7:17 ` Bart Schaefer
@ 2014-12-05  7:35   ` Mikael Magnusson
  2014-12-05  7:56     ` Jan Larres
  2014-12-05 13:26   ` Oliver Kiddle
  1 sibling, 1 reply; 15+ messages in thread
From: Mikael Magnusson @ 2014-12-05  7:35 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh workers

On Fri, Dec 5, 2014 at 8:17 AM, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Dec 4,  7:28pm, Oliver Kiddle wrote:
> }
> } The undo bindings are a change to existing behaviour.
> } I think it is better that someone has to explicitly select the
> } vi-compatible single level undo/redo toggling and that the default is
> } multi-level undo with redo on Ctrl-R. Ctrl-R is currently redisplay.
>
> I will bow to the majority opinion on this because I rarely use vi-mode
> in zsh, but I will say that I have lost more work to vim's multi-level
> undo than to practically any other catastrophe I can think of.

If you accidentally undo a bunch of stuff, and then type something in
vim, you can use :undolist to bring up the discarded redo history, and
jump between various redo/undo tree branches (extremely confusing, but
probably easier than grepping /dev/mem).

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-05  7:35   ` Mikael Magnusson
@ 2014-12-05  7:56     ` Jan Larres
  0 siblings, 0 replies; 15+ messages in thread
From: Jan Larres @ 2014-12-05  7:56 UTC (permalink / raw)
  To: zsh-workers

On 05/12/14 20:35, Mikael Magnusson wrote:
> On Fri, Dec 5, 2014 at 8:17 AM, Bart Schaefer <schaefer@brasslantern.com> wrote:
>> I will bow to the majority opinion on this because I rarely use vi-mode
>> in zsh, but I will say that I have lost more work to vim's multi-level
>> undo than to practically any other catastrophe I can think of.
>
> If you accidentally undo a bunch of stuff, and then type something in
> vim, you can use :undolist to bring up the discarded redo history, and
> jump between various redo/undo tree branches (extremely confusing, but
> probably easier than grepping /dev/mem).

This is getting a bit off-topic, but if you use the Gundo plugin in Vim
it will show you a graphical representation of the undo/redo history
tree that allows you to easily restore certain "branches".

-Jan


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-05  7:17 ` Bart Schaefer
  2014-12-05  7:35   ` Mikael Magnusson
@ 2014-12-05 13:26   ` Oliver Kiddle
  2014-12-05 17:46     ` Ray Andrews
  1 sibling, 1 reply; 15+ messages in thread
From: Oliver Kiddle @ 2014-12-05 13:26 UTC (permalink / raw)
  To: Zsh workers

Bart wrote:
> 
> I will bow to the majority opinion on this because I rarely use vi-mode
> in zsh, but I will say that I have lost more work to vim's multi-level
> undo than to practically any other catastrophe I can think of.

I can see that being a problem if you haven't learnt the key for redo
and hit undo several times before you realise what's going on.

The type of person that would affect would need to like vi-style editing
enough to use it in zsh without having ever been tempted into learning
vim. Plenty of people use vi for quick edits from the terminal but use
something else like emacs, eclipse, nedit or whatever for longer editing
sessions. I'd guess that most such people use emacs mode on zsh. It's
hard to find out for sure other than by breaking things. A quick sneak
peak in the .zshrc/.vimrc of the three vim and zsh using people who work
at the same place as me show that they have all left the default of
vi-undo-change. None have put vim in vi-compatible mode.

> This is probably because my fingers are conditioned by so many years of
> using vi before vim even existed.

Since, getting used to vim, I've found that I never use the line undo
with U anymore. Zsh doesn't support that.
 
> } The other keys are all vim bindings starting with a g prefix.
> 
> Maybe the thing to do here is to create a new keymap with vim bindings?

That might sooner lead to calls to support dozens of other editors in
that manner which is the sort of thing better handled by an oh-my-zsh
style plugin.

It also wouldn't work for some vim features like the additional special
marks and registers. vi didn't leave a lot of keys free so most extra
vim functions start with either g or z and can be unbound in one
command. Checking :help compatible, the undo is the most significant vim
behaviour difference. There's also backspacing over newlines and the
start of an insertion, some vi movements going to the start of a line
and cursors working in insert mode.

Oliver


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-05 13:26   ` Oliver Kiddle
@ 2014-12-05 17:46     ` Ray Andrews
  2014-12-05 17:59       ` Eric Cook
                         ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Ray Andrews @ 2014-12-05 17:46 UTC (permalink / raw)
  To: zsh-workers

On 12/05/2014 05:26 AM, Oliver Kiddle wrote:

> The type of person that would affect would need to like vi-style 
> editing enough to use it in zsh without having ever been tempted into 
> learning vim. Plenty of people use vi for quick edits from the terminal 

Pardon my absolute ignorance about what you guys are talking about. Can 
someone give me some idea?  vi is an editor that Martians like, because 
it helps if you have three arms, yes?  And Venusians like emacs because 
the suckers on their tentacles can hit Ctrl+Alt+Esc+L+A very easily.  
And they have wars over it.  vi has insert mode and command mode and 
such.  Ok, but is any of that actually used at the command line?  Does 
anyone need/use 'command mode' when typing a simple command?

     $ grep "The falcon cannot hear the falconer;" /Yeats/*

What would one do with command mode there?  Editing a novel, of course, 
but a command line?  Or does this mean that zsh somehow butts itself 
right in to vi so that when editing a novel, 'vi-style' as set by zsh is 
somehow active?  Is that polite?  And then there's vim mode too but 
that's different from vi mode?  I'm missing something fundamental here.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-05 17:46     ` Ray Andrews
@ 2014-12-05 17:59       ` Eric Cook
  2014-12-05 18:04       ` richo
  2014-12-05 18:55       ` Lawrence Velázquez
  2 siblings, 0 replies; 15+ messages in thread
From: Eric Cook @ 2014-12-05 17:59 UTC (permalink / raw)
  To: zsh-workers

On 12/05/2014 12:46 PM, Ray Andrews wrote:
> Ok, but is any of that actually used at the command line?  Does anyone
> need/use 'command mode' when typing a simple command?
>
Yes and yes.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-05 17:46     ` Ray Andrews
  2014-12-05 17:59       ` Eric Cook
@ 2014-12-05 18:04       ` richo
  2014-12-05 18:55       ` Lawrence Velázquez
  2 siblings, 0 replies; 15+ messages in thread
From: richo @ 2014-12-05 18:04 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-workers

On 05/12/14 09:46 -0800, Ray Andrews wrote:
>On 12/05/2014 05:26 AM, Oliver Kiddle wrote:
>
>>The type of person that would affect would need to like vi-style
>>editing enough to use it in zsh without having ever been tempted
>>into learning vim. Plenty of people use vi for quick edits from the
>>terminal
>
>Pardon my absolute ignorance about what you guys are talking about.
>Can someone give me some idea?  vi is an editor that Martians like,
>because it helps if you have three arms, yes?  And Venusians like
>emacs because the suckers on their tentacles can hit Ctrl+Alt+Esc+L+A
>very easily.  And they have wars over it.  vi has insert mode and
>command mode and such.  Ok, but is any of that actually used at the
>command line?  Does anyone need/use 'command mode' when typing a
>simple command?
>
>    $ grep "The falcon cannot hear the falconer;" /Yeats/*
>
>What would one do with command mode there?  Editing a novel, of
>course, but a command line?  Or does this mean that zsh somehow butts
>itself right in to vi so that when editing a novel, 'vi-style' as set
>by zsh is somehow active?  Is that polite?  And then there's vim mode
>too but that's different from vi mode?  I'm missing something
>fundamental here.

Please keep the holy wars off list.

Yes, vi mode gets used. I use command mode a lot (These fingers only know how
to vi). The mere existance of the feature and the people working on it should
be self evident.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: PATCH: default vi-mode key bindings
  2014-12-05 17:46     ` Ray Andrews
  2014-12-05 17:59       ` Eric Cook
  2014-12-05 18:04       ` richo
@ 2014-12-05 18:55       ` Lawrence Velázquez
  2014-12-05 20:29         ` vi mode question Ray Andrews
  2 siblings, 1 reply; 15+ messages in thread
From: Lawrence Velázquez @ 2014-12-05 18:55 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-workers

On Dec 5, 2014, at 12:46 PM, Ray Andrews <rayandrews@eastlink.ca> wrote:

> Ok, but is any of that actually used at the command line?

"The command line" doesn't dictate what keys ought to be used to move about and edit text.

> Does anyone need/use 'command mode' when typing a simple command?
> 
>    $ grep "The falcon cannot hear the falconer;" /Yeats/*
> 
> What would one do with command mode there?

Edit the command in a way that one is comfortable with? Not everyone likes Emacs keybindings.

> Editing a novel, of course, but a command line?  Or does this mean that zsh somehow butts itself right in to vi so that when editing a novel, 'vi-style' as set by zsh is somehow active?  Is that polite?

I'm pretty sure one has to enable vi mode explicitly. Then, I presume one has a command mode and an insert mode, as expected.

> And then there's vim mode too but that's different from vi mode?  I'm missing something fundamental here.

There is no vim mode; that was Bart musing. The issue is whether zsh should cater to people who know vim but not vi.

vq

^ permalink raw reply	[flat|nested] 15+ messages in thread

* vi mode question.
  2014-12-05 18:55       ` Lawrence Velázquez
@ 2014-12-05 20:29         ` Ray Andrews
  2014-12-05 21:04           ` Lawrence Velázquez
  2014-12-06 22:09           ` Bart Schaefer
  0 siblings, 2 replies; 15+ messages in thread
From: Ray Andrews @ 2014-12-05 20:29 UTC (permalink / raw)
  To: zsh-workers

On 12/05/2014 10:55 AM, Lawrence Velázquez wrote:

All,
> Edit the command in a way that one is comfortable with? Not everyone 
> likes Emacs keybindings. 

Alright then, vi command mode at the command line is actively used. As I 
mentioned, I had
wondered if this was some sort of modification of what happened inside 
vi itself.  Or perhaps some editing feature that I don't know about.  
Whatever folks like of course.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: vi mode question.
  2014-12-05 20:29         ` vi mode question Ray Andrews
@ 2014-12-05 21:04           ` Lawrence Velázquez
  2014-12-06 22:09           ` Bart Schaefer
  1 sibling, 0 replies; 15+ messages in thread
From: Lawrence Velázquez @ 2014-12-05 21:04 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-workers

On Dec 5, 2014, at 3:29 PM, Ray Andrews <rayandrews@eastlink.ca> wrote:

> As I mentioned, I had wondered if this was some sort of modification of what happened inside vi itself.  Or perhaps some editing feature that I don't know about.

It's merely a different way of working with command line text. You can read all about it in zshzle(1); salient sections include "KEYMAPS" and "STANDARD WIDGETS".

vq

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: vi mode question.
  2014-12-05 20:29         ` vi mode question Ray Andrews
  2014-12-05 21:04           ` Lawrence Velázquez
@ 2014-12-06 22:09           ` Bart Schaefer
  2014-12-06 22:44             ` Ray Andrews
  1 sibling, 1 reply; 15+ messages in thread
From: Bart Schaefer @ 2014-12-06 22:09 UTC (permalink / raw)
  To: zsh-workers

Anyone who knows vi(m) will already know this, but for Ray et al.:

The "insert mode" in vi is for exactly that and only that:  inserting
new text.  You can backspace over mistakes to erase them but you can't
move back and forth over words or lines to make other kinds of changes.

"Command mode" has to be entered (by pressing the ESC key) any time you
want to move around or do any kind of complex editing.  While in the
command mode, you can copy and paste text, but not insert brand new
text (to do that you have to drop back into insert mode again).

You might be fooled by the illusion that arrow keys or page up/down seem
to work in insert mode, but really what those keys are doing is leaving
insert mode to enter command mode, moving the cursor, and then dropping
back into insert mode at the new location.

Emacs is "modeless" which basically means it is in command mode all the
time, except that one of the possible commands is for the most recent
key press to insert itself at the current cursor position.

Either editor could mimic the basic functions of the other (in fact for
the first few years that I used emacs, I used it only with the "evi"
emulator), but that becomes increasingly difficult for the specialized
features.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: vi mode question.
  2014-12-06 22:09           ` Bart Schaefer
@ 2014-12-06 22:44             ` Ray Andrews
  2014-12-07  0:32               ` Christian Neukirchen
  0 siblings, 1 reply; 15+ messages in thread
From: Ray Andrews @ 2014-12-06 22:44 UTC (permalink / raw)
  To: zsh-workers

On 12/06/2014 02:09 PM, Bart Schaefer wrote:
> Anyone who knows vi(m) will already know this, but for Ray et al.:
...

Thanks for that.  I wonder why there isn't (*if* there isn't) a compromise:
All the most basic editing/moving primitives working in insert mode like 
emacs or
the (what DOS/Windows people would call) 'normal' human editors, but
still having a command mode for the complexities (where emacs goes
nuts with all those combinations). You'd mostly be in 'normal' mode, but
when you needed to do some heavy lifting, 'command mode' would still
be there.  I haven't tried vi for a long time, I should try again.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: vi mode question.
  2014-12-06 22:44             ` Ray Andrews
@ 2014-12-07  0:32               ` Christian Neukirchen
  2014-12-07  5:02                 ` Ray Andrews
  0 siblings, 1 reply; 15+ messages in thread
From: Christian Neukirchen @ 2014-12-07  0:32 UTC (permalink / raw)
  To: zsh-workers

Ray Andrews <rayandrews@eastlink.ca> writes:

> On 12/06/2014 02:09 PM, Bart Schaefer wrote:
>> Anyone who knows vi(m) will already know this, but for Ray et al.:
> ...
>
> Thanks for that.  I wonder why there isn't (*if* there isn't) a compromise:
> All the most basic editing/moving primitives working in insert mode
> like emacs or
> the (what DOS/Windows people would call) 'normal' human editors, but
> still having a command mode for the complexities (where emacs goes
> nuts with all those combinations). You'd mostly be in 'normal' mode, but
> when you needed to do some heavy lifting, 'command mode' would still
> be there.  I haven't tried vi for a long time, I should try again.

bindkey -e
bindkey '^[' vi-cmd-mode

Effectively combines emacs as insert-mode with vi as command mode.

-- 
Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: vi mode question.
  2014-12-07  0:32               ` Christian Neukirchen
@ 2014-12-07  5:02                 ` Ray Andrews
  0 siblings, 0 replies; 15+ messages in thread
From: Ray Andrews @ 2014-12-07  5:02 UTC (permalink / raw)
  To: zsh-workers

On 12/06/2014 04:32 PM, Christian Neukirchen wrote:

> Effectively combines emacs as insert-mode with vi as command mode. 

It's not something I'd remotely need.  So long  as I have delete, 
backspace, the arrow keys, and a few others, that's enough for me to 
edit a command line.  For me command mode is for editing documents.


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2014-12-07  5:32 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-04 18:28 PATCH: default vi-mode key bindings Oliver Kiddle
2014-12-05  7:17 ` Bart Schaefer
2014-12-05  7:35   ` Mikael Magnusson
2014-12-05  7:56     ` Jan Larres
2014-12-05 13:26   ` Oliver Kiddle
2014-12-05 17:46     ` Ray Andrews
2014-12-05 17:59       ` Eric Cook
2014-12-05 18:04       ` richo
2014-12-05 18:55       ` Lawrence Velázquez
2014-12-05 20:29         ` vi mode question Ray Andrews
2014-12-05 21:04           ` Lawrence Velázquez
2014-12-06 22:09           ` Bart Schaefer
2014-12-06 22:44             ` Ray Andrews
2014-12-07  0:32               ` Christian Neukirchen
2014-12-07  5:02                 ` Ray Andrews

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).