Configuring Built-in Vim on Mac OS X 10.8

While Mac OS X 10.8 ships with vim, it doesn't ship with any vim infrastructure--like a .vimrc file!

Here is how to setup the built-in vim to be more useful. Note that these tips are not tested on MacVim which is a standard Mac App that makes vim a GUI app. I have run the equivalent in Windows XP and  7. I so far have not needed this on my Mac.

Vim Infrastructure
Enter these commands to create a vim infrastructure:

touch ~/.vimrc
mkdir ~/.vim
cd ~/.vim;mkdir backups;mkdir colors;mkdir swaps;mkdir undo;

Go to vimninjas.com and pick a color theme. I like Disgruntled.

Download the color theme file, like disgruntled.vim and save it in ~./vim/colors.
If you get errors about "^M", you'll have to edit the file later and remove these with:

:%s/Ctrl+v,<Enter>//g

Note: In case you don't recognize the above vim command, you don't type what I show. Instead, you have to hold down the ctrl key, press "v", release both and press "Enter". Vim will display "^M".

vimrc
Copy and paste the following code into your ~/.vimrc file:


" Make Vim more useful
set nocompatible
" Use the OS clipboard by default (on versions compiled with `+clipboard`)
" Set color scheme!¬
colorscheme disgruntled¬
set clipboard=unnamed
" Enhance command-line completionset wildmenu" Allow cursor keys in insert mode
set esckeys
" Allow backspace in insert modeset backspace=indent,eol,start" Optimize for fast terminal connections
set ttyfast
" Add the g flag to search/replace by default
set gdefault" Use UTF-8 without BOM
set encoding=utf-8 nobomb
" Change mapleader
let mapleader=","
" Don’t add empty newlines at the end of files
set binary
set noeol
" Centralize backups, swapfiles and undo historyset backupdir=~/.vim/backupsset directory=~/.vim/swapsif exists("&undodir")
        set undodir=~/.vim/undo
endif
set viminfo+=! " make sure vim history worksset wmh=0 " reduces splits to a single line 

" Enable per-directory .vimrc files and disable unsafe commands in themset exrcset secure" Enable syntax highlighting
syntax on
" Highlight current lineset cursorline" Make tabs as wide as two spaces
set tabstop=2
" Enable line numbersset number" Show “invisible” characters
set lcs=tab:\ ,trail:·,eol:¬,nbsp:_
set list
" Highlight searchesset hlsearch" Ignore case of searches
set ignorecase
" Highlight dynamically as pattern is typedset incsearch" Always show status line
set laststatus=2
" Respect modeline in filesset modelineset modelines=4
" Enable mouse in all modes
set mouse=a
" Disable error bellsset noerrorbells" Don’t reset cursor to start of line when moving around.
set nostartofline
" Show the cursor positionset ruler" Don’t show the intro message when starting Vim
set shortmess=atI
" Show the current modeset showmode" Show the filename in the window titlebar
set title
" Show the (partial) command as its being typedset showcmd
" Start scrolling three lines before the horizontal window border
set scrolloff=3

" Strip trailing whitespace (,ss)
function! StripWhitespace()
        let save_cursor = getpos(".")
        let old_query = getreg('/')
        :%s/\s\+$//e
        call setpos('.', save_cursor)
        call setreg('/', old_query)
endfunction
noremap <leader>ss :call StripWhitespace()<CR>
" Save a file as root (,W)
noremap <leader>W :w !sudo tee % > /dev/null<CR>

" Automatic commandsif has("autocmd")
        " Enable file type detection
        filetype on
        " Treat .json files as .js
        autocmd BufNewFile,BufRead *.json setfiletype json syntax=javascript
endif
hi Normal ctermbg=black ctermfg=white

Note: This code is only slightly modified from Tim's. I made a grand total of three changes because his code did not work for me as is. But I want to give him credit for the bulk of this code.

Comments

Popular posts from this blog

Authentication for RESTful APIs

Security From Happiness

Actually Getting to Least Privilege in AWS Lambda