Download raw body.
ksh vi mode: stop 'P' command from moving the cursor
Ingo, On Mon, Apr 21, 2025 at 11:38:01PM +0200, Ingo Schwarze wrote: > Hello, > > here is a patch to change the behaviour of the "paste before" (P) command > in the VI command line editing mode of ksh(1). > With your diff, after pasting with the 'P' command the cursor lands one character *after* the last character in the string pasted. I'm not saying this is wrong, maybe it's more convenient, but it's not what the 'p' command does or what other popular applications (vim, bash) that have adopted moving the cursor to the end of the pasted string do. The two diffs below are *NOT TESTED* (but considering I'm not modifying functions in this case, they probably won't cause regressions.) The idea is to show the difference between two behaviors with practical examples; you'll decide which one you like best. The first behavior (vi(1), nvi2 from ports, FreeBSD sh): After pasting text with 'p' or 'P' the cursor lands on the first character of the string pasted. The second behavior (vim, bash and other shells): After pasting text with 'p' or 'P' the cursor lands on the last character of the string pasted. (By the way, I found a bug on vi(1) paste command. I'll report it in other thread in the future.) FIRST BEHAVIOR (vi like) Index: vi.c =================================================================== RCS file: /cvs/src/bin/ksh/vi.c,v diff -u -p -r1.61 vi.c --- vi.c 21 Apr 2025 20:06:15 -0000 1.61 +++ vi.c 24 Apr 2025 08:36:19 -0000 @@ -837,8 +837,10 @@ vi_cmd(int argcnt, const char *cmd) while (es->cursor < es->linelen) if (!isu8cont(es->cbuf[++es->cursor])) break; + any = es->cursor; while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0) ; + es->cursor = any + 1; while (es->cursor > 0) if (!isu8cont(es->cbuf[--es->cursor])) break; @@ -848,11 +850,10 @@ vi_cmd(int argcnt, const char *cmd) case 'P': modified = 1; hnum = hlast; - any = 0; + any = es->cursor; while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0) - any = 1; - if (any && es->cursor != 0) - es->cursor--; + continue; + es->cursor = any; if (argcnt != 0) return -1; break; SECOND BEHAVIOR (vim like) Index: vi.c =================================================================== RCS file: /cvs/src/bin/ksh/vi.c,v diff -u -p -r1.61 vi.c --- vi.c 21 Apr 2025 20:06:15 -0000 1.61 +++ vi.c 24 Apr 2025 07:34:50 -0000 @@ -848,11 +848,9 @@ vi_cmd(int argcnt, const char *cmd) case 'P': modified = 1; hnum = hlast; - any = 0; while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0) - any = 1; - if (any && es->cursor != 0) - es->cursor--; + continue; + es->cursor--; if (argcnt != 0) return -1; break; -- Walter
ksh vi mode: stop 'P' command from moving the cursor