I solved this problem and wanted to share with everyone: just takes a little regex-magic to match the first space after non-whitespace text, except where it is preceded by a comment character anywhere in the line.
In short, you can place these lines in your .vimrc
:
vnoremap <expr> -<Space> ':Tabularize /\S\(' . split(&commentstring, '%s')[0] . '.*\)\@<!\zs\ /l0<CR>'
nnoremap <expr> -<Space> ':Tabularize /\S\(' . split(&commentstring, '%s')[0] . '.*\)\@<!\zs\ /l0<CR>'
Note we can detect the filetype comment-character by parsing &commentstring
(try :echo &commentstring
to see what it looks like). You can also replace that call to split()
as follows
vnoremap <expr> -<Space> ':Tabularize /\S\(' . b:NERDCommenterDelims['left'] . '.*\)\@<!\zs\ /l0<CR>'
nnoremap <expr> -<Space> ':Tabularize /\S\(' . b:NERDCommenterDelims['left'] . '.*\)\@<!\zs\ /l0<CR>'
if you happen to use the NERDCommenter plugin.
Came across a great example of where this is useful today: case statements in bash scripts -- so the following
case "$string" in
foo) command ;; # here's a command
foobarbaz) longer_command ;; # and another one
abcdef) f ;; # the last command
*) hello ;; # and the default case
esac
becomes
case "$string" in
foo) command ;; # here's a command
foobarbaz) longer_command ;; # and another one
abcdef) f ;; # a final command
*) hello ;; # default case
esac
note again that the spaces in end-of-line comments are ignored.