Vim - Personalization
Vim - Personalization and Configuration
Section titled “Vim - Personalization and Configuration”Vim is highly customizable. You can tailor its appearance and behavior to suit your preferences and workflow. This section explores key aspects of Vim personalization.
- Configuration files (
vimrc) - Changing fonts (for GVim/MacVim)
- Applying color schemes
- Custom highlighting and visual aids
- Customizing the status line
Configuration Files
Section titled “Configuration Files”Vim’s settings are primarily controlled by a configuration file, commonly known as vimrc. There can be system-wide and user-specific configuration files.
vimrc File
Section titled “vimrc File”User vimrc: This is where you’ll put most of your personal configurations. The location depends on your OS:
- Linux/macOS:
~/.vimrcor$XDG_CONFIG_HOME/vim/vimrc(if$XDG_CONFIG_HOMEis set, defaults to~/.config/vim/vimrc). - Windows:
$HOME/_vimrc,$HOME/vimfiles/vimrc, or$VIM/_vimrc.
For Neovim, the user configuration is typically at ~/.config/nvim/init.vim (Vimscript) or ~/.config/nvim/init.lua (Lua).
System vimrc: This file contains global defaults. Its location can be found by typing :echo $VIM/vimrc in Vim. On Linux, it’s often /etc/vim/vimrc or /usr/share/vim/vimrc. Modifying this file affects all users on the system and is generally less common for personal settings.
gvimrc File (for GVim/MacVim)
Section titled “gvimrc File (for GVim/MacVim)”If you use a GUI version of Vim like GVim or MacVim, you can have GUI-specific settings in a gvimrc file. These settings are loaded after the vimrc.
- Linux/macOS:
~/.gvimrcor$XDG_CONFIG_HOME/vim/gvimrc. - Windows:
$HOME/_gvimrc,$HOME/vimfiles/gvimrc, or$VIM/_gvimrc.
exrc File (Legacy)
Section titled “exrc File (Legacy)”The .exrc (in the home directory) or exrc (in the current directory, if set exrc is enabled for security) files are for backward compatibility with ex and older vi versions. Modern Vim usage generally prefers vimrc files. Vim will ignore .exrc if a vimrc is found in the same location.
Changing Fonts (for GVim/MacVim)
Section titled “Changing Fonts (for GVim/MacVim)”In GUI versions of Vim, you can change the font and its size using the guifont option. This setting is typically placed in your gvimrc file.
The syntax varies slightly by operating system:
" Example for Linux (GVim):set guifont=Monospace\ 10
" Example for macOS (MacVim):set guifont=Menlo:h12
" Example for Windows (GVim):set guifont=Consolas:h11:cDEFAULTSpaces in font names must be escaped with a backslash. You can often use :set guifont=* to open a font selection dialog to find the correct syntax.
Changing Color Schemes
Section titled “Changing Color Schemes”Vim supports color schemes to change the syntax highlighting and editor colors. To apply a color scheme:
:colorscheme <scheme_name>You can type :colorscheme (with a space) and then press Tab to cycle through available built-in schemes (e.g., desert, elflord, murphy). Many more can be downloaded and installed.
To set a default color scheme, add the command to your vimrc: colorscheme wombat
Personal Highlighting and Visual Aids
Section titled “Personal Highlighting and Visual Aids”Highlight Characters Beyond a Certain Column
Section titled “Highlight Characters Beyond a Certain Column”To visually indicate a line length limit (e.g., 80 characters), use colorcolumn:
:set colorcolumn=80 " Highlights the 80th column:set cc=80,100 " Highlights columns 80 and 100The old method using match is still available for more complex patterns:
:match ErrorMsg /\%>79v.\+/ " Highlights text beyond column 79Description of the match command elements: \%>79v means ‘after virtual column 79’, . matches any character, \+ matches one or more occurrences. ErrorMsg is a highlight group.
Display Line Numbers
Section titled “Display Line Numbers”To show line numbers:
:set number " Show absolute line numbers:set relativenumber " Show relative line numbers (current line is 0):set nu rnu " Show both (current line absolute, others relative)To disable: :set nonumber, :set norelativenumber.
To set the width of the number column (e.g., to 4 characters):
:set numberwidth=4Spell Check Configuration
Section titled “Spell Check Configuration”To enable spell checking:
:set spellTo set the spell checking language (e.g., British English or German):
:set spelllang=en_gb:set spelllang=deYou might need to download dictionary files for some languages. See :help spell.
Abbreviations
Section titled “Abbreviations”Abbreviations automatically expand short typed text into longer strings. They are useful for correcting common typos or inserting boilerplate text.
Define an abbreviation (works in Insert and Command mode):
:abbr adn and " Corrects 'adn' to 'and':abbr myemail user@example.comFor Insert mode only abbreviations, use :iabbrev or :iab.
Now, if you type myemail followed by a space or punctuation in Insert mode, it will expand to user@example.com.
Customizing the Status Line
Section titled “Customizing the Status Line”The status line at the bottom of each window displays information about the current file and mode. By default, it might only show when there are multiple windows.
To always show the status line:
:set laststatus=2Other values: 0 (never), 1 (only if more than one window), 3 (global status line, Neovim only).
You can extensively customize the content of the status line using the statusline option. This can involve Vim script to display various details like file name, file type, cursor position, Git branch, etc. For example, a simple custom status line:
:set statusline=%F\ %y\ %r%m\%=Line\ %l/%L,\ Col\ %cMany users opt for plugins like Airline or Lightline for highly configurable and aesthetically pleasing status lines. See :help 'statusline' for details on formatting options.