Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to use Vim as plain text editor

📅 2019-Jan-28 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

Vim is not only useful for code, but also as a general plain text editor. This is useful for writing README files, Markdown files or if you like publishing plain text file content.

In plain text files, a desirable property is to have lines wrapped at a certain width. The general wrap feature of Vim is meaningful only for code which has long lines. This option just wraps the long line visually to the screen width.

To use Vim as a plain text editor you want it to automatically breaks lines at a certain width as you type. You want it to do that only at word boundaries. The option that enables this mode is textwidth. For source code, this feature is disabled and is set to 0.

To enable Vim to set a maximum width for text that is inserted, set this option: :set textwidth=80

If you have existing text that was not set to this width or you have shortened some lines all of that can be fixed too: mark them visually and press gq.

A final feature needed from a plain text editor is to be able to center a line. This is useful for example for titles and section headings. To do that, mark that line visually and type :center. This only works if the textwidth option is set.

Tried with: Vim 8.0 and Ubuntu 18.04