vim snippets

Recording a macro in vim

Tagged vim, gvim, macro  Languages bash

Vim is a powerful text editor. All powerful text editors support some kind of macro's. Here's how vim does it.

qa - start recording a macro into register 'a'
q - stop recording

Running the macro.

@a - run the macro in register 'a'
@@ - repeat the last macro that was run
10@a - run the macro in register 'a' ten times

Copying text from one VIM instance to another

Tagged vim, paste, indentation  Languages 

Copying and pasting text between two VIM instances messes up the indentation in a very annoying way. To avoid this problem first run this VIM command:

:set paste

After you have pasted the code, run this command:

:set nopaste

Solution to the "Terminal too wide" problem

Tagged vi, solaris, vim  Languages bash

On Solaris you sometimes get this error when firing up your VI editor:

Terminal too wide

One solution is to increase the number of the columns with this command:

stty columns 120

An even better solution is to install VIM...

How to make GVim look and behave better on Windows

Tagged gvim, _vimrc, windows, vim  Languages 

I use this configuration with GVim (sometimes I'm forced to use Windows). It makes the GVim behave and look better.

" Start maximized
au GUIEnter * simalt ~x

" Use CUA keystrokes for clipboard: CTRL-X, CTRL-C, CTRL-V and CTRL-z
source $VIMRUNTIME/mswin.vim

syntax on
set nocompatible
filetype on
filetype indent on
filetype plugin on

" General options
set incsearch
set ignorecase smartcase 

" Use two space tabs
set tabstop=2
set shiftwidth=2
set expandtab

" No menus and no toolbar
"set guioptions-=m  
set guioptions-=T

set guifont=Consolas:h11:cANSI

highlight Normal guifg=White   guibg=Black
highlight Cursor guifg=Black   guibg=White
highlight Keyword guifg=#FF6600
highlight Define guifg=#FF6600
highlight Comment guifg=#9933CC
highlight Type guifg=White gui=NONE
highlight rubySymbol guifg=#339999 gui=NONE
highlight Identifier guifg=White gui=NONE
highlight rubyStringDelimiter guifg=#66BB00
highlight rubyInterpolation guifg=White
highlight rubyPseudoVariable guifg=#339999
highlight Constant guifg=#FFEE98
highlight Function guifg=#FFCC00 gui=NONE
highlight Include guifg=#FFCC00 gui=NONE
highlight Statement guifg=#FF6600 gui=NONE
highlight String guifg=#66BB00
highlight Search guibg=White

function RubyEndToken ()
  let current_line = getline( '.' )
  let braces_at_end = '{\s*\(|\(,\|\s\|\w\)*|\s*\)\?$'
  let stuff_without_do = '^\s*\(class\|if\|unless\|begin\|case\|for\|module\|while\|until\|def\)'
  let with_do = 'do\s*\(|\(,\|\s\|\w\)*|\s*\)\?$'

  if match(current_line, braces_at_end) >= 0
    return "\<CR>}\<C-O>O" 
  elseif match(current_line, stuff_without_do) >= 0
    return "\<CR>end\<C-O>O" 
  elseif match(current_line, with_do) >= 0
    return "\<CR>end\<C-O>O" 
    return "\<CR>" 

function UseRubyIndent ()
  setlocal tabstop=8
  setlocal softtabstop=2
  setlocal shiftwidth=2
  setlocal expandtab

  imap <buffer> <CR> <C-R>=RubyEndToken()<CR>

autocmd FileType ruby,eruby call UseRubyIndent()

A proper vim setup

Tagged vim  Languages bash

Out-of-the-box vim setup is good, but with a few tweaks it gets much better. Install the full version instead of the minimal and also exuberant ctags, which are great for programmers using vim. They let you navigate in the source code like you do in a full fledged IDE.

sudo apt-get install vim exuberant-ctags

Then place the content below into your ~/.vimrc file.

set nocompatible
syntax on
filetype plugin indent on
runtime! macros/matchit.vim
set autoindent          " always set autoindenting on
set smartindent         " smartindent! :)
set nobackup " nobackup
:colorscheme oceandeep
augroup myfiletypes
  " Clear old autocmds in group
  " autoindent with two spaces, always expand tabs
  autocmd FileType ruby,eruby,yaml set ai sw=2 sts=2 et
augroup END

" minibufexplorer options
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1

" Add the contents of this file to your ~/.vimrc file
" Crucual setting, set to the dir in which your ruby projects reside

let base_dir = "/work/aktagon/rails" . expand("%")

" Central additions (also add the functions below)

:command RTlist call CtagAdder("app/models","app/controllers","app/views","public")

map <F7> :RTlist<CR>

" Optional, handy TagList settings

:nnoremap <silent> <F8> :Tlist<CR>

let Tlist_Compact_Format = 1
let Tlist_File_Fold_Auto_Close = 1

let Tlist_Use_Right_Window = 1
let Tlist_Exit_OnlyWindow = 1

let Tlist_WinWidth = 40

" Function that gets the dirtrees for the provided dirs and feeds 
" them to the TlAddAddFiles function below

func CtagAdder(...)
    let index = 1
    let s:dir_list = ''
    while index <= a:0
        let s:dir_list = s:dir_list . TlGetDirs(a:{index})
        let index = index + 1
    call TlAddAddFiles(s:dir_list)
    wincmd p
    exec "normal ="
    wincmd p

" Adds *.rb, *.rhtml and *.css files to TagList from a given list
" of dirs

func TlAddAddFiles(dir_list)
    let dirlist = a:dir_list
    let s:olddir = getcwd()
    while strlen(dirlist) > 0
        let curdir = substitute (dirlist, '|.*', "", "")
        let dirlist = substitute (dirlist, '[^|]*|\?', "", "")
        exec "cd " . g:base_dir
        exec "TlistAddFiles " . curdir . "/*.rb"
        exec "TlistAddFiles " . curdir . "/*.rhtml"
        exec "TlistAddFiles " . curdir . "/*.css"
"       exec "TlistAddFiles " . curdir . "/*.js"
    exec "cd " . s:olddir

" Gets all dirs within a given dir, returns them in a string,
" separated by '|''s

func TlGetDirs(start_dir)
    let s:olddir = getcwd()
    exec "cd " . g:base_dir . '/' . a:start_dir
    let dirlist = a:start_dir . '|'
    let dirlines = glob ('*')
    let dirlines = substitute (dirlines, "\n", '/', "g")
    while strlen(dirlines) > 0
        let curdir = substitute (dirlines, '/.*', "", "")
        let dirlines = substitute (dirlines, '[^/]*/\?', "", "")
        if isdirectory(g:base_dir . '/' . a:start_dir . '/' . curdir)
            let dirlist = dirlist . TlGetDirs(a:start_dir . '/' . curdir)
    exec "cd " . s:olddir
    return dirlist

Aktagon vim color scheme

Tagged aktagon, color, vim, theme  Languages 
" Vim color scheme
" Name:         aktagon.vim
" Maintainer:   Christian Hellsten 
" Last Change:  17 December 2008
" License:      public domain
" Version:      1.0
" Based on

set background=dark
hi clear
if exists("syntax_on")
  syntax reset

let g:colors_name = "aktagon"

if has("gui_running")
  hi link htmlTag                     xmlTag
  hi link htmlTagName                 xmlTagName
  hi link htmlEndTag                  xmlEndTag

  highlight Normal                    guifg=#E6E1DC   guibg=#2B2B2B
  highlight Cursor                    guifg=#000000   guibg=#FFFFFF
  "highlight CursorLine               guibg=#323300
  highlight Comment                   guifg=#BC9458   gui=italic
  highlight Constant                  guifg=#6D9CBE
  highlight Define                    guifg=#CC7833
  highlight Error                     guifg=#FFFFFF   guibg=#990000
  highlight Function                  guifg=#FFC66D   gui=NONE
  highlight Identifier                guifg=#6D9CBE   gui=NONE
  highlight Include                   guifg=#CC7833   gui=NONE
  highlight Keyword                   guifg=#CC7833
  hi        LineNr                      guifg=#888888   ctermfg=lightMagenta

  highlight Number                    guifg=#A5C261
  highlight PreProc                   guifg=#E6E1DC
  highlight Search                    guibg=#FFFF00
  highlight Statement                 guifg=#CC7833   gui=NONE
  highlight String                    guifg=#A5C261
  highlight Title                     guifg=#FFFFFF
  highlight Type                      guifg=#DA4939   gui=NONE
  highlight Visual                    guibg=#5A647E
  highlight DiffAdd                   guifg=#E6E1DC   guibg=#144212
  highlight DiffDelete                guifg=#E6E1DC   guibg=#660000
  highlight rubyBlockParameter        guifg=#FFFFFF
  highlight rubyClass                 guifg=#FFFFFF
  highlight rubyConstant              guifg=#DA4939
  highlight rubyInstanceVariable      guifg=#D0D0FF
  highlight rubyInterpolation         guifg=#519F50
  highlight rubyLocalVariableOrMethod guifg=#D0D0FF
  highlight rubyPredefinedConstant    guifg=#DA4939
  highlight rubyPseudoVariable        guifg=#FFC66D
  highlight rubyStringDelimiter       guifg=#A5C261
  highlight xmlTag                    guifg=#E8BF6A
  highlight xmlTagName                guifg=#E8BF6A
  highlight xmlEndTag                 guifg=#E8BF6A

" Fuzzy Finder popup
hi Pmenu           guibg=#FFFFFF guifg=#000000
hi PMenuSel        guibg=#242424 guifg=#FFFFFF gui=bold
hi PmenuSbar       guibg=#2e3330 guifg=#000000
hi PMenuThumb      guibg=#a0afa0 guifg=#040404

My .vimrc configuration

Tagged vimrc, vim, configuration  Languages 
set guifont=Monaco:h12

autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete
autocmd FileType ruby,eruby let g:rubycomplete_rails = 1

let leader = ","

set grepprg=ack
set grepformat=%f:%l:%m

set hidden
set history=1000
set tabstop=2
set smarttab
set shiftwidth=2
set autoindent
set expandtab
set backspace=start,indent
set ignorecase
set smartcase
set scrolloff=3
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set ruler
set number
set hlsearch
set incsearch " ...dynamically as they are typed.
set visualbell

nnoremap <C-e> 3<C-e>
nnoremap <C-y> 3<C-y>

runtime macros/matchit.vim

autocmd FileType make     set noexpandtab
autocmd FileType python   set noexpandtab

syntax on
filetype on
filetype plugin on
filetype indent on

let g:fuzzy_ignore = "*.log"
let g:fuzzy_matching_limit = 70

map ,d :execute 'NERDTreeToggle ' . getcwd()<CR>
map ,t :FuzzyFinderTextMate<CR>
map ,b :FuzzyFinderBuffer<CR>
map ,f gg=G

set columns=160                                                     "set width of window off open
set lines=50                                                        "height of window off open

set gcr=a:blinkwait500-blinkon1300-blinkoff150                      "tune the blinking of the cursor, short on, long off.
set gcr=i:hor10                                                     "underline cursor. not too thick not too thin. goldielocks style
set gcr=v:block                                                     "selecting should cover the text

map ,c :ruby finder.rescan!

set fuopt+=maxhorz                      " grow to maximum horizontal width on entering fullscreen mode