
Comfortable PHP Editing With VIM -9-
After telling you about my completely reworked VIM setup and the awesome VIM plugins I'm using in it, this blog post deals to share with some snippets and settings I'm using to make all that stuff work round.
Leader Key
VIM already provides you with a ton of pre-defined shortcuts on various keys and in various combinations with <shift>
and <ctrl>
. Therefore, the so called Leader Key was introduced, allowing you to choose one base key to deal as the starting point for all your custom shortcuts. Defining the leader is easy using:
let mapleader = ","
let maplocalleader = ","
You can then access this key through <leader>
and use it in keyboard combinations. Many plugins also try to register sensible defaults here, without inteferring with others.
Incrememntal Search
Using the incremental search setting, VIM starts running through your text document and showing you search results, while you still type your search. This helps a) to find stuff earlier and b) return early, if the desired result cannot be found. Incsearch is set by:
set incsearch
set nohlsearch
The latter option disables the annoying highlighting of search results after you finished searching.
Cursor Line
The place you are currently editing in insert mode is typically only marked by the cursor. Using the cursor line, you get a more appealing visual identifier:
autocmd InsertLeave * set nocursorline
autocmd InsertEnter * set cursorline
The autocommands deal for enabling/disabling the line when you enter/leave insert mode.
Restore Last Position
One feature which I had ages ago already (I suspect by either some plugin or by a distro based setting), but which suddenly vanished, was to restore the cursor position in a file I recently edited, when I re-open it again. Thanks to Benjamin, I now have that again through:
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
Invisible Chars
Making invisible chars (e.g. tabs) visible can sometimes help while debugging. This little snippet allows you to switch that on using <leader><L>
:
nnoremap <leader>L :set list!<CR>
set listchars=tab:▸\ ,eol:¬
Thanks to a VIM cast for that.
Save as Root
How often do you forget a sudo
in front of your vim
command when editing a system config file? Using the following snippet allows you to write your changes anyway, without exiting your VIM session:
cnoremap w!! w !sudo tee % >/dev/null
Thanks to Jakob for this snippet.
Moving through CamelCase
If you write CamelCase code, its often useful to navigate through the dedicated parts using <ctrl><left>
and <ctrl><right>
:
nnoremap <silent><C-Left> :<C-u>cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
nnoremap <silent><C-Right> :<C-u>cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>
inoremap <silent><C-Left> <C-o>:cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%^','bW')<CR>
inoremap <silent><C-Right> <C-o>:cal search('\<\<Bar>\U\@<=\u\<Bar>\u\ze\%(\U\&\>\@!\)\<Bar>\%$','W')<CR>
Paste Toggle
Using :setpaste
you can switch off all automatic formatting (e.g. indentation) in VIM. This is useful, if you want to paste from external sources through your terminal. However, using the following config, it becomes even more convenient:
set pastetoggle=<ins>
nnoremap <silent> <ins> :setlocal paste!<CR>i
autocmd InsertLeave <buffer> se nopaste
The <ins>
(insert) key becomes the paste toggle, meaning that, in insert mode, it switches between paste
/nopaste
. In normal mode, pressing insert will set you into insert mode and set paste
. Furthermore, when you leave insert mode with paste
it will be switched off, so that you don't forget it.
Endless Undo
In times of distributed version control, undo functionality might become less and less important. However, it still gives you some comfort, especially if VIM remembers your undo history between sessions:
set undodir=~/.vim/undodir
set undofile
set undolevels=1000
set undoreload=10000
This sets vim to store undo information in files below ~/.vim/undodir
. It will keep up to 1000 (yes, feels almost endless) actions and up to 10000 lines of buffers that get overwritten externally.
Edit Files Relative
Another goodie found in Benjamins config: When editing a file, it occurs quite often to me, that I need to edit one in the same directory (or one level above). I tent to write stuff like :vs %:h
which is
vertical split (
:vs
)current file name (
%
)strip last path element (
:h
)
This works, but is a) hard to type and b) does not give you auto-completion for subsequent path elements (the files). The following shortcut simply expands the desired base path from typing %%
:
cnoremap %% <C-R>=expand('%:h').'/'<cr>
I would love to have %%%
expand to %:h:h
(one more path level removed), but we did not find a solution for that, yet. Anyone maybe has an idea?
Comments