Search code examples
vimsurround

Surround tags in vim


I often want to surround a div tag with another div tag. https://github.com/tpope/vim-surround is a real help there.

|<div>
  ...
</div>

if the cursor is at | and I typeysat<div> I get

<div><div>
  ...
</div><div>

instead I want

<div>
  <div>
    ...
  </div>
</div>

how can I force the div to be on a new line?


Solution

  • :help at says:

    When used in Visual mode it is made characterwise.

    The low-level mechanism used by Surround for its ys custom operator consumes the given motion as a visual selection, the type of which is then used by the plugin to decide what to do. In this case, Surround gets a characterwise motion which makes it operate in a characterwise fashion.

    In order to achieve your goal, you will need to force a linewise motion by adding a V between the operator (ys) and the motion (at):

    ysVat<div><CR>
      ^
    

    See :help forced-motion:

                                *o_V*
    V       When used after an operator, before the motion command: Force
            the operator to work linewise, also when the motion is
            characterwise.