How to paste something between html tags in Vim?
Pressing p
pastes things bellow the开发者_StackOverflow社区 current line, dit
deletes things inside html tags. How do I paste something inside html tags?
Nor here
<p>I want to paste something here</p>
Not here
I usually just do vitp
which visually selects the inner contents of the tag, then pastes over what is selected.
Works for me.
The result of pressing P
and p
depends on what you have in the selected register at the time. If you delete or yank one or more entire lines (e.g. with dd
, Y
or Vd
commands), then pressing P
will insert the contents of your register on the line above the current line, whereas p
will insert on the line below the cursor.
If you delete or yank a section of text less than a line (e.g. with the D
, or yw
commands), then P
will insert the contents of your register directly before the current cursor position, and p
will insert directly after the cursor (i.e. on the same line).
If it helps, you could consider the linewise selection as being analogous to block html elements (such as <div>
), and characterwise selection as being analogous to inline html elements (such as span
).
So to answer your question: it depends. Supposing you have a linewise section of text in the register, you would want to break the target tag onto two lines before doing the paste operation. In your example, rather than doing dit
to delete the contents of the tag, do cit
to delete the same section and go into insert mode. Hit return
once, to insert a new line, then esc
to go back into normal mode, then P
to insert your linewise register above the line with the closing tag.
If you didn't want to split the tag onto multiple lines, you would instead have to make sure that you yanked a characterwise selection into the register. Then you could run:
"_ditP
"_
deletes the text into the black hole register, ensuring it doesn't overwrite what is in your default register. dit
deletes the contents of the tag, and P
pastes the contents of your default register before the cursor position.
remove the current content between the tags with the command
cit
that will 'change in tags' and once that content is gone, you can paste with middle click or if you need go back into command mode and use your normal p/etc.
vitp
should handle a linewise paste.
You can press "v
" for visual, then go to where the cursor is, and press p
or P
.
精彩评论