Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paste something between html tags in Vim?

Tags:

vim

Pressing p pastes things bellow the 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
like image 782
alexchenco Avatar asked Feb 18 '10 07:02

alexchenco


4 Answers

I usually just do vitp which visually selects the inner contents of the tag, then pastes over what is selected.

Works for me.

like image 190
rossipedia Avatar answered Nov 19 '22 07:11

rossipedia


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.

like image 10
nelstrom Avatar answered Nov 19 '22 07:11

nelstrom


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.

like image 3
Rick Avatar answered Nov 19 '22 08:11

Rick


vitp should handle a linewise paste.

like image 3
michael Avatar answered Nov 19 '22 07:11

michael