6

I have following code. I want to add spaces around =. With the minimal keystrokes (May be with the help of macro). Do not want search and replace approach.

<div className="row">
    <div className="col-md-12 text-center">
        <h3>vOrderv</h3>
    </div>
</div>

After changes it should look

<div className = "row">
    <div className = "col-md-12 text-center">
        <h3>vOrderv</h3>
    </div>
</div>
  • 2
    Is there a reason you don't want to use search/replace? Search/replace would be the most the minimal approach in this case. Off topic: adding spaces around the = for HTML is bad form, IMO. It makes HTML less readable. – Tommy A Jun 27 '16 at 13:25
  • yes in following case === it will become = = = . This is not complete Html, it is jsx, it can also contain javascript code. – WitVault Jun 27 '16 at 13:26
  • 3
    If you used %s/=\+/ \0 /g that wouldn't be a problem. – Tommy A Jun 27 '16 at 13:28
6

I'm not sure why you don't want to use search/replace, but here's a reusable command:

command! HTMLArgEq %s/\s\@<!=\+\s\@!/ \0 /g

It uses search/replace, but you only need to create it once. Explanation:

%s/    - Substitute entire buffer
\s\@<! - Not preceded by whitespace
=\+    - One or more equal characters
\s\@!  - Not followed by whitespace
/ \0   - Add spaces around the match
/g     - Replace globally
5

Here's what I would do. Visually select all of your HTML you want to do this to, and hit the following:

:norm f=i<space><C-v><esc>la<space>

Note that <C-v>, <esc>, and <space> are keystrokes, not literal text.

Explanation:

:norm Means apply the following set of keystrokes to every selected line. f= means move to the first equals sign. This conveniently has the added benefit of stopping playback if an = character is not found. i<space><C-v><esc> means enter insert mode, insert a space, and then esc. <C-v> allows us to enter a literal esc character, which is necessary since we are doing this from the command line. la<space> moves one to the right (so we are on top of the equals sign), and appends a space after the equals sign.

The only downside to this approach is that you must manually select every section of HTML to do this to, which is necessary if you do not want to use a regex search and replace.

  • +1 for not using search as specified. But, this requires more effort and keystrokes than a substitution 😆 – Tommy A Jun 27 '16 at 18:37
  • @TommyA Yes agreed, but the OP seemed to indicate that he had equals signs in his javascript that he wanted left alone. The only (obvious) way to do that is to visually select the non-javascript portions. – DJMcMayhem Jun 27 '16 at 18:39
  • What if there are multiple = on a line? This only gets the first. – Brian McCutchon Jun 29 '16 at 2:40
  • @BrianMcCutchon OP's example only had one = per line. – DJMcMayhem Jun 29 '16 at 2:43
3

Search and replace can be used, but I must admit, the syntax is not simple to remember:

:%s/\s*=\@<!==\@!\s*/ = /g
  • The first \@<! matches with zero width if the preceding atom doesn't match = (1st one) before = (2nd one)
  • The second \@! matches with zero width if the preceding atom (3rd =) doesn't match after the current position (2nd =)

Of course, you can add other characters to the black-list. See for instance https://stackoverflow.com/a/23072106/15934 on the same topic.

1

If you want to make changes interactively, you can still use :substitute but with the c flag ('c' for 'confirm'):

:%s/=/ = /gc

The cursor will land on the first match and you'll be prompted whether you want to carry out the substitution. Hit y for yes, n for no. Then the cursor will jump to the next match, and so on. q to quit.

See :help :substitute, and, specifically, :help :s_c.

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.