1

I have text set to wrap at 80 characters.

Is it possible to have text wrapped to, say, 50 characters when editing a certain type of line?

For example, I program Ruby and I want Ruby comments to be wrapped to 50 characters whilst everything else stays at 80.

Is it possible?

0

1 Answer 1

1

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
3
  • Thanks but I only want the setting to apply to comments not the whole of the Ruby file.
    – jordelver
    Jan 21, 2016 at 13:13
  • Oh, sorry, must have missed that. It requires a bit more effort; fortunately, I've written a plugin to simplify that. See my edit. Jan 21, 2016 at 13:42
  • This is great. Thanks so much. Sorry for the mega-late response :)
    – jordelver
    Sep 18, 2017 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .