I have a bunch of assembly code that needs commenting and I want to avoid having to space forward to make a comment every time just to align the comments.

movq %rdi, %rdi      # comment
jne label            # comment
addl %esi, %esi      # comment

Is there an easy way to do this with vanilla vim? Note the differing line lengths.

  • 2
    What behavior do you want? Do you want it to do this automatically as you type? Do you want to be able to run a command in normal mode after you're done typing? Jul 14, 2018 at 20:29
  • I'm not really too concerned about replicating the behaviour - I just want to know how I would achieve it to better understand vim and prevent similar repetition from occurring in the future. Jul 16, 2018 at 21:57
  • 1
    More solutions here that give a good overview of the different approaches in Vim: stackoverflow.com/questions/1229900/… Jul 16, 2018 at 22:31

1 Answer 1


I think this will accomplish what you want. During typing, press <F1> to pad spaces to the 21st column as shown. You can add #␣ to either of these mappings to have the comment started automatically. Note that this will erase any existing comment.

nnoremap <F1> 21A <Esc>d21\|
imap <F1> <Esc><F1>a

The trick is taken from this question here: https://stackoverflow.com/a/12412493/365478

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.