If you're anything like me, you:
- Like Vim a lot
- Know that a .vimrc file can make Vim work the way you want it to
- Copy-pasted a bunch of commands into your .vimrc to make it functional but don't really know what they all do
Today I decided to clean up my .vimrc to create something that's pretty bare bones, but does what I want and need it to do. Even better, I actually know what each line does!
If you're just looking for a starter .vimrc with some comments, copy-paste this file and run away.
" From: https://github.com/AGnias47/UtilityScripts/blob/master/bash/vimrc " Don't make efforts to make Vim VI-compatible set nocompatible " Turn on filetype detection :filetype on " Turn on syntax highlighting if more than 1 color is available if &t_Co > 1 syntax enable endif " Turn on auto-indentation for C-syntax languages :au FileType c,cpp,java set cindent " Show matching brackets set showmatch " Set one depending on terminal type set background=dark " set background=light " Makes backspace behave as expected set backspace=2 "Set the tab key to 4 spaces set tabstop=4 set softtabstop=4 set shiftwidth=4 set expandtab set smarttab " Turn on visual wrapping set wrap "Wrap at 120 characters set textwidth=120 " Turn on highlighting for searching set hlsearch " Show cursor line and column position set ruler
Below are some of the finer details of what all these commands actually do.
A staple for any .vimrc file is
set nocompatible. It basically gives you some of the more useful features of Vim at the cost of making it less compatible with Vi. If you've never used Vi and / or are not sure why you would want this setting, add this to your .vimrc.
Most of the syntax settings are explained clearly enough with the comments provided (Anything preceded by a
" is a comment). The only outlier here is the
if statement wrapping
syntax enable. It's based on the condition that
&t_Co is greater than 1.
t_Co represents the number of terminal colors, so we're saying, "turn on syntax highlighting as long as we have more than 1 color to work with".
Tabs can be a controversial subject. Personally, I find it easiest to go with the most common paradigm, at least in 2020, and use 4 space characters as my indentation marker. This .vimrc is set up so that hitting the tab key will auto-generate 4 spaces and not a tab character. The settings that make that happen are included below. The Vim help guide and this article really helped me understand them better.
- tabstop - Number of columns that a tab displays as
- softtabstop - Number of columns to indent when hitting the tab key
- shiftwidth - Number of columns to indent when using shift operations
- expandtab - Convert tab key to specified number of spaces
- smarttab - Insert blanks when hitting tab in front of a line
The Vim help guide does make a note that setting
tabstop to a value other than 8 can "make your file appear wrong in many places (e.g., when printing it)", but personally I have not had issues with using a different tabstop value.
Last thing to note here is the
set backspace=2. In some instances, the backspace and delete keys will not work as you expect them to. Fortunately, this can typically be resolved through some troubleshooting with the help of the Vim Backspace and delete problems Wiki. Working on Ubuntu, I had some issues using the backspace key not erasing at the end of a line, and enabling this setting fixed that issue for me. A more general setting as suggested in the guide, such as
set backspace=indent,eol,start should be good enough to resolve most issues for most users.
Obviously there's much more you can do to with this file to improve your Vim experience. Many Vim users have provided some fantastic, and detailed, .vimrc files. My only recommendation in using someone else's settings is to understand what their commands are actually doing, which can usually be found out through a Google search or by using the help command in Vim (ex.
:help some_weird_setting_I_dont_understand). If you're just starting out, or have been using and loving Vim for a while, hopefully this gives you a greater appreciation of what a basic .vimrc can do for you and how it does it.