Skip to content

New zy command: Yank without trailing whitespace#8292

Closed
chrisbra wants to merge 4 commits intovim:masterfrom
chrisbra:normal_zy_command
Closed

New zy command: Yank without trailing whitespace#8292
chrisbra wants to merge 4 commits intovim:masterfrom
chrisbra:normal_zy_command

Conversation

@chrisbra
Copy link
Member

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.

@chrisbra chrisbra force-pushed the normal_zy_command branch from 5de2955 to 6bbec6a Compare May 30, 2021 20:32
@codecov
Copy link

codecov bot commented May 30, 2021

Codecov Report

Merging #8292 (ccc32f4) into master (2fa9384) will decrease coverage by 1.85%.
The diff coverage is 95.93%.

❗ Current head ccc32f4 differs from pull request most recent head e19f2a0. Consider uploading reports for the commit e19f2a0 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8292      +/-   ##
==========================================
- Coverage   89.58%   87.73%   -1.86%     
==========================================
  Files         148      148              
  Lines      167049   163420    -3629     
==========================================
- Hits       149659   143376    -6283     
- Misses      17390    20044    +2654     
Flag Coverage Δ
huge-clang-none ?
huge-gcc-none ?
huge-gcc-testgui 87.69% <95.93%> (+0.14%) ⬆️
huge-gcc-unittests 2.50% <1.94%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
src/blowfish.c 87.60% <ø> (ø)
src/gui.c 60.62% <0.00%> (-1.61%) ⬇️
src/map.c 90.30% <ø> (-1.19%) ⬇️
src/menu.c 91.28% <ø> (-1.37%) ⬇️
src/misc2.c 90.56% <ø> (-1.42%) ⬇️
src/os_unix.c 68.84% <ø> (-3.12%) ⬇️
src/search.c 92.18% <ø> (-0.48%) ⬇️
src/sound.c 95.87% <ø> (+1.87%) ⬆️
src/time.c 94.96% <ø> (-0.04%) ⬇️
src/mouse.c 48.69% <28.57%> (-39.01%) ⬇️
... and 183 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...e19f2a0. Read the comment docs.

@brammool
Copy link
Contributor

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.

@chrisbra
Copy link
Member Author

oh, where did my test go?

@chrisbra
Copy link
Member Author

looks like test failures are unrelated.

@brammool brammool closed this in 544a38e Jun 10, 2021
@Shane-XB-Qian

This comment was marked as off-topic.

@chrisbra
Copy link
Member Author

chrisbra commented Jun 11, 2021

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

This comment was marked as off-topic.

janlazo added a commit to janlazo/neovim that referenced this pull request Jun 11, 2021
Problem:    Cannot yank a block without trailing spaces.
Solution:   Add the "zy" command. (Christian Brabandt, closes vim/vim#8292)
vim/vim@544a38e
@chrisbra chrisbra deleted the normal_zy_command branch June 22, 2021 17:14
kraftwerk28 pushed a commit to kraftwerk28/neovim that referenced this pull request Jun 25, 2021
Problem:    Cannot yank a block without trailing spaces.
Solution:   Add the "zy" command. (Christian Brabandt, closes vim/vim#8292)
vim/vim@544a38e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants