Using Kana Natsuno's textobj-user: install textobj-user, then put the following in a file textobj-comment-lines/plugin/textobj/comment_lines.vim
:
if exists('g:loaded_textobj_comment_lines')
finish
endif
let g:loaded_textobj_comment_lines = 1
let s:save_cpo = &cpo
set cpo&vim
call textobj#user#plugin('commentlines', {
\ '-': {
\ 'select-a': "a#", '*select-a-function*': 'CommentLinesSelectA',
\ 'select-i': "i#", '*select-i-function*': 'CommentLinesSelectI'
\ }
\ })
function! CommentLinesSelectA() " {{{1
if getline(line('.')) !~# '\m^#.*\S'
return 0
endif
let head_pos = search('\m\%(\%^\|^\n\|^[^#].*\n\|^#\s*\n\)\zs#.*\S', 'cbnW')
let tail_pos = search('\m\%#.*\n\%(^#.*\S.*\n\)*\%(^#\s*\n\)*\zs\%(^#\s*\n\)', 'cnW')
if tail_pos > 0
if getline(tail_pos) !~# '\m^#'
let tail_pos -= 1
endif
else
let tail_pos = search('\m^#.*\S.*\n\ze\%(\%$\|^\n\|^[^#].*\n\|^#\s*\n\)', 'cnW')
endif
return ['V', [0, head_pos, 1, 0], [0, tail_pos, 1, 0]]
endfunction " }}}1
function! CommentLinesSelectI() " {{{1
if getline(line('.')) !~# '\m^#.*\S'
return 0
endif
let head_pos = search('\m\%(\%^\|^\n\|^[^#].*\n\|^#\s*\n\)\zs#.*\S', 'cbnW')
let tail_pos = search('\m^#.*\S.*\n\ze\%(\%$\|^\n\|^[^#].*\n\|^#\s*\n\)', 'cnW')
if !tail_pos
let tail_pos = line('$')
endif
return ['V', [0, head_pos, 1, 0], [0, tail_pos, 1, 0]]
endfunction " }}}1
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: set sw=4 sts=4 fdm=marker:
This adds two mappings, vi# and va#, and the first one does what you want. The gist is the regexp searches, the rest is mostly boilerplate.
The comments are hard-coded to #
anchored at the beginning of line. Dealing with other kinds of comments (f.i. /*
... */
) would be possible, but not trivial. You can, of course, refine this in many ways.
Reference: textobj-user user manual.
paragraphs
option. It uses nroff macros which I'm not familiar with. I tried to mess with it a bit but never got working solution. Still, I feel that there might be a way to do it with this.textobj-user
plugin below. For what its worth.'paragraphs'
option does not allow you to arbitrarily define different types of "paragraph" by using nroff macros. It's an incredibly outdated option that allows you to change which nroff macro strings count as paragraph breaks when editing a file that already contains them. i.e. It's intended to make editing nroff files easier, not as a general purpose paragraph customization tool.