Vim Configuration

For those of you who use vim, vim comes with a bunch of settings you can use to tune the editor to how you want vim to work.

When vim first opens, it looks for an initialization file, .vimrc, in your home directory, ~/.vimrc in Mac and Linux, and $HOME/.vimrc in Windows.  Let's take a look at a fairly simple .vimrc file before getting started.

A simple .vimrc file

 

The .vimrc will not be there when you first configure a system, you'll have to create it.  Let's do that, in your home directory, type: "touch .vimrc"  Now that we have a configuration file lets see what we can do with the editor.  If you have been typing a bunch of switches when you open vim, you'll find we can put them in the .vimrc file and save you some typing.

Let's get started.  Open up .vimrc in vim and put at the very top of the file the following line, "set nocompatible" without the quotes.  This has to be the first line in the file.  "set nocompatible" tells vim that you are going to use the vim variables in this file, rather than the standard vi configuration.  Comments in the .vimrc are done with a single double quote.  Variables should be one to a line.

The easiest way to do this is just list the options and what they do.

set nocompatible    " use vim defaults.  MUST BE FIRST LINE

" LAYOUT
set number          " show line numbers. Handy when you get error messages with line numbers.
set nu                " show line numbers, same as above
set title           " show title of the file in vim title bar
set ls=2            " show status bar with file path and name
set showcmd         " displays an incomplete command in the lower right of the window
set ruler           " show the cursor vertical and horiz position in the lower right corner, or right status line
set nowrap            " don't wrap lines
set showmode        " show the current mode of the editor

set history=1000    " keep the last 1000 commands and last 1000 search patterns
set background=dark " make background dark, i.e black
set mouse=a            " enable mouse usage
set virtualedit=onemore    " allow cursor one line beyond last line
set spell            " turn on spell checking
set backspace=indent,eol,start  " cut blank spaces at the front of the line, end of line, and before the insert cursor when starting insert mode.

" MOVEMENT AND FORMATTING
set ttyfast            " smoother changes
set ttyscroll=0        " turn off scrolling
set autoindent        " indents if previous line was indented
set noautoindent    " no autoindent
set smartindent        " smart indenting
set nosmartindent    " turn off smart indent
set cindent            " cindent
set nocindent        " no cindent

"FOR SEARCHING
set hlsearch        " highlight searches.  One of my personal favorites.  All valid search results are highlighted in color.
set incsearch       " do searching as characters are typed in the search pattern
set ignorecase      " ignore case when searching
set noignorecase    " don't ignore case
set sm                " show matchin braces
syntax on            " syntax highlighting

"FOR KEYMAPPING  bind a single key to vim commands
:map <F2> :previous    " maps F2 key to open previous buffer
:map <F3> :next        " maps F3 to open next buffer

" LINE INDENTING
set tabstop=4       " numbers of spaces for the tab character
set expandtab        " tabs are converted to spaces
set shiftwidth=4    " numbers of spaces to (auto)indent
set scrolloff=3     " keep 3 lines when scrolling

"ERROR BEEPS
set visualbell    t_vb     " turn off visual errors alert both the flash(v) and the beep(b)
set novisualbell    " turn off visual bell error alert

" SAVING
set nobackup        " do not keep a backup file
set autowrite        " automatically saves changes when quitting or switching buffers

" CREATE A BACKUP COPY, but not for vim itself
if has("vms")
set nobackup
else
set backup
endif

This list is not in any way inclusive.   The .vimrc file can get quite extensive, and there are many examples of .vimrc files on the Internet that are quite lengthy.  What most folks don't know is that there are plugins that can be configured with vim to do a whole lot more than basic editing.  Plugins are another topic which I'll cover in another post.   We can add plugins, change the cursor, color any aspect of the editor you would like including syntax coloring, but this list should get you started.  For more information see the actual vim documentation.

In order for any of your changes in .vimrc to take effect you will have to reopen your terminal window, or run "source .vimrc" at the command line.  Bear in mind, not all of the above setting may work as the different linux distros handle things slightly differently.  If a command is not available, vim will tell you when you source .vimrc.

Comments

Vim Configuration — 1 Comment