Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Last active December 23, 2015 18:19
Show Gist options
  • Save sycobuny/6674570 to your computer and use it in GitHub Desktop.
Save sycobuny/6674570 to your computer and use it in GitHub Desktop.
tl;dr: How do you insert line breaks at the beginning and end of a visual block in vim?

Since I've just started using snipMate, I was playing around and used table to get a pre-built table. Pretty nicely, it dropped a whole lot of text in place for me:

<table border="0">
    <tr><th>Header</th></tr>
    <tr><th>Data</th></tr>
</table>

But, bypassing the fact that I don't like this default table setup (it uses <th> for the same data cell, for some reason, and does not use <thead> or <tbody>, which may be uncommon but I tend to use them), we'll skip to another problem that presented itself.

    <tr><th>Header</th></tr>

Like many other anal retentive quirks of mine, I like tags with children to be on a line separated from those children (for most elements, anyway; inline text modification in paragraphs I tend to let slide). But, here, snipMate has created <tr> tags with <th> tags inside on the same line. The horror!

Of course, the answer to my first problem could be as simple as the answer to this one: make my own snippets, or tweak the existing ones, so that they're more to my liking. But, I'd rather know what I can do about it with basic vim commands. It's more easy to apply that knowledge elsewhere.

So, let's get started with a further explanation of my problem. I already know that, say I have my cursor at |, like so:

    <tr><th>|Header</th></tr>

I can, starting from normal mode, type <C-v>at and wind up with a visual block selecting the inner tag. Assume, for a moment, that { and } indicate the bounds of visual selection, and | once again represents the cursor:

    <tr>{<th>Header</th>|}</tr>

I would, ideally, like to perform the inverse operation of J; that is, I'd like to insert a line break at the { and another at the }. From this point, if I could do that, I assume that either the line would properly indent because vim is magical, or I could figure out a way to select the new line and do == myself, which should then fix the indenting.

But, I'm not entirely sure how I would do such a thing. So far, the not-very-pleasant solution I've come up with is:

<C-v>at<ESC>a<enter><ESC>gvo<ESC>i<enter><ESC>

That's obviously pretty hacky and, I worry, is highly dependent on the HTML looking just this way (also, it only works for HTML because of the at text object I've married it to).

So, is there a more simple solution available?

@sycobuny
Copy link
Author

In case anyone ever looks at my old gists, it turns out splitjoin.vim is meant to solve exactly this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment