Register now and start sharing your code snippets.

A proper vim setup

Shell Script (Bash) posted 3 months ago by marko

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 .

   1  sudo apt-get install vim exuberant-ctags

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

   1  set nocompatible
   2  syntax on
   3  filetype plugin indent on
   4  runtime! macros/matchit.vim
   5  set autoindent          " always set autoindenting on
   6  set smartindent         " smartindent! :)
   7  set nobackup " nobackup
   8  :colorscheme oceandeep
   9  augroup myfiletypes
  10    " Clear old autocmds in group
  11    autocmd!
  12    " autoindent with two spaces, always expand tabs
  13    autocmd FileType ruby,eruby,yaml set ai sw=2 sts=2 et
  14  augroup END
  15  
  16  
  17  " minibufexplorer options
  18  let g:miniBufExplMapWindowNavVim = 1
  19  let g:miniBufExplMapWindowNavArrows = 1
  20  let g:miniBufExplMapCTabSwitchBufs = 1
  21  let g:miniBufExplModSelTarget = 1
  22  
  23  
  24  
  25  
  26  " Add the contents of this file to your ~/.vimrc file
  27  "
  28  " Crucual setting, set to the dir in which your ruby projects reside
  29  
  30  let base_dir = "/work/aktagon/rails" . expand("%")
  31  
  32  " Central additions (also add the functions below)
  33  
  34  :command RTlist call CtagAdder("app/models","app/controllers","app/views","public")
  35  
  36  map <F7> :RTlist<CR>
  37  
  38  " Optional, handy TagList settings
  39  
  40  :nnoremap <silent> <F8> :Tlist<CR>
  41  
  42  let Tlist_Compact_Format = 1
  43  let Tlist_File_Fold_Auto_Close = 1
  44  
  45  let Tlist_Use_Right_Window = 1
  46  let Tlist_Exit_OnlyWindow = 1
  47  
  48  let Tlist_WinWidth = 40
  49  
  50  " Function that gets the dirtrees for the provided dirs and feeds 
  51  " them to the TlAddAddFiles function below
  52  
  53  func CtagAdder(...)
  54  	let index = 1
  55  	let s:dir_list = ''
  56  	while index <= a:0
  57  		let s:dir_list = s:dir_list . TlGetDirs(a:{index})
  58  		let index = index + 1
  59  	endwhile
  60  	call TlAddAddFiles(s:dir_list)
  61  	wincmd p
  62  	exec "normal ="
  63  	wincmd p
  64  endfunc 
  65  
  66  " Adds *.rb, *.rhtml and *.css files to TagList from a given list
  67  " of dirs
  68  
  69  func TlAddAddFiles(dir_list)
  70  	let dirlist = a:dir_list
  71  	let s:olddir = getcwd()
  72  	while strlen(dirlist) > 0
  73  		let curdir = substitute (dirlist, '|.*', "", "")
  74  		let dirlist = substitute (dirlist, '[^|]*|\?', "", "")
  75  		exec "cd " . g:base_dir
  76  		exec "TlistAddFiles " . curdir . "/*.rb"
  77  		exec "TlistAddFiles " . curdir . "/*.rhtml"
  78  		exec "TlistAddFiles " . curdir . "/*.css"
  79  "		exec "TlistAddFiles " . curdir . "/*.js"
  80  	endwhile
  81  	exec "cd " . s:olddir
  82  endfunc
  83  
  84  " Gets all dirs within a given dir, returns them in a string,
  85  " separated by '|''s
  86  
  87  func TlGetDirs(start_dir)
  88  	let s:olddir = getcwd()
  89  	exec "cd " . g:base_dir . '/' . a:start_dir
  90  	let dirlist = a:start_dir . '|'
  91  	let dirlines = glob ('*')
  92  	let dirlines = substitute (dirlines, "\n", '/', "g")
  93  	while strlen(dirlines) > 0
  94  		let curdir = substitute (dirlines, '/.*', "", "")
  95  		let dirlines = substitute (dirlines, '[^/]*/\?', "", "")
  96  		if isdirectory(g:base_dir . '/' . a:start_dir . '/' . curdir)
  97  			let dirlist = dirlist . TlGetDirs(a:start_dir . '/' . curdir)
  98  		endif
  99  	endwhile
 100  	exec "cd " . s:olddir
 101  	return dirlist
 102  endfunc
 103  

