A.J.Mechelynck wrote:
Nikolaos A. Patsopoulos wrote:
Hi,

I'm trying to delete several lines from the beginning of file till the appearance of a specific pattern, without deleting the pattern. I have used the following command:

:1,/Citations: /d/e-10

but the offset doesn't work.

Thanks in advance,

Nikos


Well, I suppose

    gg/Citations: /

would put the cursor on the first occurrence of the pattern from the start of the file (assuming that it isn't at the very start of the file, in which case there's nothing to do).

(a) if the pattern is at the start of a line:

    :1,.-1d

(b) otherwise

    hdgg

will delete everything preceding it to the start of the file.

Breakup of the commands:

    :    ex-command
    1,    from first line
    .-1    to current line minus 1
    d    delete lines (the :d[elete] command)

    h    left one character
    dgg    delete to start of file.
        ( d{motion} is a Normal-mode command
          and gg moves to start of file )

That's for doing it manually, on one file, at the command-line. Now let's assume you wanted to do it on all *.txt files in the current directory and everywhere below it. You could then first source the following scriptlet:

    function DeletePreamble(pattern)
        " search from and including start-of-file
        normal gg
        let matchline = search(a:pattern, 'c')
        " if not found, there's nothing to do
        if matchline == 0
            return
        endif
        " pattern was found in line(matchline)
        " the cursor is now on the first char of the match
        if col('.') == 1
            " match found in column 1
            if matchline == 1
                " match at start of file: nothing to do
                return
            else
                " delete all preceding lines (linewise)
                1,.-1d
            endif
        else
            " match not at start of line
            " delete from preceding character
            " to start of file (characterwise)
            normal hdgg
        endif
    endfunction

and then call it as:

    :args ./**/*.txt
    :argdo call DeletePreamble('Citations: ') | update


Best regards,
Tony.


That looks great!!!

I'll take some time to study it thoroughly.

Thanks!!

Reply via email to