Wrapping is controlled by the 'textwidth'
option. :help 'textwidth'
shows that it's local to buffer, so different values can be set, for example manually via
:setlocal textwidth=50
To make this automatically apply to all Ruby files, you can use Vim's built-in filetype detection and filetype plugins. Put the above command into ~/.vim/after/ftplugin/ruby.vim
. (This requires that you have :filetype plugin on
; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/ruby.vim
.) Alternatively, you could define an :autocmd FileType ruby ...
directly in your ~/.vimrc
, but this tends to become unwieldy once you have many customizations.
The built-in functionality stops there, at buffer scope. To apply the different textwidth only to comments (or any other syntax element), dynamic adaptation is needed. This can be implemented using :autocmd
, hooking into the CursorMoved
event. My OnSyntaxChange plugin provides utility functions to simplify the setup (again placed in ~/.vim/after/ftplugin/ruby.vim
):
call OnSyntaxChange#Install('Comment', '^Comment$', 1, 'a')
autocmd User SyntaxCommentEnterA setlocal textwidth=50
autocmd User SyntaxCommentLeaveA setlocal textwidth=80