Tagged vim

How to make GVim look and behave better on Windows

Plain Text posted 7 months ago by christian

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

   1  " Start maximized
   2  au GUIEnter * simalt ~x
   3  
   4  " Use CUA keystrokes for clipboard: CTRL-X, CTRL-C, CTRL-V and CTRL-z
   5  source $VIMRUNTIME/mswin.vim
   6  
   7  syntax on
   8  set nocompatible
   9  filetype on
  10  filetype indent on
  11  filetype plugin on
  12  
  13  " General options
  14  set incsearch
  15  set ignorecase smartcase 
  16  
  17  " Use two space tabs
  18  set tabstop=2
  19  set shiftwidth=2
  20  set expandtab
  21  
  22  " No menus and no toolbar
  23  "set guioptions-=m  
  24  set guioptions-=T
  25  
  26  set guifont=Consolas:h11:cANSI
  27  
  28  highlight Normal guifg=White   guibg=Black
  29  highlight Cursor guifg=Black   guibg=White
  30  highlight Keyword guifg=#FF6600
  31  highlight Define guifg=#FF6600
  32  highlight Comment guifg=#9933CC
  33  highlight Type guifg=White gui=NONE
  34  highlight rubySymbol guifg=#339999 gui=NONE
  35  highlight Identifier guifg=White gui=NONE
  36  highlight rubyStringDelimiter guifg=#66BB00
  37  highlight rubyInterpolation guifg=White
  38  highlight rubyPseudoVariable guifg=#339999
  39  highlight Constant guifg=#FFEE98
  40  highlight Function guifg=#FFCC00 gui=NONE
  41  highlight Include guifg=#FFCC00 gui=NONE
  42  highlight Statement guifg=#FF6600 gui=NONE
  43  highlight String guifg=#66BB00
  44  highlight Search guibg=White
  45  
  46  function RubyEndToken ()
  47    let current_line = getline( '.' )
  48    let braces_at_end = '{\s*\(|\(,\|\s\|\w\)*|\s*\)\?$'
  49    let stuff_without_do = '^\s*\(class\|if\|unless\|begin\|case\|for\|module\|while\|until\|def\)'
  50    let with_do = 'do\s*\(|\(,\|\s\|\w\)*|\s*\)\?$'
  51  
  52    if match(current_line, braces_at_end) >= 0
  53      return "\<CR>}\<C-O>O" 
  54    elseif match(current_line, stuff_without_do) >= 0
  55      return "\<CR>end\<C-O>O" 
  56    elseif match(current_line, with_do) >= 0
  57      return "\<CR>end\<C-O>O" 
  58    else
  59      return "\<CR>" 
  60    endif
  61  endfunction
  62  
  63  function UseRubyIndent ()
  64    setlocal tabstop=8
  65    setlocal softtabstop=2
  66    setlocal shiftwidth=2
  67    setlocal expandtab
  68  
  69    imap <buffer> <CR> <C-R>=RubyEndToken()<CR>
  70  endfunction
  71  
  72  autocmd FileType ruby,eruby call UseRubyIndent()

Tagged gvim, _vimrc, windows, vim

Solution to the "Terminal too wide" problem

Shell Script (Bash) posted about 1 year ago by christian

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

   1  Terminal too wide

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

   1  stty columns 120

An even better solution is to install VIM …

Tagged vi, solaris, vim

Copying text from one VIM instance to another

Plain Text posted about 1 year ago by christian

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:

   1  :set paste

After you have pasted the code, run this command:

   1  :set nopaste

Tagged vim, paste, indentation

Recording a macro in vim

Shell Script (Bash) posted about 1 year ago by marko

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

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

Running the macro.

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

Tagged vim, gvim, macro