Wednesday, September 26, 2012

Re: help with rexpr please

On Tuesday, September 25, 2012 11:27:23 PM UTC-5, Patrick wrote:
> Hi
>
> This is my first post here.
>
> I am new to vim but making good progress through self study. I don't know how to represent a word though. I know there is a /w word character but it does not seem to do what I am trying to do.
>

Yes, it does. What did you try? It's \w by the way, not /w.

> Lets say i have four words and I want to delete 1, 3 and 4.
>
> delete_me1 leave_me delete_me2 delete_me 3
>
> could anyone help me represent this?
>

I assume the extra space in "delete_me 3" is unintentional.

Here's a lazy way:

s/^\w\+\s\+\(\w\+\).*/\1/

Explanation:

^ is start-of-line
\w\+ is one or more word characters (in other words, the first word)
\s\+ is the whitespace separating the first and second word
\(\w\+\) is one or more word characters (the second word), captured in a group for later use
.* matches the rest of the line starting from the first non-word character after the 2nd word.

Replace with \1, which is the 1st captured group, here being the desired 2nd word.


Here's a more exact method if the above is too liberal and matches things you don't want it to. Consult the help for an explanation:

s/\v^\w+\s+(\w+)(\s+\w+){2}$/\1/

--
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

No comments: