4

If someone is editing the following javascript file:

let foo = true
if (foo) {
  let bar = 42
  // moooooore code...
}

You can use vi{ anywhere on the line with let bar to select let bar = 42 and all other lines inside {...}.

However, how can I select based on indentation?

Let's take Ruby as an example:

def some_method
  if @foo
    @bar = 42
    # moooooore code...
  end
end

What command would I use to select @bar = 42 and all other code ?

  • 3
    Ive used github.com/michaeljsmith/vim-indent-object for a while with some success. Not sure if that counts as an answer though – D. Ben Knoble Jan 24 at 0:00
  • @D.BenKnoble good enough for me, thanks a lot :+1: – joaumg Jan 24 at 1:48
  • 1
    @D.BenKnoble +1. I think your comment is an actual answer. BTW it is easier for others to see if a question has got an answer when people do not place their answers in comments. I think comments are for clarifications and questions to the OP. This also disables the possibility to up/down vote to get a fast feedback. – Hotschke Jan 24 at 11:04
  • @D.BenKnoble If you was concerned about answering by referring to a plugin, maybe following discussion on vi.meta.stackexchange.com/questions/21/… reassures you that plugin answers are on-topic. – Hotschke Jan 24 at 12:37
  • @Hotschke thanks for the info; I know I’ve got a couple of answers to put together when I get a moment – D. Ben Knoble Jan 24 at 14:02
4

I've used vim-indent-object for a while with some success.

It provides text objects like ii, ai, iI, and aI, each with slightly different semantics.

For your sample code

def some_method
  if @foo
    @bar = 42
    # moooooore code...
  end
end

If the cursor is anywhere inside the if block, you could use vii to select the the bar and more lines. Or, use =ii, cii, dii, !ii, yii &c., depending on what you want to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.