[vim/vim] New zy command: Yank without trailing whitespace (#8292)

189 views
Skip to first unread message

Christian Brabandt

unread,
May 30, 2021, 4:31:56 PM5/30/21
to vim/vim, Subscribed

So with 2fa9384 we can now paste without adding trailing whitespaces.

So let's add an new command 'zy' to yank text, but leave trailing whitespace out. This allows to yank whitespace delimited text like in a visual table:

column column2 col3
text /dir trailing text
text /longer/dir/here trailing text
text /even/longer/dir/here trailing text

So you can now block select using Ctrl-V the second column and use zy to only copy the non-whitespace characters as a new block.

This makes it easier to paste them later using e.g. zp.

Add a test and documentation to it.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/8292

Commit Summary

  • make zy yank contents but leave out whitespace at the end
  • zy: add new member to oap struct
  • zy: update documentation

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Christian Brabandt

unread,
May 30, 2021, 5:32:29 PM5/30/21
to vim/vim, Push

@chrisbra pushed 1 commit.

  • ff03d20 only change pnew, if whitespace is found


You are receiving this because you are subscribed to this thread.

View it on GitHub or unsubscribe.

codecov[bot]

unread,
May 30, 2021, 5:45:56 PM5/30/21
to vim/vim, Subscribed

Codecov Report

Merging #8292 (ff03d20) into master (2fa9384) will decrease coverage by 0.13%.
The diff coverage is 54.54%.

Impacted file tree graph

@@            Coverage Diff             @@

##           master    #8292      +/-   ##

==========================================

- Coverage   89.58%   89.45%   -0.14%     

==========================================

  Files         148      148              

  Lines      167049   164301    -2748     

==========================================

- Hits       149659   146978    -2681     

+ Misses      17390    17323      -67     
Flag Coverage Δ
huge-clang-none 91.19% <50.00%> (+2.40%) ⬆️
huge-gcc-none 89.04% <54.54%> (-0.01%) ⬇️
huge-gcc-testgui 87.53% <54.54%> (-0.01%) ⬇️
huge-gcc-unittests 2.50% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/register.c 91.71% <50.00%> (-0.26%) ⬇️
src/ops.c 93.66% <66.66%> (-0.56%) ⬇️
src/os_unix.c 70.99% <0.00%> (-0.97%) ⬇️
src/if_xcmdsrv.c 88.50% <0.00%> (-0.76%) ⬇️
src/textobject.c 91.69% <0.00%> (-0.71%) ⬇️
src/screen.c 85.80% <0.00%> (-0.68%) ⬇️
src/if_python.c 82.65% <0.00%> (-0.62%) ⬇️
src/bufwrite.c 84.17% <0.00%> (-0.60%) ⬇️
src/drawline.c 84.74% <0.00%> (-0.55%) ⬇️
src/move.c 93.77% <0.00%> (-0.54%) ⬇️
... and 107 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2fa9384...ff03d20. Read the comment docs.

Bram Moolenaar

unread,
May 31, 2021, 3:32:25 PM5/31/21
to vim/vim, Subscribed

As someone noticed, doing the same when deleting text, which is basically yanking the text first, "zd" cannot be used, it is an existing command. But perhaps that is OK, since when deleting the text you would still want the whole block to be deleted, only the yanking would omit the trailing spaces. So it could be a three step operation:

  • select the block, use the new zy command to yank the text without trailing spaces
  • gv to reselect the block
  • "_d delete without yanking

Still, that requires typing three commands. I don't see an obvious two-letter command we could use for this.

Christian Brabandt

unread,
Jun 10, 2021, 5:57:53 AM6/10/21
to vim/vim, Subscribed

oh, where did my test go?

Christian Brabandt

unread,
Jun 10, 2021, 7:38:50 AM6/10/21
to vim/vim, Push

@chrisbra pushed 1 commit.

  • 3e2e648 Include the forgotten Changes :(


You are receiving this because you are subscribed to this thread.

Christian Brabandt

unread,
Jun 10, 2021, 7:52:43 AM6/10/21
to vim/vim, Push

@chrisbra pushed 1 commit.

  • e19f2a0 Another test. Yank with spaces inside the block


You are receiving this because you are subscribed to this thread.

Christian Brabandt

unread,
Jun 10, 2021, 9:35:51 AM6/10/21
to vim/vim, Subscribed

looks like test failures are unrelated.

Bram Moolenaar

unread,
Jun 10, 2021, 1:39:54 PM6/10/21
to vim/vim, Subscribed

Closed #8292 via 544a38e.

Shane-XB-Qian

unread,
Jun 10, 2021, 11:09:09 PM6/10/21
to vim/vim, Subscribed

why not just whatever what kind of visual block yank ('y' or 'zy'), but as long as paste by 'zp' then no tail?
if so:

  • the different just was the 'p' or 'zp' to choose when you were doing the 'paste', but sometime you may not know what kind of 'yank' should be done at first.
  • the cmd also is clear and simplest, no need another 'zy' cmd.

Christian Brabandt

unread,
Jun 11, 2021, 2:21:06 AM6/11/21
to vim/vim, Subscribed

There are 2 different use cases:

Take a block like this:

aa
bbbbb
ccc

You can block select this <C-V>2j$ and you will have only the actual content (e.g. different line length in your register) when yanking. However, when pasting a block using p Vim will add padding (trailing white space) so that a rectangular block is pasted (so padding is added). zp will prevent adding the padding on paste, however it does not look at the actual register content, so if you have trailing whitespace in the register, it will get pasted as expected.

You have a whitespace-delimited block like this:

aaaa      text to yank                            ccccc
aa          some more text to yank         ccccccc
aaaaa    this text to yank is very long   cccccccc

Now, if you visual select column 2 using <c-v>2jtc you will yank the complete block, including the delimiting white-space. Possibly, in that case you are more interested in just the text and not in the whitespace at the end. If you care about the whitespace, use y to yank and it will be included in the register and later be put when using the p command family (even with zp). But you may have to clean up the trailing whitespace after pasting.

This can be prevented by using the zy command. It will only copy the actual content and leave the trailing whitespace out. So your register contains a block that in the end looks similar to what is in the register after example 1. However, when pasting using the p command Vim will add padding and make it a rectangular block (see example 1), except when using zp you will again only put the actual register content.

This case also happens for example 1, when you have :set virtualedit=block set.

but sometime you may not know what kind of 'yank' should be done at first.

I think the user knows what he actually wants so can decide depending on his actual requirements, so this is okay.

the cmd also is clear and simplest, no need another 'zy' cmd.

As shown, there are difference use cases, that each command covers, so a zy and zp are actually needed.

Shane-XB-Qian

unread,
Jun 11, 2021, 2:45:25 AM6/11/21
to vim/vim, Subscribed

the example 1 if <c-v>2j$$ whatever 'y' or 'zy', but/then 'zp' looks could be no tail.
but example 2 if <c-v>2jtc, looks had to 'zy' first.
my opinion was whatever '$' or 'tc', just make 'zp' paste withOut tail.

Reply all
Reply to author
Forward
0 new messages