substitute() regex trim last newline from register ?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

substitute() regex trim last newline from register ?

M Kelly
Hi,

In visual-line mode when I yank to the * register and then outside of vim paste - I am getting a newline at the end of the selection.
This does not happen in visual block or char mode.
Does anyone know of a way to load into * reg the same selection but have the last newline trimmed off ?
ie something like:
let @* = substitute(@a, "\(.*\)\\n/\1/", "", "")

thx for everything vim,
-m

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/5811a14e-257e-4048-8639-beebcd1c5af6%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: substitute() regex trim last newline from register ?

Tony Mechelynck
On Thu, Jan 16, 2020 at 2:29 AM M Kelly <[hidden email]> wrote:

>
> Hi,
>
> In visual-line mode when I yank to the * register and then outside of vim paste - I am getting a newline at the end of the selection.
> This does not happen in visual block or char mode.
> Does anyone know of a way to load into * reg the same selection but have the last newline trimmed off ?
> ie something like:
> let @* = substitute(@a, "\(.*\)\\n/\1/", "", "")
>
> thx for everything vim,
> -m

When yanking linewise, what you get is lines, and each of them has a
proper end-of-line. This way, when you paste it, it will be pasted
linewise, with line breaks between it and whatever happens before and
after it.

To yank without an end-of-line at the end, yank characterwise from the
first character of the first desired line (or from the first character
you want to yank even if it is in the middle of a line) to the last
character you want to yank. This way, if you paste in the middle of a
line, you won't get line breaks before and after the pasted text.

See also :help setreg()

Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXtFYM-HgyE%3DGUt-kAZJA-RFBPx%3DHaEK9ceGrgy9dTamag%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: substitute() regex trim last newline from register ?

Tim Chase
In reply to this post by M Kelly
On 2020-01-15 17:29, M Kelly wrote:
> Hi,
>
> In visual-line mode when I yank to the * register and then outside
> of vim paste - I am getting a newline at the end of the selection.
> This does not happen in visual block or char mode.
> Does anyone know of a way to load into * reg the same selection but
> have the last newline trimmed off ?
> ie something like:
> let @* = substitute(@a, "\(.*\)\\n/\1/", "", "")

Pretty sure you're 99% of the way there:

 let @+ = substitute(@a, '\n\+$', '', '')

If you want all potential trailing whitespace (spaces, tabs, and
newlines)

 let @+ = substitute(@a, '\_s\+$', '', '')

should do the trick.

-tim



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200115194741.6171b3b6%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: substitute() regex trim last newline from register ?

M Kelly
In reply to this post by Tony Mechelynck
Tony, hi

ok, thanks.  I can use 0 v $ y.  But V y is handy/quick ...
Inside of vim its all great, V works perfectly - its just I wanted to automatically remove the last newline from "* if possible, 
so that a V y and then outside of vim paste doesn't have the extra newline.
I just wondered if I could regex trim the last newline of "* or of "a and put it into "*

thanks always for your help,
-m

On Wednesday, January 15, 2020 at 8:44:44 PM UTC-5, Tony Mechelynck wrote:
On Thu, Jan 16, 2020 at 2:29 AM M Kelly <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="FRsqiMAODAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mckel...@...> wrote:

>
> Hi,
>
> In visual-line mode when I yank to the * register and then outside of vim paste - I am getting a newline at the end of the selection.
> This does not happen in visual block or char mode.
> Does anyone know of a way to load into * reg the same selection but have the last newline trimmed off ?
> ie something like:
> let @* = substitute(@a, "\(.*\)\\n/\1/", "", "")
>
> thx for everything vim,
> -m

When yanking linewise, what you get is lines, and each of them has a
proper end-of-line. This way, when you paste it, it will be pasted
linewise, with line breaks between it and whatever happens before and
after it.

To yank without an end-of-line at the end, yank characterwise from the
first character of the first desired line (or from the first character
you want to yank even if it is in the middle of a line) to the last
character you want to yank. This way, if you paste in the middle of a
line, you won't get line breaks before and after the pasted text.

See also :help setreg()

Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/9b5f482a-1037-4c4d-8ed3-252cf2f2b501%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: substitute() regex trim last newline from register ?

M Kelly
In reply to this post by Tim Chase
Tim, hi

ok, awesome.
I think this works now -

"aygv<Esc>:let @* = substitute(@a, "\\_s\\+$", "", "")<CR>

thx so much!
-m

On Wednesday, January 15, 2020 at 8:47:54 PM UTC-5, Tim Chase wrote:
On 2020-01-15 17:29, M Kelly wrote:
> Hi,
>
> In visual-line mode when I yank to the * register and then outside
> of vim paste - I am getting a newline at the end of the selection.
> This does not happen in visual block or char mode.
> Does anyone know of a way to load into * reg the same selection but
> have the last newline trimmed off ?
> ie something like:
> let @* = substitute(@a, "\(.*\)\\n/\1/", "", "")

Pretty sure you're 99% of the way there:

 let @+ = substitute(@a, '\n\+$', '', '')

If you want all potential trailing whitespace (spaces, tabs, and
newlines)

 let @+ = substitute(@a, '\_s\+$', '', '')

should do the trick.

-tim



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/80727860-1e5c-4c05-9b4f-c4a638d98f32%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: substitute() regex trim last newline from register ?

Tim Chase
In reply to this post by M Kelly
On 2020-01-15 18:08, M Kelly wrote:
> V y is handy/quick

