Modeline in vim

ยท 180 words ยท 1 minute read

Options set in .vimrc applies to all vim files. You can use modelines to set specific vim options for one file. Modeline option must be set in .vimrc to take advantage of this option.

set modeline

The modeline should be the first line in the file. You can set it inn multiple ways as shown below.

# vim: set ai et ts=4 sts=4 sw=4 tw=79 cc=80 wrap wb nu sm:
/* vim: set enc=UTF-8 ai et ts=4 sts=4 sw=4 tw=79 cc=80 wrap wb nu sm: */
// vim: set enc=UTF-8 ai et ts=4 sts=4 sw=4 tw=79 cc=80 wrap wb nu sm:

Explanation ๐Ÿ”—

  • enc=UTF-8
    Set encoding to UTF-8
  • ai
    Auto intend
  • et
    Expand tabs with spaces
  • ts=4
    Tab stop set to 8
  • sts=4
    Soft tab stop
  • sw=4
    Shift width, used by >> and <<
  • tw=79
    Text width
  • cc=80
    Set colour column, a colour bare at character 80
  • wrap
    Wraps text, only affects how the text is displayed
  • wb
    Write a backup before saving files
  • nu
    Show line numbers
  • sm
    Show matching brackets

More information in the Vim documentations under options.