5

I often edit code where I have a general comment for a block of code and specific comment for each line. For instance, this a sample of my .vimrc.

"indentation
set autoindent                 "automatically indent
set shiftwidth=2               "auto-ident two blanks
set tabstop=2                  "tabs are two blanks
set expandtab                  "tabs are spaces
set backspace=indent,eol,start "backspaces delete auto-inserted indents

":cmdline
set showcmd        "show incomplete cmds
set wildmenu       "cmdline completion
set history=1000   "store 1000 cmd from cmdline
set laststatus=2   "always show the current file status
set noruler        "off by default, but had to reset it
set shell=/bin/zsh "use zsh

For now, I am able to render that kind of formatting by using easy align. However this is quite limited because it natively only considers few delimiters. For vim (whose comments start by ") and LaTeX (whose comments start by %) I need to tweak the g:easy_align_delimiters variable. Which in my opinion is not right, since it is syntax dependent it should arise in a locale variable with a filetype detection.

I cannot use it globally either, since some comments are aligned too far from the corresponding line of code and it takes quite a bit of time to go through each block.

Tabular isn't a solution either, since it aligns everything (including those "general" block comments).

Is there a script or a plugin that, given that syntax recognition is enabled, automatically indents comments in the same fashion as in the sample .vimrc?

  • Tabular accepts regexes so it actually works for your example: :Tabularize / ".* – Erik Dec 19 '15 at 17:30
2

I'm not familiar with easy align, but by reading the documentation my understanding is that you shouldn't tweak g:easy_align_delimiters. As the plugin allows specifying the character along the command or operator, you could define a new command/operator that calls the easy align specifying the correct characters.

Usually the comment characters are set by the ftplugin on the 'commentstring' option, which is local to the buffer. Thus the new command/operator would need to parse the contents of &commentstring and include it dynamically, possible using :execute.

  • Thanks. Sounds like a good idea and a good way to get familiar with the plugin. Will look at it. – Ronan Dec 22 '15 at 19:25

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.