Wednesday, December 11, 2013

Re: Selecting an IPv4-address?

> In the case I use it btw. something like vt; (handling a dhcpd.conf)
> is the fastest way so far.

Event faster:

onoremap ; t;

which lets you use "d;", "y;", etc., as "dt;", "yt;".

And since we're having quite some fun (and automating repetitive behaviors is
one of the strong points of Vim), you can try:

let s:patt = '\%(\d\{1,3}\.\)\{3}\d\{1,3}'
function! s:findIP ()
let [l, c] = getpos(".")[1:2]
let c -= 1
let L = getline(l)
let diff = 10000
let e = 0
while 1
let [s, e] = [match(L, s:patt, e), matchend(L, s:patt, e)]
let [d1, d2] = [s-c, c-e]
if s <= -1
break
elseif d1 <= 0 && d2 <= 0
let best = [s, e]
break
else
let [d1, d2] = [abs(d1), abs(d2)]
if d1 <= diff || d2 <= diff
let best = [s, e]
let diff = d1 < d2 ? d1 : d2
endif
endif
endwhile
if exists("l:best")
call cursor(l, best[0]+1)
normal! v
call cursor(l, best[1]+1)
endif
endfunction

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.

(Note: the script was not thoroughly tested!)

Best,
Paul

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