Just in case you haven't encountered it, you can make that even
quicker by just using

  Y

:-)

-tim


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200115203902.368cf2b4%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: substitute() regex trim last newline from register ?

Tony Mechelynck
In reply to this post by M Kelly
On Thu, Jan 16, 2020 at 3:08 AM M Kelly <[hidden email]> wrote:

>
> Tony, hi
>
> ok, thanks.  I can use 0 v $ y.  But V y is handy/quick ...
> Inside of vim its all great, V works perfectly - its just I wanted to automatically remove the last newline from "* if possible,
> so that a V y and then outside of vim paste doesn't have the extra newline.
> I just wondered if I could regex trim the last newline of "* or of "a and put it into "*
>
> thanks always for your help,
> -m

What's wrong with
    :call setreg('*', getreg('*'), 'c')
(see "see also" at he end of my previous message)?

Best regards,
Tony.

>
> On Wednesday, January 15, 2020 at 8:44:44 PM UTC-5, Tony Mechelynck wrote:
>>
>> On Thu, Jan 16, 2020 at 2:29 AM M Kelly <[hidden email]> wrote:
>> >
>> > Hi,
>> >
>> > In visual-line mode when I yank to the * register and then outside of vim paste - I am getting a newline at the end of the selection.
>> > This does not happen in visual block or char mode.
>> > Does anyone know of a way to load into * reg the same selection but have the last newline trimmed off ?
>> > ie something like:
>> > let @* = substitute(@a, "\(.*\)\\n/\1/", "", "")
>> >
>> > thx for everything vim,
>> > -m
>>
>> When yanking linewise, what you get is lines, and each of them has a
>> proper end-of-line. This way, when you paste it, it will be pasted
>> linewise, with line breaks between it and whatever happens before and
>> after it.
>>
>> To yank without an end-of-line at the end, yank characterwise from the
>> first character of the first desired line (or from the first character
>> you want to yank even if it is in the middle of a line) to the last
>> character you want to yank. This way, if you paste in the middle of a
>> line, you won't get line breaks before and after the pasted text.
>>
>> See also :help setreg()
>>
>> Best regards,
>> Tony.
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/9b5f482a-1037-4c4d-8ed3-252cf2f2b501%40googlegroups.com.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXsvqS_awyNOLXxb3WzsKWPw%3Do4-%3Dh6qwY%3D6t4H4qkyUSQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: substitute() regex trim last newline from register ?

M Kelly
Tony, hi

I ended up using getregtype("*") along with the @*=substitute() to get a perfect solution for my use.
Thank you again for this info.

take care,
-m

On Wednesday, January 15, 2020 at 11:53:24 PM UTC-5, Tony Mechelynck wrote:
On Thu, Jan 16, 2020 at 3:08 AM M Kelly <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZHYiEgwZDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mckel...@...> wrote:

>
> Tony, hi
>
> ok, thanks.  I can use 0 v $ y.  But V y is handy/quick ...
> Inside of vim its all great, V works perfectly - its just I wanted to automatically remove the last newline from "* if possible,
> so that a V y and then outside of vim paste doesn't have the extra newline.
> I just wondered if I could regex trim the last newline of "* or of "a and put it into "*
>
> thanks always for your help,
> -m

What's wrong with
    :call setreg('*', getreg('*'), 'c')
(see "see also" at he end of my previous message)?

Best regards,
Tony.

>
> On Wednesday, January 15, 2020 at 8:44:44 PM UTC-5, Tony Mechelynck wrote:
>>
>> On Thu, Jan 16, 2020 at 2:29 AM M Kelly <[hidden email]> wrote:
>> >
>> > Hi,
>> >
>> > In visual-line mode when I yank to the * register and then outside of vim paste - I am getting a newline at the end of the selection.
>> > This does not happen in visual block or char mode.
>> > Does anyone know of a way to load into * reg the same selection but have the last newline trimmed off ?
>> > ie something like:
>> > let @* = substitute(@a, "\(.*\)\\n/\1/", "", "")
>> >
>> > thx for everything vim,
>> > -m
>>
>> When yanking linewise, what you get is lines, and each of them has a
>> proper end-of-line. This way, when you paste it, it will be pasted
>> linewise, with line breaks between it and whatever happens before and
>> after it.
>>
>> To yank without an end-of-line at the end, yank characterwise from the
>> first character of the first desired line (or from the first character
>> you want to yank even if it is in the middle of a line) to the last
>> character you want to yank. This way, if you paste in the middle of a
>> line, you won't get line breaks before and after the pasted text.
>>
>> See also :help setreg()
>>
>> Best regards,
>> Tony.
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit <a href="http://www.vim.org/maillist.php" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.vim.org%2Fmaillist.php\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH_i_vTWvOy2_ml2WNZwm9vDnSvaA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.vim.org%2Fmaillist.php\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH_i_vTWvOy2_ml2WNZwm9vDnSvaA&#39;;return true;">http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZHYiEgwZDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">vim...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/vim_use/9b5f482a-1037-4c4d-8ed3-252cf2f2b501%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/vim_use/9b5f482a-1037-4c4d-8ed3-252cf2f2b501%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/vim_use/9b5f482a-1037-4c4d-8ed3-252cf2f2b501%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/vim_use/9b5f482a-1037-4c4d-8ed3-252cf2f2b501%40googlegroups.com.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/df776c0a-bee4-4171-a456-0fa3cfff06fa%40googlegroups.com.