Is there a way to split a line automatically like awk would?

Given "A quick brown fox jumped over ",
    awk '{print $3}' ... ==> brown

Well, Vim supports passing a range of lines through awk, so assuming you have awk available, you can just do

        :%!awk '{print $3}'

Fairly straight-forward and readable, though as mentioned, not entirely a "vim" solution, as it uses awk to do the heavy lifting.

or like in perl
    split(':',$line)...

Vim also has (or at least *can* have, if built that way) a built-in perl interpreter. Not being a perl wonk, I'm afraid I don't know the ins and outs of using perl within vim. The python version would look something like

        :python current.line=current.line.split[2]

(my version here doesn't have either perl or python built in)

I'd like to do within vim something like
    :s/{some notation}/\3
without having to define the pattern

Others on the list have given the canonical, though rather opaque/verbose vim solution(s) which unfortunately do require the expression to be written out.

Theoretically, one could wrap the logic in a function that 'exec'ed a ":s" statement that it built on the fly, something like this purely untested

function foo(delim, pos) range
exec a:firstline.','.a.lastline.'s/\%([^'.a:delim.']*'.a:delim.'\)\{'.(a:pos-1).'}\([^'.a:delim.']*\).*'/\1'
endfunc

Various levels of escaping may need to be tweaked, and I'm not sure I've got the range syntax correctly. However, this theoretical function would then be called with

        :%call foo(' ', 3)

or

        :'<,'>call foo(':', 2)

It doesn't gracefully handle certain delimiters such as forward and backward slashes, right square brackets, multi-character delimiters, etc.

Just a few more ideas to add to your stockpile.  Hope they help,

-tim










Reply via email to