Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

DreadCthulhu posted:

is there a popular plugin out there for tidying a buffer in vim? For example, sometime I'll paste some optimized CSS into a buffer and will want to make it readable. Is there a simple and properly working tool for this in vim?

As far as "unoptimizing" CSS, check out the five sed commands at the bottom of http://mabblog.com/source/shell/unCompress.bash. They do a pretty good job of it, and are a good springboard if you want to implement the functionality in vim.

Adbot
ADBOT LOVES YOU

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

ArcticZombie posted:

code:
let n=1 | g/<p>/s/<p>/\='<p id="'.n.'">'/ | let n=n+1

See :help sub-replace-expression or :help sub-replace-\=.

:s/<p>/\='<p id="'.n.'">'/ is replacing each <p> with the concatenation of the string <p id=", the contents of the vimscript variable n and the string ">.
. is the concatenation operator in vimscript (see :help expr-.).

The vertical bar is basically just the equivalent of a semicolon in C; it allows multiple expressions on the same line. See :help :bar. I say basically because it has different behavior when following certain commands, :g (a.k.a. :global) being one of them.

Also check out :help :g. That command has two arguments (separated by / or whatever). The first is the pattern it matches, the second is the Ex command it runs on those lines. In this case, it matches any line containing <p> and replaces it as explained above.

Then follows the nonstandard behavior of the vertical bar used after :g. Instead of just moving on to the next match, it first runs the command after the bar and thus increments n. Only after that command finishes does it move on to the next matching line.

And to leave no stone unturned, :let declares an internal vimscript variable. n is still going to be there after the command finishes running, so you can use it afterwards if you'd like.

Hope that helps! :)

fidel sarcastro posted:

In fact, the whole thing sounds like it's behaving like a C-style for loop but a quick search says Vim doesn't have any support for them?

:help for / :for :confused:

EDIT:
For example:
:for i in range(10) | echo i | endfor

The Laplace Demon fucked around with this message at 16:43 on Aug 11, 2013

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

fuf posted:

A command to jump from a tag to the corresponding close tag (like jump from <div> to </div>)

Place cursor over div, and press %.

fuf posted:

A command to replace the contents of the open and close tag at the same time (like if I replace <h1> with <h2> it will simultaneously replace </h1> with </h2>)

I don't do much with markup, but a plug-in I use all the time for refactoring is vim-multiple-cursors. Without changing its configuration, you'd put your cursor over the first h1, hit <C-n> twice to enter multicursor mode and select the current word, hit <C-x> once for each of the nested h1s you have ;), and when it highlights the closing h1, press c then type h2.

If you don't have any nested h1s, that'd reduce to <C-n><C-n>ch2<Esc>. Usually when I'm refactoring my code, I want to replace all of my variable names, so I can just mash on <C-n> until everything is matched, but sometimes that granularity of being able to skip some matches comes in handy.

The Laplace Demon fucked around with this message at 18:25 on Apr 25, 2014

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

fuf posted:

(it jumps from between the opening and closing brackets of <div>, but not from <div> to </div>)

:downs: Oops, I use sensible.vim, which automatically runs runtime! macros/matchit.vim. That macro has been included in vim since 6.0, and it's what adds the jump between tag behavior.

  • Locked thread