I frequently have inline comments for variables:

var1 = 0    # Var 1
var2 = 0    # Var 2
var3 = 0    # Var 3
var4 = 0    # Var 4

(This is in python, for example, but this question applies to C-Style // just the same.)

Sometimes it happens that as I develop I have to add new variables and they may end up being longer than the previous variables. I then have to refactor my white space from

var1 = 0    # Var 1
var2 = 0    # Var 2
var3 = 0    # Var 3
var4 = 0    # Var 4
variable = 234 # Var


var1 = 0        # Var 1
var2 = 0        # Var 2
var3 = 0        # Var 3
var4 = 0        # Var 4
variable = 234  # Var

or thereabouts.

Right now I line-by-line it ciw with the cursor in the white space and then tab a lot. Is there a simpler way to do this? >w and >W just indent the line (it honestly seems like > only indents line by line so I don't understand the 'subject-verb' point of it (it almost seems like >> is redundant)).

I would really like something built-in, like I would have expected >w to have worked, but if that's not the case, I suppose a plug in will do.


>w doesn't work because > is a line-wise operator no matter what motion you give it.

There are a few ways to accomplish the edit you're looking for:

1. insertion with repeat

After you've inserted tabs/spaces to get the alignment you want, press . on the remaining lines. No need to insert again.

2. visual block insert

Again visually select a block (ctrl-v3j) then type I to insert. Press <tab> or <space> to make up the spaces, then press <esc>. The insertion will apply to all the lines within the selection.

3. visual block shift (:help v_b_<)

Visually select a block (press ctrl-v) downwards from the first # (3j). Now type >. This will shift only the text to the right of the start of the visual block selection. This operation has downsides though, if the space needed isn't a multiple of shiftwidth, and the corresponding < doesn't work.

4. plugin tabular.vim

Visually select the lines you want to align then type :Tabularize /#/. This will look like

:'<,'>Tabularize /#/

in the cmdline. Then the comments will become aligned.

| improve this answer | |
  • (1) has the problem that you always have to have cursor return to the same position in the white space (I've used . for this purpose in conjunction with my ciw method but never found it satisfactory. (3) is the closest to my original conception of a solution, but you're right, it does have significant alignment problems and you can only > once before the text is de-selected. (4) I have literally no plug ins installed, but I will investigate tabular.vim. (2) seems to me to be the best solution and it's the closest to how I already comment out lines of code and works very well. Thank you. – malan Jul 25 '18 at 12:15

You can use visual block mode: Move to the first # which you want to indent, press CTRLV, select all the lines you care about and the use I or A so insert text before or after the block you selected.

The text will only appear on the first line until you press Esc, then it will also appear on the other lines.

| improve this answer | |

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.