Re: ksh: allow editing empty line with 'v' in vi-mode

Johannes Thyssen Tishman <[email protected]> Fri, 31 Jul 2026 09:10:13 +0000
Newsgroups gmane.os.openbsd.tech
Message-ID <[email protected]>
2026-07-30T22:55:18+0200 Uwe Werler <[email protected]>:
> On Thu Jul 30, 2026 at 10:41 PM CEST, Uwe Werler wrote:
> > On Thu Jul 30, 2026 at 8:21 PM CEST, Johannes Thyssen Tishman wrote:
> >> Sometimes, when I know I'm gonna write a long command, I like to use vi
> >> mode's 'v' function to edit the command in my editor. Currently this
> >> only works to modify a command, i.e., when the command line is not
> >> empty, e.g.:
> >>
> >>   $ abcd^[v     --> Opens editor with 'abcd'
> >>
> >> Because of this, I've now acquired the (bad) habit of typing nonsense
> >> before using this function, such that I can start writing the command
> >> from scratch in the editor. The diff below allows using this function
> >> with an empty command line:
> >>
> >>   $ ^[v         --> Opens empty editor
> >>
> >> I tried adding tests, e.g., by setting VISUAL=cat and HISTFILE=`mktemp
> >> ...`, but I couldn't get them to work. However, both the examples above,
> >> as well as when passing a number from the command history (see fc -l),
> >> e.g.,
> >>
> >>   $ ^[10v       --> Opens 10th command in history
> >>
> >> are still working fine.
> >>
> >> Index: vi.c
> >> ===================================================================
> >> RCS file: /cvs/src/bin/ksh/vi.c,v
> >> diff -u -p -r1.70 vi.c
> >> --- vi.c	22 May 2026 18:11:08 -0000	1.70
> >> +++ vi.c	30 Jul 2026 18:05:10 -0000
> >> @@ -964,8 +964,6 @@ vi_cmd(int argcnt, const char *cmd)
> >>  			break;
> >>  
> >>  		case 'v':
> >> -			if (es->linelen == 0 && argcnt == 0)
> >> -				return -1;
> >>  			if (!argcnt) {
> >>  				if (modified) {
> >>  					es->cbuf[es->linelen] = '\0';
> >
> > +++ for this diff I already proposed in 2022:
> >
> > https://marc.info/?l=openbsd-tech&m=166196351223367&w=2

Nice! I should've checked the archives before posting. Regarding the
issue mentioned in that thread:

> In vi mode, if you enter only spaces, then ESC then `v' to edit:
> - our ksh will edit the previous line, not current whitespace one

This is only true if HISTCONTROL includes 'ignorespace', which causes
the (white-space only) line not to be saved to history (history.c:658).
Since no history command number is given, argcnt is 0 in following
relevant snippet from vi.c

   976				shf_snprintf(es->cbuf, es->cbufsize,
   977				    argcnt ? "%s %d" : "%s",
   978				    "fc -e ${VISUAL:-${EDITOR:-vi}} --",
   979				    argcnt);

This evaluates to "fc -e ${VISUAL:-${EDITOR:-vi}} --", causing the
editor to be opened with the last command in history (which is not the
white-space command as it wasn't saved). This is the case whether we
apply our proposed diff above or not.

> and here's another one I once proposed:
> 
> https://marc.info/?l=openbsd-tech&m=166681423915767&w=2

I think it makes sense not to save empty lines in history, so ok jtt@
for the diff in the above archived thread. Given that there were no
objections, I can commit if kn@ is still ok with it.