Dev tip: Learn VIM

So happy that I know how to use VIM. Why? Because sometimes I need to edit files on the remote host.
Maybe your IDE can’t successfully connect to the remote host and you end up with SSH and CLI commands.
My current scenario is implementing a payment gateway and using localhost as host is ignored 😦 I ended up coding on the shared hosting via SSH, but without slowdown thanks to VIM skills.

ss_2017-09-16-10-47-17

Improving PHP autocompletion in VIM

After watching VIM as PHP IDE I revisited my config, specifically pimped ctags according to articles Vim Autocompletion for PHP and Exuberant ctags with PHP in Vim

So my current $HOME/.ctags is:

--recurse
--languages=php
--langmap=php:.inc.php
--fields=+aimS
--PHP-kinds=+cfd
--exclude=.svn
--exclude=.git
--exclude=cache
--exclude=tags
--exclude=min
--exclude=\*.min.\*
--exclude=node_modules
--regex-PHP=/abstract class ([^ ]*)/\1/c/
--regex-PHP=/interface ([^ ]*)/\1/c/
--regex-PHP=/trait ([^ ]*)/\1/c/
--regex-PHP=/(public |static |abstract |protected |private )+ function +([^ \(]*)/\2/f/

Faster CtrlP fuzzy search with custom ignore

Had some issues with listing files from modules/ via CtrlP so I ended up writing my own user command for.

let g:ctrlp_user_command = 'find %s -type f -not \( -path "*/.svn/*" -o -path "*/.git/*" -o -path "*/vendor/*" -o -path "*/.idea/*" -o -path "*/cache/*" \)'

Thanks CtrlP for the ctrlp_user_command var 😉

 

Reuse command or search from history in VIM

I just came across http://vim.wikia.com/wiki/Using_command-line_history from a tweet.

In Normal mode press q: and the command (:) history will appear.

VIM command history
VIM command history

Movement with j or k moves up and down and hitting Enter reexecutes the command where the cursor stands.
The same works with the search (/) history q/.

Improving VIM skills

I decided to study VIM more to master it better. I just started watching https://www.youtube.com/watch?v=SkdrYWhh-8s and Ben made a great point already in the first 6mins.

Map saving to Ctrl+s

# ~/.vimrc
nnoremap <c-s> :w<CR>
inoremap <c-s> <Esc>:w<CR>
vnoremap <c-s> <Esc>:w<CR>

I switched to Z shell so:

# ~/.zshrc
vim() STTY=-ixon command vim "$@"
alias vi="vim"

More info at
https://stackoverflow.com/questions/3446320/in-vim-how-to-map-save-to-ctrl-s
https://unix.stackexchange.com/questions/59770/saving-with-ctrl-s-in-vim

Unneeded whitespace visual notification in VIM

Just read http://antjanus.com/blog/thoughts-and-opinions/use-vim/ and found the following lines in his .vimrc at https://github.com/AntJanus/dotfiles/blob/master/.vimrc#L200


" Whitespace fixes
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()

" Strips whitespace
nnoremap W :%s/\s\+$//:let @/=''