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.

Don Hatch
  • 291
  • 1
  • 6
  • 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 09 '16 at 07: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 09 '16 at 07: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 09 '16 at 07: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 09 '16 at 07:35
  • @statox - I tried vim -u NONE per your suggestion-- same behavior (aside from different shiftwidth) – Don Hatch Aug 09 '16 at 07:38
  • @DonHatch I tested that on a gVim 7.4 under windows I reproduced your steps: 2 lines, a and b '' which switches between them >' which indents both of them and the dot command indents bot of them too... It also works on a vim 7.2 on a rhel. – statox Aug 09 '16 at 07:40
  • @statox - unbelievable. Am I cursed? gVim on ubuntu (same version as vim: 7.4.1816) gives same unfriendly behavior for me that I've always seen. You tried vim -u NONE too, right? (And vim -u NONE -U NONE -N) And same behavior? – Don Hatch Aug 09 '16 at 08:06
  • @DonHatch: yup it did it without configuration file... I admit that if you've seen this behavior for several years I'm pretty confused to have a different one... Maybe it would be interesting to have someone else testing it... Maybe I didn't understood properly what you are trying to do but I don't think so. – statox Aug 09 '16 at 08:14

2 Answers2

3

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

v''>...
Rich
  • 31,891
  • 3
  • 72
  • 139
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''>>>.

romainl
  • 40,486
  • 5
  • 85
  • 117