Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65841567/surround-tags-in-vim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

: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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...