ViSnips

From Custard
Jump to: navigation, search

Contents


tags

  • find . -name '*.[ch]' | xargs ctags
  • <C-]> Jumpto function
  • <C-t> Return to call

Setting up Vim for Programming

Setup your .exrc (or .vimrc) file

This file is read by vi and vim on startup.

set tabstop=8
set expandtab
set shiftwidth=8
syntax on

This example will set up your vim session to use tabstops of 8 spaces. Pressing tab will tab to the next tabstop filling in with spaces, and >> and << should work ok too. This also enables syntax highlighting. You may also have to set up your xterm and putty to get the coloring to work properly.

Setup your terminal for color

In an xterm:

export TERM=color_xterm

And in Putty, when setting up a new session, choose Connection->Data from the left, and find Terminal-type string. This will probably be 'xterm'. Change it to 'color_xterm'.

Change Connection->Data->Terminal-type string to color_xterm

Set up alias to vim

If you have vi and vim on the same system, you might like to alias vi.

alias vi=vim

These tips work on my Solaris9 / Putty setup. YMMV

Python macros

  • C-p tm - insert testMethod
  • C-p ts - insert unit test skeleton

.vimrc

:imap <C-p>tm <Tab>def testMethod(self):
:imap <C-p>ts #!/usr/bin/python<CR><CR>from myclass import MyClass<CR>import unittest<CR><CR>class MyClassTest(unittest.TestCase):<CR><CR><Tab>def setUp(self):<CR><Tab><Tab>pass<CR><CR><Tab>def tearDown(self):<CR><Tab><Tab>pass<CR><CR><Tab>def testMethod(self):<CR><Tab><Tab>self.assertEqual(1,1)<CR><CR>if __name__ == '__main__':<CR><Tab>unittest.main()

Python Tab completion

Example .vimrc

filetype plugin on
let g:pydiction_location='$HOME/.vim/complete-dict'
:imap <C-p>tm <Tab>def testMethod(self):
:imap <C-p>ts #!/usr/bin/python<CR><CR>from myclass import MyClass<CR>import unittest<CR><CR>class MyClassTest(unittest.TestCase):<CR><CR><Tab>def setUp(self):<CR><Tab><Tab>pass<CR><CR><Tab>def tearDown(self):<CR><Tab><Tab>pass<CR><CR><Tab>def testMethod(self):<CR><Tab><Tab>self.assertEqual(1,1)<CR><CR>if __name__ == '__main__':<CR><Tab>unittest.main()
:imap <C-p>tskip <Tab>@unittest.skip("Skip this test")
:vmap \em :call ExtractMethod()<CR>
function! ExtractMethod() range
  let name = inputdialog("Name of new method:")
  '<
  exe "normal! O\<BS>private " . name ."()\<CR>{\<Esc>"
  '>
  exe "normal! oreturn ;\<CR>}\<Esc>k"
  s/return/\/\/ return/ge
  normal! j%
  normal! kf(
  exe "normal! yyPi// = \<Esc>wdwA;\<Esc>"
  normal! ==
  normal! j0w
endfunction
set expandtab
set tabstop=4
set shiftwidth=4

Finding Subs in Perl

.vimrc

set expandtab
set tabstop=4
set shiftwidth=4

function! Subfinder()
    let [subline,subpos] = searchpos('^sub ','bnW')
    return getline(subline)
endfunction
set statusline=%<%f\ {%{Subfinder()}}\ %h%w%m%r%y%=L:%l/%L\ (%p%%)\ C:%c%V\ B:%o

Cheat Sheet

Personal tools