How do I delete from the end of the line through space (up to) the first word?

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

How do I delete from the end of the line through space (up to) the first word?

Grant Taylor-2
How do I delete from the end of the line (where the cursor is at)
through the space after the first word?

example.net <-- actually important
different.example.com <-- not actually important

What is the optimal way to delete from (and including) the t at the end
of the line up through the space after the first Word?

I'm looking for something functionally comparable to D when the cursor
is on the space after the first word, just coming from the opposite
direction.

diW works and multiple dot repetitions get the job done.  But i feel
like there is a better method.



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Tim Chase
On 2018-09-04 16:24, 'Grant Taylor' via vim_use wrote:

> How do I delete from the end of the line (where the cursor is at)
> through the space after the first word?
>
> example.net <-- actually important
> different.example.com <-- not actually important
>
> What is the optimal way to delete from (and including) the t at the
> end of the line up through the space after the first Word?
>
> I'm looking for something functionally comparable to D when the
> cursor is on the space after the first word, just coming from the
> opposite direction.
>
> diW works and multiple dot repetitions get the job done.  But i
> feel like there is a better method.

I'm having a little trouble parsing (1) where the cursor is (it
sounds like you're describing it at the last "t" of the first line)
and (2) what you want the final results to be.  Do you want

  example.net <-- actually importan.example.com <-- not actually important

or

  example.net <-- actually importan<-- not actually important

Or are the "<-- ..." meta-commentary and not actually content in the
file?  It would help to have unannotated "before" and "after" and
"where is the cursor".

Also, is this a one-off thing where you're trying to optimize
something manually, or should this be automated across all lines (or
lines matching some condition) in the file?  (noting that a :s
command can be repeated with either "@:" or "&" or "g&" depending on
intent)

-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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Gary Johnson-4
In reply to this post by Grant Taylor-2
On 2018-09-04, 'Grant Taylor' via vim_use wrote:

> How do I delete from the end of the line (where the cursor is at)
> through the space after the first word?
>
> example.net <-- actually important
> different.example.com <-- not actually important
>
> What is the optimal way to delete from (and including) the t at the
> end of the line up through the space after the first Word?
>
> I'm looking for something functionally comparable to D when the
> cursor is on the space after the first word, just coming from the
> opposite direction.
>
> diW works and multiple dot repetitions get the job done.  But i feel
> like there is a better method.

The best way to move to the first space on a line is to move to the
start of the line, then find the next space to the right.  The best
solution to your problem that I know of would be this:

    ^f D

Any other solution that tries to find the first space by working
backwards from the end of the line is going to require more typing,
unless you create a macro for it.

Regards,
Gary

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Grant Taylor-2
In reply to this post by Tim Chase
On 09/04/2018 05:16 PM, Tim Chase wrote:
> I'm having a little trouble parsing (1) where the cursor is (it
> sounds like you're describing it at the last "t" of the first line)

Yes, the cursor is on the last t at the last character of the line.

> (2) what you want the final results to be.  Do you want
>
>    example.net <-- actually importan.example.com <-- not actually important
>
> or
>
>    example.net <-- actually importan<-- not actually important

No, I want to end up with the following:

example.net
different.example.com

> Or are the "<-- ..." meta-commentary and not actually content in the file?

No, the actual content includes the ""arrow and what you're calling
meta-commentary.  That (and the proceeding space) are what I wanted to
remove.

> It would help to have unannotated "before" and "after" and "where is
> the cursor".

Before:

"""
example.net <-- actually important
different.example.com <-- not actually important
"""

After:
"""
example.net
different.example.com
"""

Background is I was copying and pasting content from an email (including
annotations) from a friend about domains I'm configuring for him.  I
wanted to get just the domain names first \W of the line and enter them
into my /etc/mail/local-host-names file.

I was editing /etc/mail/local-host-names in vim, copied and pasted the
content from email into the terminal, and wanted to remove the annotations.

> Also, is this a one-off thing where you're trying to optimize something
> manually, or should this be automated across all lines (or lines matching
> some condition) in the file?  (noting that a :s command can be repeated
> with either "@:" or "&" or "g&" depending on intent)

This was a one-off.  But I would like to learn the vim methodology to
play Vim Golf next time I need to do it.

I have frequently (monthly if not more often) needed to do a delete with
something similar to a "gE", but stop before the word.



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Tim Chase
In reply to this post by Gary Johnson-4
On 2018-09-04 16:27, Gary Johnson wrote:
>     ^f D
>
> Any other solution that tries to find the first space by working
> backwards from the end of the line is going to require more typing,
> unless you create a macro for it.

