Skip to content

Commit 2fa9384

Browse files
chrisbrabrammool
authored andcommitted
patch 8.2.2914: cannot paste a block without adding padding
Problem: Cannot paste a block without adding padding. Solution: Add "zp" and "zP" which paste without adding padding. (Christian Brabandt, closes #8289)
1 parent d2ea7cf commit 2fa9384

File tree

8 files changed

+56
-10
lines changed

8 files changed

+56
-10
lines changed

runtime/doc/change.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,11 @@ inside of strings can change! Also see 'softtabstop' option. >
11261126
Using the mouse only works when 'mouse' contains 'n'
11271127
or 'a'.
11281128

1129+
["x]zp or *zp* *zP*
1130+
["x]zP Like "p" and "P", except without adding trailing spaces
1131+
when pasting a block. Thus the inserted text will not
1132+
always be a rectangle.
1133+
11291134
You can use these commands to copy text from one place to another. Do this
11301135
by first getting the text into a register with a yank, delete or change
11311136
command, then inserting the register contents with a put command. You can
@@ -1165,6 +1170,9 @@ a register, a paste on a visual selected area will paste that single line on
11651170
each of the selected lines (thus replacing the blockwise selected region by a
11661171
block of the pasted line).
11671172

1173+
Use |zP|/|zp| to paste a blockwise yanked register without appending trailing
1174+
spaces.
1175+
11681176
*blockwise-register*
11691177
If you use a blockwise Visual mode command to get the text into the register,
11701178
the block of text will be inserted before ("P") or after ("p") the cursor

runtime/doc/index.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,8 @@ tag char note action in Normal mode ~
864864
|zm| zm subtract one from 'foldlevel'
865865
|zn| zn reset 'foldenable'
866866
|zo| zo open fold
867+
|zp| zp paste in block-mode without trailing spaces
868+
|zP| zP paste in block-mode without trailing spaces
867869
|zr| zr add one to 'foldlevel'
868870
|zs| zs when 'wrap' off scroll horizontally to
869871
position the cursor at the start (left

src/normal.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,6 +2973,10 @@ nv_zet(cmdarg_T *cap)
29732973
}
29742974
break;
29752975

2976+
// "zp", "zP" in block mode put without addind trailing spaces
2977+
case 'P':
2978+
case 'p': nv_put(cap);
2979+
break;
29762980
#ifdef FEAT_FOLDING
29772981
// "zF": create fold command
29782982
// "zf": create fold operator
@@ -7418,11 +7422,13 @@ nv_put_opt(cmdarg_T *cap, int fix_indent)
74187422
}
74197423
else
74207424
dir = (cap->cmdchar == 'P'
7421-
|| (cap->cmdchar == 'g' && cap->nchar == 'P'))
7422-
? BACKWARD : FORWARD;
7425+
|| ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7426+
&& cap->nchar == 'P')) ? BACKWARD : FORWARD;
74237427
prep_redo_cmd(cap);
74247428
if (cap->cmdchar == 'g')
74257429
flags |= PUT_CURSEND;
7430+
else if (cap->cmdchar == 'z')
7431+
flags |= PUT_BLOCK_INNER;
74267432

74277433
if (VIsual_active)
74287434
{

src/register.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,7 @@ copy_yank_reg(yankreg_T *reg)
14971497
* "flags": PUT_FIXINDENT make indent look nice
14981498
* PUT_CURSEND leave cursor after end of new text
14991499
* PUT_LINE force linewise put (":put")
1500+
* PUT_BLOCK_INNER in block mode, do not add trailing spaces
15001501
*/
15011502
void
15021503
do_put(
@@ -1794,7 +1795,7 @@ do_put(
17941795
bd.textcol = 0;
17951796
for (i = 0; i < y_size; ++i)
17961797
{
1797-
int spaces;
1798+
int spaces = 0;
17981799
char shortline;
17991800

18001801
bd.startspaces = 0;
@@ -1845,12 +1846,16 @@ do_put(
18451846

18461847
yanklen = (int)STRLEN(y_array[i]);
18471848

1848-
// calculate number of spaces required to fill right side of block
1849-
spaces = y_width + 1;
1850-
for (j = 0; j < yanklen; j++)
1851-
spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
1852-
if (spaces < 0)
1853-
spaces = 0;
1849+
if ((flags & PUT_BLOCK_INNER) == 0)
1850+
{
1851+
// calculate number of spaces required to fill right side of
1852+
// block
1853+
spaces = y_width + 1;
1854+
for (j = 0; j < yanklen; j++)
1855+
spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
1856+
if (spaces < 0)
1857+
spaces = 0;
1858+
}
18541859

18551860
// insert the new text
18561861
totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;

src/testdir/test_normal.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ endfunc
595595
" Test for errors with z command
596596
func Test_normal_z_error()
597597
call assert_beeps('normal! z2p')
598-
call assert_beeps('normal! zp')
598+
call assert_beeps('normal! zq')
599599
endfunc
600600

601601
func Test_normal15_z_scroll_vert()

src/testdir/test_visual.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,4 +1044,26 @@ func Test_visual_put_in_block()
10441044
bwipe!
10451045
endfunc
10461046

1047+
func Test_visual_put_in_block_using_zp()
1048+
new
1049+
" paste using zP
1050+
call setline(1, ['/path;text', '/path;text', '/path;text', '',
1051+
\ '/subdir',
1052+
\ '/longsubdir',
1053+
\ '/longlongsubdir'])
1054+
exe "normal! 5G\<c-v>2j$y"
1055+
norm! 1Gf;zP
1056+
call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1057+
%d
1058+
" paste using zP
1059+
call setline(1, ['/path;text', '/path;text', '/path;text', '',
1060+
\ '/subdir',
1061+
\ '/longsubdir',
1062+
\ '/longlongsubdir'])
1063+
exe "normal! 5G\<c-v>2j$y"
1064+
norm! 1Gf;hzp
1065+
call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1066+
bwipe!
1067+
endfunc
1068+
10471069
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2914,
753755
/**/
754756
2913,
755757
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ extern int (*dyn_libintl_wputenv)(const wchar_t *envstring);
10681068
#define PUT_LINE 8 // put register as lines
10691069
#define PUT_LINE_SPLIT 16 // split line for linewise register
10701070
#define PUT_LINE_FORWARD 32 // put linewise register below Visual sel.
1071+
#define PUT_BLOCK_INNER 64 // in block mode, do not add trailing spaces
10711072

10721073
// flags for set_indent()
10731074
#define SIN_CHANGED 1 // call changed_bytes() when line changed

0 commit comments

Comments
 (0)