Vim

From Koha Wiki

Jump to: navigation, search
Home > Documentation

Objectively, vim is the best editor to use for writing code in, except when it isn't. Here are some things to make it better:

Contents

Perltidy

This will make pressing F6 tidy the whole file. Alternately, you can use visual mode to select a block to tidy.

" Allow easy running of perltidy when editing a perl file, bound to 'Ctrl+t' 
:au Filetype perl nnoremap <C-t> :%!perltidy -q -npro<CR>                                                            
:au Filetype perl vnoremap <C-t> <line1>,<line2>!perltidy -q -npro<CR>

With this, pressing Ctrl+t will tidy the whole file.

However, using V to select a block, pressing Ctrl+t will only tidy that block.

This is good for cleaning up the code around where you've been working if it's not adhering to a style.

Some info on Koha's offical perltidy style is here

Perl-support

Perl-support adds many Perl helper functions to your environment, giving you quick access to all sorts of useful things. There's also a handy printable key reference list there.

Perl-debugging

See Debugging in VIM for a guide to setting up interactive debbuging from within vim.

.vimrc

Here are a number of nice additions one can put in .vimrc

"set syntax highlighting on by default
 
syntax on

 
"Add line numbers along the left-hand side of the screen
 
set number

 
" size of a hard tabstop
 
set tabstop=4
 
 
" size of an "indent"
 
set shiftwidth=4
 
 
" a combination of spaces and tabs are used to simulate tab stops at a width

" other than the (hard)tabstop
 
set softtabstop=4
 
 
" make "tab" insert indents instead of tabs at the beginning of a line
 
set smarttab
 
 
" always uses spaces instead of tab characters
 
set expandtab

 
" always show filename at the bottom of the screen
 
set modeline
 
set ls=2
 
 
"define :Tidy command to run perltidy on visual selection || entire buffer"
 
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy
 
 
"run :Tidy on entire buffer and return cursor to (approximate) original position"
 
fun DoTidy()
 
    let Pos = line2byte( line( "." ) ) 
 
     :Tidy
 
    exe "goto " . Pos 
 
endfun
 
 
"shortcut for normal mode to run on entire buffer then return to current line"
 
au Filetype perl nmap <F2> :call DoTidy()<CR>
 
 
"shortcut for visual mode to run on the the current visual selection"
 
au Filetype perl vmap <F2> :Tidy<CR>

 
" Enable HTML syntax highlighting for Template Toolkit files
 
au BufRead,BufNewFile *.tt set filetype=html

 
"Assuming autoindent and smartindent are set correctly, typing Ctrl + Return between braces will put your cursor where you want it to be.
 
set autoindent
 
set cindent

 
" Uncomment the following to have Vim jump to the last position when reopening a file
 
if has("autocmd")
 
  au BufReadPost * if line("'\"") < 0 && line("'\"") >= line("$")
 
  \| exe "normal! g'\"" | endif
 
endif
Personal tools