Improving my coding efficiency in vim

I have been using Vim for most editing for about 12 years now. I think I tried it for the first time in about 2003, and quickly gave up. Then in 2004 my roommate at the time convinced me to give it another try, and I quickly got hooked. I wrote my dissertation completely in vim. I have been coding in vim since then. I frequently find myself accidentally typing hjkl in non-vim editors. I feel I am pretty efficient in vim. For the most part, my .vimrc has always been relatively simple. Over the years, I have occasionally tried out some different plugins, but usually abandoned them, because they were buggy, or slowed things down, or used too much memory. I was at a meetup last week with colleagues, and my boss kept inquiring about how I can program efficiently without an IDE. This got me thinking the last couple days, as I found myself programming in PHP, and frequently looking up documentation on the net. I program in a lot of different languages. Currently, I am doing a lot of PHP and javascript, but from 2010-2015, I was mostly doing python, Java, and perl. I frequently forget the exact syntax of a lot of languages, which slows me down. Someone else at the meetup mentioned something about autocompletion in vim, so I decided to investigate a bit today. After about 2 hours or so of searching, installing plugins and such, I think I have something for PHP and JS now which should help improve my productivity.

Here are some pages on the web I found useful. Vim as an IDE has a lot of great info. I didn’t use all of the suggestions, but a number of them.

To make installing vim plugins easy, I used pathogen, which was itself easy to install, and seems fairly lightweight. You simple add 2 lines in your .vimrc, and then you can add vim plugins at will in your ~/.vim/bundle directory

A number of places recommended YouCompleteMe, which has completion for all sorts of different languages. I failed to get this installed, since my version of CMake was too old, and I am not currently in the position to upgrade it (using a shared host). I decided to stick with other options which were a little bit more specific.

I found some helpful general info about Omni completion

I installed this plugin with nicer php omni-completion

That plugin uses the default omnicompletion which Vim 7 supports out of the box. To use it, you start typing something, and then hit ctrl-x ctrl-o to show possible completions. This is a bit cumbersome, so instead I decided to try SuperTab, which automates a bunch of this for you, by simply pressing the TAB key, including looking both in omnicompletion, which includes built-in function names and such, as well as user completion, which is derived from the current file you are editing
I found a helpful comment for some .vimrc configuration to get supertab working nicely, with both user completion and omni completion.

