I have a huge file with the entries like the one below:

<option value="201">Medical</option>
<option value="203">Finance</option>
<option value="205">Tax Return</option>

I am trying to delete 'options' entries in this file in one-shot so that file looks like the one below after editing:

Tax Return

I searched this in the google if I could do this with vim or gedit but didn't find any relevant to this(don't know searching with what keyword). Anybody has any idea, please?

2 Answers 2


You can use the following in vim to do what you want.


Here I use the s command, This is the complete syntax, you can check by typing :help :s

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

% for range means the whole file, {pattern} is an regular rexpression {string} means the string that has to be replaced. That can contain backreferences, i.e a part of the matched pattern enclosed within braces. [flags] are some extra options. g for global, i.e all the patterns in a line needs to be replaced. Other useful flag is c which asks for confirmation before changing. [count] must be the number of times, I guess.

So this can be read as, search for a lessthan< followed by any number or not a greaterthan> characters then a > then select any number of not a lessthan< characters into first group \1 then any number or any characters And replace this with the first group \1 globally.

Check this link to learn more about vim specific regular expression details http://www.softpanorama.org/Editors/Vimorama/vim_regular_expressions.shtml

  • Awesome!!!. If possible could you explain me, because it doesn't requre me to post again if anything similar I have to do. Anyhow, Thank a lot!
    – user3215
    Mar 25, 2011 at 13:47
  • @user3215 Added explanation.
    – balki
    Mar 25, 2011 at 18:43

Install surround.vim, which provides a normal-mode mapping dst that should do the job.

In order to apply this to multiple lines, select them in the visual mode and invoke

:'<,'>normal dst

This will apply the normal-mode command dst to the selected area. If you want to apply it to the entire file (not only the visual selection), use the following command

:%normal dst
  • Which command would help in this specific question?
    – jrg
    Mar 25, 2011 at 8:32
  • 1
    I've changed the formatting to make it more obvious that I mean dst. Mar 25, 2011 at 8:44
  • Nicely done. :)
    – jrg
    Mar 25, 2011 at 8:49
  • I have just checked :%normal dst with normal vim, it properly arranged <option> entries line by line and I actually wanted to mention this but forgot. Nice!!!. I have no much knowledge of visual mode(don't know using properly) and I didn't not understand how do I delete the <options...> and </option>
    – user3215
    Mar 25, 2011 at 9:26
  • Nice.. I didn't know that normal takes a range.
    – intuited
    Apr 4, 2011 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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