1

I am doing some refractoring and one of the objectives is to move some inline js functions in HTML.

<a href="#" onClick="function('');">text</a>

The idea is to remove the onClick event along with the contents inside the quotes

<a href="#">text</a>

suggestions to move faster?

I would imagine after doing it one time I can just repeat the motion with . after finding the next instance.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Helmut Granda
  • 4,565
  • 7
  • 35
  • 51
  • If you use multiple commands to remove the whole attribute, you can use `q` to record them and then replay them. See `:help record`. – Felix Kling Mar 05 '14 at 06:52

2 Answers2

3

A simple regex should do it:

%s /onClick=".\{-}"//g
tuxcanfly
  • 2,494
  • 1
  • 20
  • 18
  • 1
    Wouldn't work for `` I guess. – Felix Kling Mar 05 '14 at 06:51
  • true, but not sure why? Shouldn't ".*" match only string inside quotes? – tuxcanfly Mar 05 '14 at 06:58
  • nevermind, I see why. It's because of the ending quote of the last attr.. oops – tuxcanfly Mar 05 '14 at 06:59
  • 3
    A better solution than tacking a semicolon on to the end would be to either use `\{-}` rather than `*` after the `.`, or changing the `.` to `[^"]`. – icktoofay Mar 05 '14 at 07:12
  • It was hard to chose but after trying both solutions this was the best option given an addition to the command %s /onClick=".\{-}"//gc (note the "c") at the end to enable interactivity in order to verify all elements that were to be replaced. – Helmut Granda Mar 05 '14 at 07:32
  • 1
    @HelmutGranda - +1 Choose the solution that works best for you. Considering my own note of warning, interactively changing and being able to verify **is** the best solution. – Lieven Keersmaekers Mar 05 '14 at 07:39
1

Another possible method

%norm / onClick<C-v><ESC>ndt>

Breakdown

%norm       # following are normal mode commands
/ onClick   # Search for _onClick
<C-v><ESC>  # Exit search mode
n           # goto next match
dt>         # delete till >

Note that this can be sufficient for a one time job. If you need a foolproof solution, this or regex or whatnot is not the right tool for the job.

Community
  • 1
  • 1
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • This worked! The only replacement I had to do was instead of . see: http://stackoverflow.com/questions/426896/vim-ctrl-v-conflict-with-windows-paste – Helmut Granda Mar 05 '14 at 07:16