If "^f D" (or "+f D") works, then one can do

  :s/ .*

to delete the first one and then use "&" on each subsequent line to
delete if they need to be done manually.  Or you can do it for the
whole file

  :%s/ .*

Or just lines matching a pattern

  :g/example.com/s/ .*

-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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Grant Taylor-2
In reply to this post by Gary Johnson-4
On 09/04/2018 05:27 PM, Gary Johnson wrote:
> The best way to move to the first space on a line is to move to the start
> of the line, then find the next space to the right.  The best solution
> to your problem that I know of would be this:
>
>     ^f D

Interesting idea.  It makes perfect sense.

It also solves the problem as I described it.

I would still like to know how to delete backwards {t,T}o a word, not
{f,F} on a character I'd like to keep.

I've got all sorts of things like the following:

aaa bbbb ccccc ddd eeeeeee ffff

Where the cursor is on the last f and I want to delete from the last f
up to but not including the last d.  I.e. "Td".

But, "Td" leaves the last f there.

I suppose I could use "Tdx".

I figured that there was a Vim movement that I'm not aware of.

> Any other solution that tries to find the first space by working
> backwards from the end of the line is going to require more typing,
> unless you create a macro for it.

Ya.  I'm not as worried about the "first space" as I am going backward
and deleting as I go with something like gE.



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Tim Chase
On 2018-09-04 17:52, 'Grant Taylor' via vim_use wrote:

> On 09/04/2018 05:27 PM, Gary Johnson wrote:
> > The best way to move to the first space on a line is to move to
> > the start of the line, then find the next space to the right.
> > The best solution to your problem that I know of would be this:
> >
> >     ^f D  
>
> Interesting idea.  It makes perfect sense.
>
> It also solves the problem as I described it.
>
> I would still like to know how to delete backwards {t,T}o a word,
> not {f,F} on a character I'd like to keep.
>
> I've got all sorts of things like the following:
>
>   aaa bbbb ccccc ddd eeeeeee ffff
>
> Where the cursor is on the last f and I want to delete from the
> last f up to but not including the last d.  I.e. "Td".

But "dTb" does keep the character under the cursor.  If you're at
the end of the line and *do* want to delete the character under the
cursor, you can use

  vTbd

to use character-wise visual mode or

  TbD

to position first and then delete to the end of the line (slightly
different behaviors if the cursor isn't at the end of the line).

There are lots of options depending on how much you want to do
manually vs. automatically, as well as how easy it is to identify the
matches in question.

-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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Tony Mechelynck
In reply to this post by Grant Taylor-2
On Wed, Sep 5, 2018 at 1:44 AM 'Grant Taylor' via vim_use
<[hidden email]> wrote:

> Before:
>
> """
> example.net <-- actually important
> different.example.com <-- not actually important
> """
>
> After:
> """
> example.net
> different.example.com
> """

You could do that with a substitute: to delete " <--" and all that
follows it to the end of the line anywhere in the file:

:%s/ <--.*$//

Remove the % sign to do it on the current line only, or for a Visual
block, hit : in Visual mode, you'll see :'<,'> on the command-line,
and fill it up starting at the s of the :s[ubstitute] command, to get

:'<,'>s/ <--.*$//

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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Grant Taylor-2
In reply to this post by Tim Chase
On 09/04/2018 06:45 PM, Tim Chase wrote:
> But "dTb" does keep the character under the cursor.  If you're at the
> end of the line and *do* want to delete the character under the cursor,
> you can use
>
>    vTbd

Interesting.

> to use character-wise visual mode or
>
>    TbD

I like that.  It takes a moment to wrap my head round.

It doesn't work as such with my original example of the annotated
domains.  (Because the "b" is not in them.)

That being said, I can modify it a little bit:

    F D

Or more specifically, a count version of it:

    4F D

I feel like that is within the spirit of what I was originally asking
about.  Namely something to arbitrarily delete backwards to words.

> to position first and then delete to the end of the line (slightly
> different behaviors if the cursor isn't at the end of the line).

Yep.

> There are lots of options depending on how much you want to do manually
> vs. automatically, as well as how easy it is to identify the matches
> in question.

I know.

I'm mainly asking about how I can clean up / mung arbitrary data that
I'm working with.



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Tim Chase
On 2018-09-05 10:59, 'Grant Taylor' via vim_use wrote:
> That being said, I can modify it a little bit:
>
>     F D
>
> Or more specifically, a count version of it:
>
>     4F D

Also, note that once you've done a f/F/t/T command, you can save a
character (and some thinking) by using "," and ";" to navigate to the
next/previous one, prefixing that with a count.

  :help ,
  :help ;

Some people remap <leader> to one of these keys, but I use them
frequently enough that I recoil at the thought of losing them :-)

So the first one could be

  F D

and then subsequent ones could be

  ,D

or

  ;D

depending on where your cursor is.

-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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Grant Taylor-2
On 09/05/2018 11:11 AM, Tim Chase wrote:
> Also, note that once you've done a f/F/t/T command, you can save a
> character (and some thinking) by using "," and ";" to navigate to the
> next/previous one, prefixing that with a count.
>
>    :help ,
>    :help ;

Ya.  I think it moves the thinking from "how many times do I want to do
the following action?" (count / multiplier) to "do I want to do the last
action again?".  Depending on what the context is, sometimes the count /
multiplier ends up being less mental load.

> Some people remap <leader> to one of these keys, but I use them frequently
> enough that I recoil at the thought of losing them :-)