Now if I start typing a function in a php file and hit tab, I get a little popup with several different options I might want (the coloring and highlighting is totally configurable – I played around with this a bit too – it’s in my .vimrc file below. At the top of the window, you get a brief synopsis of the function parameters, which is very helpful when trying to remember if you specify (needle, haystack) or (haystack,needle) for PHP functions. I configured the preview to stick around while I am still in insert mode, and then it goes away automatically. Unlike some IDEs, which will autocomplete the function parameters as well, this does not, which I prefer. I find that auto-inserting of quotes, parameters and such usually slows me down more than it speeds me up.

Screen Shot 2016-04-14 at 12.12.35

After all of this, I then decided to start looking around for javascript completion. For some reason, I was not able to find as nice of a solution. I looked at Tern for a bit, but it involves installing a lot of stuff, running a node server, and a bunch of configuration files. I wasn’t able to get it working really. YouCompleteMe probably would be a great solution for everything, if only I had a more recent OS. I did find a simple javascript completion vim plugin, which offers some decent code completion, although it doesn’t open a little preview with command arguments like the PHP one.

And finally, here is my updated .vimrc file with these new goodies.

" pathogen easy plugin management
execute pathogen#infect()
filetype plugin indent on
syntax on "highlight syntax
set bg=dark "use dark background
color desert "use this color scheme

"color marklar "use this color scheme
set shiftwidth=2 "indent equals two spaces
set tabstop=2 "tabstop equals two spaces
set expandtab "should make vim use spaces instead of tabs in autoindent
" use verymagic for searching
:nnoremap / /\v
:cnoremap '<,'>s/ '<,'>s/\v
:cnoremap %s/ %s/\v

set wildmenu
set wildmode=list:longest,full
"set mouse=a
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
" DF - Also do not do this if the file resides in the $TEMP directory,
" chances are it is a different file with the same name.
" This comes from the $VIMRUNTIME/vimrc_example.vim file
augroup JumpCursorOnEdit
au!
autocmd BufReadPost *
\ if expand(":p:h") !=? $TEMP |
\ if line("'\"") > 1 && line("'\"") <= line("$") | \ let JumpCursorOnEdit_foo = line("'\"") | \ let b:doopenfold = 1 | \ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
\ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
\ let b:doopenfold = 2 |
\ endif |
\ exe JumpCursorOnEdit_foo |
\ endif |
\ endif
" Need to postpone using "zv" until after reading the modelines.
autocmd BufWinEnter *
\ if exists("b:doopenfold") |
\ exe "normal zv" |
\ if(b:doopenfold > 1) |
\ exe "+".1 |
\ endif |
\ unlet b:doopenfold |
\ endif
augroup END
set hlsearch "highlight searches
set backspace=2 "better backspacing
set nocompatible "use vim features not compatible with vi
set showmatch " match parentheses
set ruler "display line and column number at bottom of screen
set ignorecase "ignore case in search and replace
set smartcase "don't ignore case if Upper Case letters appear in search

"do word wrapping for txt and tex files
if has("autocmd")
autocmd BufRead *.tab,tsv,txt set tw=78 noexpandtab
autocmd BufRead *.tex set tw=78 ai spell spelllang=en_us
autocmd BufRead *.{groovy,m,pl,grxml,xml,r} set tw=78 ai
autocmd BufRead *.{grxml,xml,py} set foldenable foldmethod=indent foldlevel=0
autocmd BufRead *.py set tw=76 sts=4 sta tabstop=4 shiftwidth=4 smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
autocmd BufRead *.{php,js,css,scss,html} set foldenable foldmethod=indent foldlevel=0 tw=78 noexpandtab smartindent tabstop=2 shiftwidth=2
autocmd BufWritePre *.{php,js,css,scss,html} :%s/\s\+$//e
autocmd FileType javascript setlocal omnifunc=js#CompleteJS
endif

"control-t checks spelling with aspell
"map  :w!:!aspell check %:e! %

"control-l checks spelling with aspell with tex mode on
"map :w!:!aspell check -t %:e! %

set encoding=utf-8 "use UTF-8 encoding to display non ASCII characters

"the next line searches for the entire line wherever the cursor is
"first move to beginning of line with "0"
"then yank til the end of the line
"then start a search, and use ctrl-R which inserts something
"in this case '"' which is the contents of the latest yank
"map  0y$:/"
set spellfile=/home/robfelty/.vimspell.utf-8.add
" the next line is for the timepie todo list macro
"map  :wI!:%!$HOME/timepie/tskproc.pl
" copy tags from previous line to current line
""" mark b, search backwards for :, move to next space or end of line.
""" append a space, mark it a,
""" move to beginning of line, search forwards to :, yank to mark a
""" move to b, then paste
"map  mb?:/\(\s\\|$\)a ma0/:y`a`bp
"imap  
"replace tags on this timepie line with those from the prev line.
"map mzk0wmx/\([\\|$\)bea mc`x"vy`cj0wd/\([\\|$\)h"vpa `zj
map mzk0el"vy/\([\\|$\)jd/\([\\|$\)h"vp`zj
"cool stuff for python

"pyfile ~/bin/vim.py
"map ) :python pythonblockFind()
"map ( :python pythonblockFind(forward = 0)

"inflect german adjectives!
map "zyiwddOi "zpa"zpae"zpaes"zpaen"zpaem"zpaer
"delete current line along with the previous and next line
map k3dd

" --- Autocompletion stuff ---
" supertab (completefunc) + latex-box (omnifunc)
let g:SuperTabDefaultCompletionType = "context"
let g:SuperTabContextDefaultCompletionType = ""
let g:SuperTabCompletionContexts = ['s:ContextText', 's:ContextDiscover']
let g:SuperTabContextDiscoverDiscovery = ["&omnifunc:"]
autocmd FileType *
\ if &omnifunc != '' |
\ call SuperTabChain(&omnifunc, "") |
\ call SuperTabSetDefaultCompletionType("") |
\ endif

" If you prefer the Omni-Completion tip window to close when a selection is
" made, these lines close it on movement in insert mode or when leaving
" insert mode
"autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
autocmd InsertLeave * if pumvisible() == 0|pclose|endif

hi Search term=reverse ctermbg=7 ctermfg=1
hi Pmenu guifg=#00ffff guibg=#000000 ctermbg=0 ctermfg=6
hi PmenuSel guifg=#ffff00 guibg=#000000 gui=bold ctermbg=7 cterm=bold
hi PmenuSbar guibg=#204d40 ctermbg=6
hi PmenuThumb guifg=#38ff56 ctermfg=3

Join 165 other subscribers

archives

  • 2024 (10)
  • 2023 (8)
  • 2022 (15)
  • 2021 (19)
  • 2020 (1)
  • 2019 (1)
  • 2018 (2)
  • 2017 (1)
  • 2016 (2)
  • 2015 (5)
  • 2014 (5)
  • 2013 (2)
  • 2011 (7)
  • 2010 (10)
  • 2009 (50)
  • 2008 (28)
  • 2007 (31)
  • 2006 (8)

Category