I've always been a vi-hater, until I really came to know it. As most Unix newbes I first did not know in a way how to deal with vi in general and therefore never even touched it for years (I guess in the very first weeks, I did not even know, it exists).
After some month on Linux, someone threw me into cold water and told me to use vi for editing files and I tried, what resulted in the usual process (I imagine):
"Damn, now I'm in here, but I cannot type anything!" *argh``*``
"Bloody shit, I cannot even exit it anymore" *grmpf``*``
By mid of this year some friends tried to evangelise me to use vi and, I bet you know, the were successful. Even that successful, that I bought "Learning the vi editor"*a few weeks after they convinced me to try out vim. This is now nearly 6 month ago and I slowly start really getting used to some of it's neat functions and have some fixed set of settings, I'd like to provide to others.
Beside the usual options like autoindent, smartindent, nowrap and number, I created some nice shortcuts (for checking PHP syntax and for running "pear package" directly from the editor. But the most important feature I really like is creating code skeletons on the fly, using key mappings.
So, what does that basically mean? There are quite some constructs in PHP which you really use often, like if-statements, foreach-loops, class/method/attribute-definitions and so on. Usually people write that most of these once in a file (mostly with those definition parts) and then copy and paste the stuff. What I do since a few month now, is just typing (e.g.) "=cla" in vim's editing mode and a class defintion (including comments) appears and leaves me on a place where I can comfortably start filling it. Same for the other named constructs and some more.
I guess I sped up my code-writing speed by about 15% with that and it's pretty comfortable. So, if you like, check my current .vimrc here and try it out! Feedback, additions and hints are pretty welcome! :)
*A big recommendation on that book to everyone out there who's new to vi or one of it's clones!
Try adding this to your vimrc:
set dictionary-=/usr/share/funclist.txt dictionary+=/usr/share/funclist.txt
set complete-=k complete+=k
And grab http://lerdorf.com/funclist.txt and copy it to /usr/share
Then hit Ctrl-N or Ctrl-P as you are typing a function name. Read up on vim's dictionary completion feature to learn more.
VIM is really a great editor! Note that you can use <cr> instead of ^M in your vimrc, this makes it easier to read or view in other applications.
Here are some more mappings for automatically closing quotes and parentheses:
inoremap { {<CR>}<C-O>O
inoremap [ []<LEFT>
inoremap ( ()<LEFT>
inoremap " ""<LEFT>
inoremap ' ''<LEFT>
Hope you find this useful.
Rasmus, thanks a lot for your addition! Sounds really pretty useful!
Link to comment
Thanks! I always enjoyed that feature in other editors and wondered how to get that into VIM. :)
Beside that, I cannot imagine, which character you meant instead of ^M. Maybe you could write me an email about that?
Hi Rasmus,
That function completion stuff is a gem :) I'm eventually getting my vim to behave better seeing that we have a vim zealot here at work and various tips I've started picking up from various sources.
I've been playing with my .vimrc today (and thanks Toby for the starting point), and changed these to:
inoremap { {}<Left>
...
Which will place the cursor between them instead of having to move it manually. I'm lazy.
I also discovered Cream [http://cream.sf.net], which is like Vim without relying so much on editing modes, which are easily the thing that annoy me most. It also supports defining code templates to be expanded be hitting <Shift-Space>x2.
My .cream-user file is at http://www.jellybob.co.uk/~jon/cream.rc for the curious.
Thanks, Jon! Please see Jörns entry for a better solution especially with {}. Jörns one does autoindent correctly.
(Jon's entry was posted when the above so crappy shown > and < signs were not present due to some wired stuff in this blog-software.)
" Autocompletion using the TAB key (forward)
" and SHIFT-TAB (backward)
function InsertTabWrapper(direction)
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
elseif "backward" == a:direction
return "\<c-p>"
else
return "\<c-n>"
endif
endfunction
inoremap <tab> <c-r>=InsertTabWrapper("forward")<CR>
inoremap <s-tab> <c-r>=InsertTabWrapper("backward")<CR>
You might like to have a look at my PHP5 template VIM filetype plugin, at
http://www.vim.org/scripts/script.php?script_id=1307
Well I do not like general abbreviations in my .vimrc.
So I done all that stuff in a special file called "php_abb.vim" located in my ~/.vim/ftplugin folder.
It is sourced when editing php-files. So I also put:
au! BufNewFile *.php :r ~/.vim/skeleton.php | :source ~/.vim/ftplugin/php_abb.vim
au! BufNewFile *.phps :r ~/.vim/skeleton.php | :source ~/.vim/ftplugin/php_abb.vim
au! BufNewFile *.php4 :r ~/.vim/skeleton.php | :source ~/.vim/ftplugin/php_abb.vim
au! BufNewFile *.php5 :r ~/.vim/skeleton.php | :source ~/.vim/ftplugin/php_abb.vim
au! BufNewFile *.php4
into the "augroup filedetect"-section of ~/.vim/filetype.vim and
augroup phpstuff
autocmd FileReadPost *.php :source ~/.vim/ftplugin/php_abb.vim
autocmd FileReadPost *.php4 :source ~/.vim/ftplugin/php_abb.vim
autocmd FileReadPost *.php5 :source ~/.vim/ftplugin/php_abb.vim
autocmd FileReadPost *.phps :source ~/.vim/ftplugin/php_abb.vim
autocmd FileReadPost *.inc :source ~/.vim/ftplugin/php_abb.vim
augroup END
into the same file.
The abbreviations are just set while working on php then. I am doing a lot of other stuff with vim so I don't get bothered of these settings while writing mails or other non-php related things.
Btw: Thank you for your nice work here!
Yours,
wolle
Hi!
Please consider my more up2date blogentries anbout VIM (nr 4 is the most actual in the series):
http://schlitt.info/applications/blog/index.php?serendipity%5Baction%5D=search&serendipity%5BsearchTerm%5D=vimrc
:)
Cheers!
Toby
Dead links on some of your a tags.
Link to commentHello i also wrote a introduction to vim. More like a small cheat sheet. That incluse some tricks and covers other extension like DBext let you build queries (say you forgot the table name, field name ...) It has auto completation.
Link to commentFields with bold names are mandatory.
Comfortable PHP editing with VIM -2-
I updated my .vimrc regarding the comments posted for my first entry about "Comfortable PHP editing with VIM". You can see and download it here.
What I changed (in short):
Added remappings for automatic completion of brackets and quotatio
Comfortable PHP editing with VIM (from Tobias Schlitt)
Tobias Schlitt has put together some really useful information how to enhance vim to you can get more productive with vim. Don't miss his articles:
Comfortable PHP editing with VIM
Comfortable PHP editing with VIM -2-
Comfortable PHP editing wi...
Comfortable PHP editing with VIM -5-
More than a half year after my last "Comfortable PHP editing with VIM" post, I take up this series again, although I decided to let it die in January. Sadly I did not find any time by now, to extend PDV (the PHP Documentor for VIM plugin) furthe
3 years of blogging
Yes, it's true, exactly 3 years ago I wrote my first blog entry. By that time I would never have imagined, that I would keep blogging for more than 3 years and that more or less constantly. My weblog now contains 458 entries. Surely, there is some bulshit
PHP Documentor for VIM documented
Because I had a talk about how to use VIM in respect to PHP source code editing, I took the time during the week, to write some extensive documentation for PDV, the PHP Documentor plugin for VIM. Additionally, I documented my VIM file type plugin, which i