Some useful options for Vim

Learning Vim is an everlasting effort. Here are a couple of good options you don’t want to miss to make your experience a little bit more smooth.

You may either place the commands into your ~/.vimrc or execute the commands in Vim. In the latter case don’t forget to change to the command mode with <Esc> and a colon (:), i.e. :set tabstop=4 ....

Change tabs to spaces

Use the following command to set tabs to four (4) spaces:

set tabstop=4 shiftwidth=4 expandtab

The tabstop will set the length of tab to four columns regardless of the character used in the file. The shiftwidth specifies the number of spaces when (auto)indenting. And finally expandtab enables the use of spaces instead of tabs.

Enable line wrapping

You have two options here: wrap with or without line breaks. The first option adds hard line breaks to the file. The second option just wraps long lines on the screen without modifying file contents.

To have Vim insert a hard line break whenever a certain threshold is exceeded use the following command:

set textwidth=80

textwidth limits the line length to 80 characters and lLine break is added to the file when exceeding the specified limit. By default Vim puts the line break after the last complete word that fits in the desired width.

Sometimes it’s nice to keep the long lines as-is in the file but wrap them on the screen:

set wrap linebreak nolist wrapmargin=0 textwidth=0

wrap enables lines longer than the window to be wrapped. This makes it suitable for any window and screen size.linebreak enables Vim to be smart on line breaks by wrapping on suitable characters instead of immediately at the specified length. If you want to see $ where line breaks are used, use list to enable the symbol while nolist hides them. wrapmargin can be used to define the margin on the right window border. Finally textwidth=0 explicitly disables adding physical line breaks to files (unless you press <Enter>, of course).

Word of caution: Don’t forget you’re still editing a single line even if it’s wrapped on the screen. If you use <Esc>dd to delete a line it will remove the whole paragraph (as there are no line breaks in the file).

Of course this is just the tip of the ice berg and there’s plenty of other useful options to be covered.

Please let me know if you have any other good tips to share!

Written on February 5, 2018