Using Vim to cut and paste an element and move it to another line efficiently

130 views Asked by At

so I am continuing my use off vim...I have this piece of html here:

<div id=container>
    <ol>
        <li><h1>banner</h1></li>
        <li><first_item</li>
        <li><second_item</li>
        <li><third_item</li>
        <li><fourth_item</li>
</div>

The div tag starts on the 17th line. I went to move the header one out of the list but still within the div tag. My moves were:

  1. 19gg (go to 19th line)
  2. dd (this is delete the line, but I see it the same as cutting the line as well)
  3. 17gg (go to the 17th line)
  4. p (pasting here brings the pasted line to the next line)
  5. shift + << (indent back one, because it used the original indentation which was one more due to being in the list)

Then there was the next question about removing the tags - there is most probably a plugin out there to help me which I will hunt down now.

Was what I did long winded? is there a quicker way or more efficient way to achieve this? (excluding the bit about removing the list tags around the header.

2

There are 2 answers

0
Ingo Karkat On BEST ANSWER

Tim Pope's unimpaired plugin, as well as my LineJuggler plugin provide ]e mappings to quickly move line(s). With that, you can move a line from 19 to 17 (i.e. over 18) with 1[e.

For removing surrounding tags, have a look at surround.vim - Delete/change/add parentheses/quotes/XML-tags , also from Tim. Alternatively, you could just delete the inner tag (dat), and use my UnconditionalPaste plugin's glp mapping to paste that as a separate line, or even use g[p on line 18 to paste to above with the correct indent (of the current line).

PS: Instead of 19gg, you can also do 19G; still two key presses, but in parallel.

0
romainl On

With surround.vim:

/li<CR> " jump to the first <li>
dst     " remove surrounding <li> and </li>
dd      " cut the line
k       " move up one line
[p      " paste above with the same indent

Without:

/li<CR> " jump to the first <li>
da<     " delete <li>
$       " jump to end of line
.       " repeat deletion
dd      " cut the line
k       " move up one line
[p      " paste above with the same indent

or:

/li<CR> " jump to the first <li>
"xyit   " yank what's inside the `<li>` into register xd
dd      " cut the line
k       " move up one line
O       " open a new line above
<C-r>=x " insert content of register x

By the way, your <ol> is missing an </ol>.