Hardscrabble đŸ«

By Max Jacobson

See also: the archives and an RSS feed

vim will fix indentation mistakes

September 7, 2014

Today I learned a neat vim trick (via the Aaron Paterson Play by Play interview):

vim auto indent gif

You can highlight some lines with visual mode and then press = and it will fix all of the indentation mistakes therein. If you’re in normal mode, you can press == to fix just the line you’re currently on.

Paterson cites this trick as an argument in favor of not indenting private methods an extra level, because vim doesn’t do that. For example:

# do this:
class Dog
  def bark
    barks.sample
  end

  private

  def barks
    ["woof", "yap", "ruff"]
  end
end

# NOT this:

class Cat
  def meow
    meows.sample
  end

  private

    def meows
      ["meow", "purr", "idk what else, cats are weird"]
    end
end

I generally do that extra indent but I might stop now 🍃.

Note: I don't have comments or analytics on this website, so it's hard to tell if people are reading or enjoying it. Please feel free to share any feedback or thoughts by shooting me an email or tagging me in a post on Mastodon @maxjacobson@mastodon.online.