1

I very often want to do the following or similar:

  • indent all lines from previous position to current position inclusive
  • repeat, repeat, repeat (to further indent the same lines)

I feel I should be able to do that by saying:

>''...

(greater-than single-quote single-quote period period period).

But it doesn't work. The first >'' successfully indents the desired lines, but then "previous position" is lost, and each . only indents the current line (which is always the first of the desired lines, for some reason, no matter whether I started at the beginning or end of the desired lines).

So, instead, my recipe for indent-and-repeat is:

'' if necessary to get to first line to be indented
ma to mark it "a"
'' to get to last line to be indented
mb to mark it "b"
>'a to indent from mark "a" to current position (mark "b")
>'b to do it again (since the >'a moved me to mark "a" for some reason)
>'b to do it again (since the >'b did *not* move me to mark "b" for some reason)
>'b to do it again

In other words, to indent the lines 4 times and retain ability to jump to previous position, I say:

''ma''mb>'a>'b>'b>'b

Instead of simply the following which doesn't work:

>''...

That is WAY more time and effort and mental burden than I would like.

An alternative workaround, still awful, and loses "previous position":

>''
look for something like "63 lines >ed 1 time", and type that number:
63>>

Is there a way to make >''... work as I would like it to? Perhaps by some clever rebinding of the > key.

Requirement: >'' must indent the lines without changing the current or previous cursor position (regardless of whether '' refers to earlier or later in the file).

Requirement: . must do the same, as must subsequent .'s.

8
  • In general, gg=G is used to indent entire file. If you want to indet from current line till end, you can use command like >>G
    – SibiCoder
    Aug 9, 2016 at 7:09
  • I can't reproduce your problem: >''... indent from my current position to the previous insert position as it should (but indeed the cursor moves to the ' mark). Maybe your problem is that '' doesn't represent what you think: maybe you should precise what you consider to be "previous position". If you are familiar with the '' notation maybe the problem comes from a configuration in your vimrc or a plugin and you should test the operation with vim -u NONE (see this)
    – statox
    Aug 9, 2016 at 7:25
  • @SibiCoder Thanks, but I'm not interested in indenting the entire file, nor from current line to end-of-file (which is >G, not >>G). My question is very specific: I want to indent to "previous position" (i.e. single-quote single-quote), without changing "current" or "previous" position, and in such a way that "." indents more.
    – Don Hatch
    Aug 9, 2016 at 7:26
  • @statox - strange-- what OS and vim version are you on? I believe I've experienced the behavior I describe on every variation of unix I've worked on for the past ~25 years, in particular ubuntu linux with vim 7.4.1816. I reproduce as follows: vim a new file, insert lines "a" and "b". '' (singlequote singlequote) several times to verify that it goes back and forth between the two lines. Stop on the first line, so "current" is "a" on line 1, and "previous" is "b" on line 2. Type >'' which indents both lines as desired. Then type . which further indents only the first line, as undesired.
    – Don Hatch
    Aug 9, 2016 at 7:35
  • @statox - I tried vim -u NONE per your suggestion-- same behavior (aside from different shiftwidth)
    – Don Hatch
    Aug 9, 2016 at 7:38

2 Answers 2

3

Similar to romainl's suggestion, but without the mapping, I would do this by using one extra keystroke to enter visual mode:

v''>...
2

After your first >'' you can do >'] to indent the last changed lines, which can be repeated with ..

It's not quite >''..., but >''>'].. is certainly better than ''ma''mb>'a>'b>'b>'b.

Alternatively, you could use visual mode and this fairly common mapping:

xnoremap > >gv
xnoremap < <gv

and simply do v''>>>.

2
  • I agree that's better :-) And I'll start using it, pending finding an implementation of >''... . Thanks.
    – Don Hatch
    Aug 9, 2016 at 7:41
  • @DonHatch, see my edit.
    – romainl
    Aug 9, 2016 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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