Configuring vim for postgres development

From PostgreSQL wiki
Jump to navigationJump to search

Here is some notes and tips that will help to start using vim for postgres development.

Some recommended ~/.vimrc options

Turn on syntax highlight

 syntax on

Set tab width to 4 characters, to match code style

 set tabstop=4

You can highlight 80th column in order to see if your code fits in 80 character per line limits

 set colorcolumn=80
 highlight ColorColumn ctermbg=52

The second line here defines color of this column. 52 is for dark red. You can change it to the color you like.

Recommended plugins

Pathogen, a simple plugin manager

Vim needs a plugin manager to add plugins. pathogen is a simple one. You can install it from your OS repository (For Debian it would be vim-pathogen package) or install it following the instructions from the site. After installing you should add

 execute pathogen#infect()

to your ~/.vimrc file and create a ~/.vim/bundle dir. Put all the vim plugins in the bundle dir, they will be loaded automatically.

Filestyle, highlights style errors

Filestyle allows to highlight wrong indention, trailing spaces, and too long lines. You can install it from git.

To make it highlight code that does not comply postgres codestyle add to ~/.vimrc following lines:

 set textwidth=80

This will make filestyle to highlight tails of lines that are longer than 80 characters

 let g:filestyle_ignore_patterns = ['^\t* \{1,3}\S']

This will make filestyle not to highlight indention that has from 1 to three spaces at the end. This is acceptable case in postgres codestyle

If you are using terminal version of vim, you should also add this line

 highlight Normal ctermbg=16

This is some strange trick that is needed for filestyle, I do not completely understand, but it sets background color using terminal color numbers and 0 and 16 is for black and 15 is for white. If you use some other color, find proper value yourself...

Lastplace, return to the place, you've been editing before

If you like to start editing file from the place you've been when you've exited vim before, lastplace plugin is for you. Just install it.

Localvimrc, use different vim configurations for different projects

If you are developing different projects on same computer, you may be would not like to use postgres specific vim configure options in other projects. Then you would like localvimrc plugin. Just install it and put .lvimrc file in postgres project dir (or in the dir above it) and put all postgres specific vim options there. Vim will use them only if you load it from your postgres dir.

More recommendations

ctags

Ctags is a tool that indexes C (and not only C code) to allow editors to jump to function source by pressing some hot key.

In postgres code there is a script src/tools/make_ctags that automatically builds ctags index for all the code. You should run it from the root of the code source

 $ src/tools/make_ctags

To run it you will need to install ctags utility (for Debian it is in exuberant-ctags package)

You will also like to add tags to .gitignore file, because ctags creates a tags file in each directory in the code, it would be better if git would ignore them all.

After running make_ctags you will be able to jump to source of the function by pressing Ctrl+]

Old staff

In Developer_FAQ article had the following recommendations

For vim, a better way is to install localvimrc via Pathogen or Vundle then add a .lvimrc in your PostgreSQL directory containing:
" Works best with vimrc entries (otherwise localvimrc will complain):
"   let g:localvimrc_sandbox = 0
"   let g:localvimrc_whitelist = "/path/to/postgres/tree/.*"
"
if g:localvimrc_sourced_once_for_file
        finish
endif
au BufNewFile,BufRead *.[ch] setlocal noexpandtab autoindent cindent tabstop=4 shiftwidth=4 softtabstop=0 cinoptions="(0,t0"

Here nothing is explained, and not all options were moved to this article and explained. May be you would find this example useful. Or may be you manage to move all the items of this unexplained example to this article and add explanations.