Fair enough.

> So the first one could be
>
>    F D
>
> and then subsequent ones could be
>
>    ,D
>
> or
>
>    ;D
>
> depending on where your cursor is.

ACK



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Arun
In reply to this post by Grant Taylor-2


On Tue, Sep 4, 2018 at 4:51 PM 'Grant Taylor' via vim_use <[hidden email]> wrote:
On 09/04/2018 05:27 PM, Gary Johnson wrote:
> The best way to move to the first space on a line is to move to the start
> of the line, then find the next space to the right.  The best solution
> to your problem that I know of would be this:
>
>     ^f D

Interesting idea.  It makes perfect sense.

It also solves the problem as I described it.

I would still like to know how to delete backwards {t,T}o a word, not
{f,F} on a character I'd like to keep.

I've got all sorts of things like the following:

aaa bbbb ccccc ddd eeeeeee ffff

Where the cursor is on the last f and I want to delete from the last f
up to but not including the last d.  I.e. "Td".

But, "Td" leaves the last f there.

Could not you just move right one character and do the "Td" (kind of like
"Tdx", but moving right can be done easily with the space bar). Anyway,
to move beyond the last character in line, though, you will have to set:
    :set virtualedit=onemore

Alternatively, have you considered recording/macros?
    :h complex-repeat

You could just record a set of key strokes using the "q" command and
recall the sequence whenever you want.

Regards,
-Arun


I suppose I could use "Tdx".

I figured that there was a Vim movement that I'm not aware of.

> Any other solution that tries to find the first space by working
> backwards from the end of the line is going to require more typing,
> unless you create a macro for it.

Ya.  I'm not as worried about the "first space" as I am going backward
and deleting as I go with something like gE.



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Grant Taylor-2
On 09/05/2018 12:38 PM, Arun wrote:
> Could not you just move right one character and do the "Td" (kind of like
> "Tdx", but moving right can be done easily with the space bar).

Um …

> Anyway, to move beyond the last character in line, though, you will have
> to set:
>
>      :set virtualedit=onemore

I'll have to check that out.  I do think that would work.

Of course, when it comes to Vim Golf, it would be comparable.

  TdD

-or-

$TdD

Is effectively the same number of characters (strokes).

Tdx

> Alternatively, have you considered recording/macros?

I use macros quite frequently.  I wouldn't have considered them for this
particular use case as it was two lines of data I was cleaning up.

If I need to do something multiple times and I have a wrote process to
use, I tend to reach for the macro hammer quite readily.

> You could just record a set of key strokes using the "q" command and
> recall the sequence whenever you want.

Agreed.  It's just not proper for this use case.  And it doesn't answer
the question of if there is a different method that is like a backwards dw.



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

Grant Taylor-2
In reply to this post by Arun
On 09/05/2018 12:38 PM, Arun wrote:
> Could not you just move right one character and do the "Td" (kind of like
> "Tdx", but moving right can be done easily with the space bar). Anyway,
> to move beyond the last character in line, though, you will have to set:
>      :set virtualedit=onemore

Thank you for sharing virtualedit=onemore Arun.  I've used it multiple
times and have found it very helpful.  :-)



--
Grant. . . .
unix || die

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I delete from the end of the line through space (up to) the first word?

meine van essen
In reply to this post by Grant Taylor-2
On Tue, Sep 04, 2018 at 04:24:00PM -0600, 'Grant Taylor' via vim_use wrote:
> How do I delete from the end of the line (where the cursor is at)
> through the space after the first word?
>
> example.net <-- actually important
> different.example.com <-- not actually important

in a macro: 0WhD
j.

//meine

--
--
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].
For more options, visit https://groups.google.com/d/optout.