A Drupal-centric Vim Configuration

My .vimrc

In this post I'll describe my .vimrc, my vim configuration file. An important thing to keep in mind is that Vim has a handy built in help system. If there is any command that is unfamiliar to you, then you can look it up in a vim session by typing :help <command> and vim will pop open a buffer with the documentation for that command.

I generally use vim while in a ssh session on a dev server and this vimrc works well for that.


  • backupdir tells vim to put backup files in one central directory instead of littering ~ files in every directory.
  • hlsearch highlights search terms in the file after you search for them through /
  • colors abbreviation for colorscheme, sets the Vim colorscheme.

set nocompatible
" set vim to use 256 Colors if in a terminal!!

set backupdir=~/.vim/backup_dir
set directory=~/.vim/swap_dir
set title
set hlsearch

colors xoria256

Using Pathogen for Plugin Management

Installing Pathogen

Pathogen is a plugin that organizes and enables other vim plugins. After setting up pathogen, all that you need to do to install a new program, is to drop it into your "~/.vim/bundle/" directory. This can be as simple as running the following shell commands to install Tim Pope's awesome surround plugin:

cd ~/.vim/bundle/
git clone https://github.com/tpope/vim-surround.git

Setting up Pathogen in my .vimrc

  • infect() looks through .vim/bundle/ for plugins
  • helptags() looks through plugins for their help docs.

call pathogen#infect()
call pathogen#helptags()

Setting up the Status Line

set showmode "show current mode down the bottom

"Setting the status line... {{{

set statusline=%f
"tail of the filename

"display a warning if the file format isn't Unix
set statusline+=%#warningmsg#
set statusline+=%{&ff!='unix'?'['.&ff.']':''}
set statusline+=%*

"display a warning if file encoding isn't UTf-8
set statusline+=%#warningmsg#
set statusline+=%{(&fenc!='utf-8'&&&fenc!='')?'['.&fenc.']':''}
set statusline+=%*

set statusline+=%h      "help file flag
set statusline+=%y      "filetype
set statusline+=%r      "read only flag
set statusline+=%m      "modified flag
"end status line }}}

  • enable syntax highlighting
  • enable line numbers

syntax on
set number

Indentation Settings

set smartindent
set tabstop=2
set shiftwidth=2
set expandtab
set autoindent
set smartindent

Drupal Specific Settings

from the vim help doc:
Add {cmd} to the list of commands that Vim will execute automatically on {event} for a file matching {pat}

So in the line autocmd BufRead,BufNewFile *.module set filetype=php

  • BufRead and BufNewFile are buffer events called when a new or existing file is opened in a buffer.
  • ** *.module** is the pattern for drupal module files
  • set filetype=php is the command that is triggered. It set's the buffer's filetype, which is used for syntax highlighting and indentation among other things.

if has("autocmd")
  " Drupal *.module and *.install files.
  augroup module
    autocmd BufRead,BufNewFile *.module set filetype=php
    autocmd BufRead,BufNewFile *.install set filetype=php
    autocmd BufRead,BufNewFile *.test set filetype=php
    autocmd BufRead,BufNewFile *.inc set filetype=php
    autocmd BufRead,BufNewFile *.profile set filetype=php
    autocmd BufRead,BufNewFile *.view set filetype=php
  augroup END
  autocmd BufNewFile,BufRead *.twig   set syntax=html

If you'd like to read more about autocmd or augroup, type ":help autocmd" in during a vim session.

Remapping Keys

Setting up Custom Keyboard Mappings

The mapleader is a special key that prefixes all of your custom keyboard shortcuts. I use a comma as my mapleader. So if I want to toggle the plugin NERDTree, I type ,nt and NERDTree, a file browser, either opens or closes a display of the current directory.

autocmd VimEnter * wincmd p
nnoremap nn :set nonumber!
nnoremap gt :GitGutterToggle
nnoremap gn :GitGutterNextHunk
nnoremap gp :GitGutterPrevHunk

" adding keybindings for Gdiff
"vimdiff current vs git head (fugitive extension)
nnoremap gd :Gdiff
"switch back to current file and closes fugitive buffer
nnoremap gD :diffoff!h:bd

" a lit bit of emacs in insert mode
inoremap  $
inoremap  0

nnoremap  :buffers:buffer

map w!! to "sudo save current file"

cmap w!! %!sudo tee > /dev/null %

Add new comment