Wednesday, December 11, 2013

Re: Selecting an IPv4-address?

On Wednesday, December 11, 2013 6:42:05 AM UTC-6, Paul Isambert wrote:
>
> let s:patt = '\%(\d\{1,3}\.\)\{3}\d\{1,3}'
>

[snip]

>
> onoremap <silent> I :<C-U>call <SID>findIP()<CR>
>
>
>
> This lets you use "dI", "yI", etc., to delete/yank/etc. the IP address that is
>
> the closer to the cursor. This way you don't have to move at the beginning of
>
> the address you want, simply as close to it as possible. If there's only one
>
> address on the current line, you don't have to move the cursor at all.
>

It looks like your complicated script just acts on the last search match, just like gn does built-in.

If you're going to bring regex into it, just do /\%(\d\{1,3}\.\)\{3}\d\{1,3} (or use ? instead of / if the match you want is before the cursor) and use ygn, dgn, cgn, etc. to operate on the match (in Vim 7.3.610 or higher). Then you can do it again with ygn to get the NEXT match, or if you deleted it or changed it or something the entire action is repeatable with '.'.

Example, using text:
--------------------
Mine: 192.168.1.101
Yours: 192.168.1.102
--------------------

Place cursor on "Mine". Type /\(\d\+\.\)\{3}\d\+ to select (very loosely) the first IP. Type dgn and then type either dgn again or just . (repeat command) to delete both IP addresses. Yes, I know this toy example could be easily done with :s. But more complicated examples very quickly get to the point where recording a simple macro using gn or gN is much easier than piecing together a complicated regular expression/substitute string for an :s command.

As Gary said, this depends on Vim 7.3.610 or higher. Thanks VERY much to Christian Brabandt for this great feature! I probably use it daily.

--
--
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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment