2

I've html file (e.g. curl -o page.html http://example.com/) where I would like to remove inner html block for given html tag.

I've the following code which works:

$ ex -V1 page.html <<-EOF
/<h1>
norm nvitd " Jump to next match and remove the inner tag block.
wq
EOF

However I'd like to make the condition to execute norm nvitd only when the pattern <h1> is found. For example when testing with <h2> then the next line is executed generating the warnings, despite the pattern is not found.

How to make such condition? It's possible to make it in one line (like a ternary operator)?

1 Answer 1

2

A sequence of normal mode commands aborts as soon as one command (e.g. a search) fails. So, if the n jump to the next match fails (with Pattern not found: <h1>), the following vitd isn't executed. That's your condition, and it works for me that way (using Vim in Ex mode via vim -e).

Note that you have another bug in your script: By first searching for /<h1>, that already positions the cursor on the first match. The following n command then moves to the next match, so this effectively clears the second H1 tag, not the first. You should instead combine both, like this:

/<h1>/norm vitd
wq

Or, if you want to clear all H1 tags, use :global.

7
  • I've added n, because I'm confused when Vim set the cursor on matched pattern and when it's not. I guess in Ex mode it doesn't set cursor properly on the pattern (it seems so, because the tag is at the beginning of the line), but when it is in the middle, it won't work. So I think the n is necessary to make this works for these both cases. See: this for more details.
    – kenorb
    May 16, 2015 at 10:44
  • Why do you use Ex mode at all (with all its complications)? To automate Vim, see stackoverflow.com/a/23237529/813602 for possibilities. May 16, 2015 at 10:56
  • I'm using ex to make and look it simple as part of the script and I'm learning it at the same time. Secondly it's distributed widely by default (opposite to vim). The other thing is that I can always add silent (-s), so the screen doesn't blink when I run the script and I can also deal with stdin and stdout easily. I know I can use vi instead of vim, but on OSX it's linked to vim by default, so it's the same thing (so I can't check the compatibility of my changes with plain vi), that's why the easiest way for me is to use ex, so it's cross-platform compatible.
    – kenorb
    May 16, 2015 at 11:06
  • 2
    Using dit would be simpler (and faster?) for a batch command.
    – Vitor
    May 18, 2015 at 13:36
  • 1
    @kenorb: Yes :help :/ defines /{pattern}/ as a range. Not all Vim commands take such range, but in those that do, it is indicated at the corresponding :help {cmd} page; all Ex commands are listed under :help :index Nov 2, 2015 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.