Skip to content

Commit 8b530c1

Browse files
committed
patch 8.1.2231: not easy to move to the middle of a text line
Problem: Not easy to move to the middle of a text line. Solution: Add the gM command. (Yasuhiro Matsumoto, closes #2070)
1 parent 077ff43 commit 8b530c1

7 files changed

Lines changed: 57 additions & 6 deletions

File tree

runtime/doc/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ tag char note action in Normal mode ~
781781
|gn| gn 1,2 find the next match with the last used
782782
search pattern and Visually select it
783783
|gm| gm 1 go to character at middle of the screenline
784+
|gM| gM 1 go to character at middle of the text line
784785
|go| go 1 cursor to byte N in the buffer
785786
|gp| ["x]gp 2 put the text [from register x] after the
786787
cursor N times, leave the cursor after it

runtime/doc/motion.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*motion.txt* For Vim version 8.1. Last change: 2019 Jun 02
1+
*motion.txt* For Vim version 8.1. Last change: 2019 Oct 28
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -227,6 +227,12 @@ g^ When lines wrap ('wrap' on): To the first non-blank
227227
gm Like "g0", but half a screenwidth to the right (or as
228228
much as possible).
229229

230+
*gm* *gM*
231+
gM Like "g0", but to halfway the text of the line.
232+
With a count: to this percentage of text in the line.
233+
Thus "10gM" is near the start of the text and "90gM"
234+
is near the end of the text.
235+
230236
*g$* *g<End>*
231237
g$ or g<End> When lines wrap ('wrap' on): To the last character of
232238
the screen line and [count - 1] screen lines downward

runtime/doc/quickref.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ N is used to indicate an optional count that can be given before the command.
4747
|g$| N g$ to last character in screen line (differs from "$"
4848
when lines wrap)
4949
|gm| gm to middle of the screen line
50+
|gM| gM to middle of the line
5051
|bar| N | to column N (default: 1)
5152
|f| N f{char} to the Nth occurrence of {char} to the right
5253
|F| N F{char} to the Nth occurrence of {char} to the left

runtime/doc/usr_25.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,13 @@ scroll:
346346

347347
g0 to first visible character in this line
348348
g^ to first non-blank visible character in this line
349-
gm to middle of this line
349+
gm to middle of screen line
350+
gM to middle of the text in this line
350351
g$ to last visible character in this line
351352

352-
|<-- window -->|
353-
some long text, part of which is visible ~
354-
g0 g^ gm g$
353+
|<-- window -->|
354+
some long text, part of which is visible in one line ~
355+
g0 g^ gm gM g$
355356

356357

357358
BREAKING AT WORDS *edit-no-break*

src/normal.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5979,6 +5979,24 @@ nv_g_cmd(cmdarg_T *cap)
59795979
curwin->w_set_curswant = TRUE;
59805980
break;
59815981

5982+
case 'M':
5983+
{
5984+
char_u *ptr = ml_get_curline();
5985+
5986+
oap->motion_type = MCHAR;
5987+
oap->inclusive = FALSE;
5988+
if (has_mbyte)
5989+
i = mb_string2cells(ptr, STRLEN(ptr));
5990+
else
5991+
i = (int)STRLEN(ptr);
5992+
if (cap->count0 > 0 && cap->count0 <= 100)
5993+
coladvance((colnr_T)(i * cap->count0 / 100));
5994+
else
5995+
coladvance((colnr_T)(i / 2));
5996+
curwin->w_set_curswant = TRUE;
5997+
}
5998+
break;
5999+
59826000
case '_':
59836001
/* "g_": to the last non-blank character in the line or <count> lines
59846002
* downward. */

src/testdir/test_normal.vim

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,7 @@ fun! Test_normal33_g_cmd2()
17331733
set wrap listchars= sbr=
17341734
let lineA='abcdefghijklmnopqrstuvwxyz'
17351735
let lineB='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1736+
let lineC='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
17361737
$put =lineA
17371738
$put =lineB
17381739

@@ -1766,9 +1767,30 @@ fun! Test_normal33_g_cmd2()
17661767
call assert_equal(15, col('.'))
17671768
call assert_equal('l', getreg(0))
17681769

1770+
norm! 2ggdd
1771+
$put =lineC
1772+
1773+
" Test for gM
1774+
norm! gMyl
1775+
call assert_equal(73, col('.'))
1776+
call assert_equal('0', getreg(0))
1777+
" Test for 20gM
1778+
norm! 20gMyl
1779+
call assert_equal(29, col('.'))
1780+
call assert_equal('S', getreg(0))
1781+
" Test for 60gM
1782+
norm! 60gMyl
1783+
call assert_equal(87, col('.'))
1784+
call assert_equal('E', getreg(0))
1785+
1786+
" Test for g Ctrl-G
1787+
set ff=unix
1788+
let a=execute(":norm! g\<c-g>")
1789+
call assert_match('Col 87 of 144; Line 2 of 2; Word 1 of 1; Byte 88 of 146', a)
1790+
17691791
" Test for gI
17701792
norm! gIfoo
1771-
call assert_equal(['', 'fooabcdefghijk lmno0123456789AMNOPQRSTUVWXYZ'], getline(1,'$'))
1793+
call assert_equal(['', 'foo0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'], getline(1,'$'))
17721794

17731795
" Test for gi
17741796
wincmd c

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ static char *(features[]) =
741741

742742
static int included_patches[] =
743743
{ /* Add new patch number below this line */
744+
/**/
745+
2231,
744746
/**/
745747
2230,
746748
/**/

0 commit comments

Comments
 (0)