Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I spend most of my time on Windows 8 and use gvim -c as a sed replacement. I think I can get sed with Cygwin, but I hope that I can do this with gvim -c.

I have a directory of tab-delimited .txt files, and some have a header (as follows) that I want to remove.

Definition of the Ultimate Owner :                                                                                                                                                  
- minimum percentage that must characterize the path from a subject company up to its Ultimate owner : 25.01%                                                                                                                                                   
- At least one of its shareholders must be known and it cannot own more than 25.01%                                                                                                                                                 

Mark     Company name ... 

To remove the header in a gvim session I would use d/^Mark, then :wq. But I have hundreds of these, so I would like to use gvim -c and argdo. I do this a lot with s//, say something like gvim -c "argdo %s/foo/bar/ge | wq" *.tex.

My first thought was gvim -c "d/^Mark | wq" *.txt, but this doesn't work because / isn't an ex command.

Is there an ex equivalent of d/^Mark that I can use with gvim -c and argdo?

share|improve this question
up vote 2 down vote accepted

You want the :d[elete] command, prepended with a range:

:[fromline],[toline]d

Ranges are not limited to absolute line numbers: search can be used too:

:1,/bar/d

deletes all the lines between line 1 and the first line containing bar.

The following command deletes every line between line 1 and the line above the first line starting with bar:

:1,/^bar/-1d

Assuming you don't pass other commands before that could move the cursor, we can cut out a couple of characters:

  • the current line is assumed if the first part of the range is missing, since we open a new buffer and we didn't move the cursor we can remove the 1,

  • the default offset for - is 1, since that's the offset we use we can leave the last 1 out too. The command becomes:

    :,/^bar/-d

Built from the bricks above, the following command should work:

$ gvim -c "argdo ,/^Mark/-d | wq" *.txt
share|improve this answer
    
Thanks for the lesson! This is very helpful. – Richard Herron Aug 3 '13 at 18:33
    
I found a problem. When Mark is in the first row, first column, this deletes the first row so that I lose the header. Is there a solution for this? Thanks. – Richard Herron Aug 4 '13 at 15:24
    
My proposed solution works for the snippet in your question. If you want a more generic solution you'll need to write a function that checks for the line number of the first ^Mark and acts accordingly. – romainl Aug 4 '13 at 15:41
    
Ah, OK, thanks! – Richard Herron Aug 5 '13 at 10:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .