Vim

From Koha Wiki
Jump to navigation Jump to search

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:

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

if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

" Specify a directory for plugins
" - For Neovim: stdpath('data') . '/plugged'
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

Plug 'vim-perl/vim-perl'
Plug 'scrooloose/syntastic'
Plug 'bling/vim-airline'
Plug 'tpope/vim-fugitive'
Plug 'prettier/vim-prettier', { 'do': 'yarn install' }

" Initialize plugin system
call plug#end()

"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 <F6> :call DoTidy()<CR>

"shortcut for visual mode to run on the the current visual selection"
au Filetype perl vmap <F5> :Tidy<CR>

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

" Enable HTML matching tag jumping with shift-% just like matching bracket jumping
runtime macros/matchit.vim

"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("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif