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.

  • As mentioned in the accepted answer, > is line-wise. But it’s particular convenient for, say, >ip or >i{
    – D. Ben Knoble
    Sep 23, 2020 at 12:36

4 Answers 4


>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.

  • 1
    (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.
    – mas
    Jul 25, 2018 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.


Plugin vim-visual-multi

E.g. to align by # 3 lines:

  • [cursor at start of 1st line]
  • Ctrl-V
  • jj
  • \\c
  • f#
  • \\a
  • Esc

Note: \\ can be other keys, in link:

Pay attention: in this page as in others, leader- is your g:VM_leader (default \\). Also, don't trust the pics for mappings.

  • mnemotechnic: "a" in \\a stands for align, while "c" stands for column Sep 23, 2020 at 9:23
  • I think you meant *mnemonic :)
    – D. Ben Knoble
    Sep 23, 2020 at 12:39
  • What is the f= doing here? How is the plug-in aligning by # if you haven't specified it even once? What if I wanted to align by another character?
    – filbranden
    Sep 23, 2020 at 14:48
  • 1
    My mistake, usually I align "=" and not "#", the force of habit. I corrected the answer, thanks filbranden Sep 24, 2020 at 9:41
  • You are right @d-ben-knoble, thx! Sometimes even german is easier than english Sep 24, 2020 at 9:44

Note that it's possible to use Visual Block even if the comments are not aligned in the original text.

For example, say you want to align the comments in this block:

var1 = 0 # Var 1
var22 = 0 # Var 22
var333 = 0 # Var 333
var4444 = 0 # Var 4444
variable = 234 # Var


var1 = 0        # Var 1
var22 = 0       # Var 22
var333 = 0      # Var 333
var4444 = 0     # Var 4444
variable = 234  # Var

One easy way to do it is to use :normal to insert spaces at the # on each line, then use Visual Block and the left shift operator to shift all comments left, flush to a specific column at the start of a visual block.

Use the following sequence of steps:

  1. Select the lines on a visual selection. For example, use V4j from the first line.
  2. Insert enough whitespace before the #, which you can do with :normal f#9i . (Note the "space" at the end!) This will add 9 spaces before each # in the lines of the visual selection. If 9 is not enough, add more (like 99 or 999, as much as you want.) Note that when you type : with the visual selection, Vim will automatically insert the range, so the actual command is :'<,'>normal f#9i , but you don't need to type those characters.
  3. Move to the column where you want the #s to be flushed to. In this case, the last line has the longest variable name, so move to two spaces after the end of that name, which is where the #s should be at the end. You could use 4j3E2l to get there.
  4. Start visual-block selection, pressing Ctrl-V.
  5. Extend the visual block to the other end of the block, by moving to the first line, while staying on the same column. For example, by using 4k.
  6. Now you can use the < command to shift the lines left, but only until they hit the left of the visual block. Each < will shift them by one 'shiftwidth' only, so you're likely to need more than one. So, to be sure, use 9< (or 99<, or 999<, if you added tons of spaces in step 2.)

That should do it. This is a nifty technique and it can be helpful when you need more flexibility than what plug-ins can afford you. For example, you could easily have used this same technique to align the assignments on the = symbols.


Your Answer

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

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