Posts tagged 'plugins'

Current Vim setup for PHP development

published on February 10, 2017.

I made some changes to my Vim setup for PHP development recently, so it’s time to write it all down. I’m more than sure that I’ll break it soon and won’t be able to remember all the things I did to have the current setup.

Some new plugins popped up on my radar, I tweaked some older plugins and I even wrote one for PHPStan!

Last year I wrote how I got really good tag support in Vim, so I’ll first expand on that.

Gutentags

Gutentags is probably the plugin that had the biggest impact on my workflow. It made possible many other functionalities and plugins to just work.

First thing I have configured for it is the location for the tag files:

" Where to store tag files
let g:gutentags_cache_dir = '~/.vim/gutentags'

The second is a more “aggressive” excluding of files from generating tags:

let g:gutentags_exclude = ['*.css', '*.html', '*.js', '*.json', '*.xml',
                            \ '*.phar', '*.ini', '*.rst', '*.md',
                            \ '*vendor/*/test*', '*vendor/*/Test*',
                            \ '*vendor/*/fixture*', '*vendor/*/Fixture*',
                            \ '*var/cache*', '*var/log*']

Takes out a lot of rubbish.

I also have the following in the global ~/.ctags configuration file (gutentags uses ctags under the hood to generate the tags):

--PHP-kinds=+cfit-va

This way I get tags for classes, interfaces, functions, namespaces and traits, while variables and aliases are ignored to remove the noise level.

Jump to definition

I paired CtrlP’s CtrlPTag method with gutentags tag files to get jump to definition functionality:

map <silent> <leader>jd :CtrlPTag<cr><C-\>w

My complete CtrlP settings are here.

Current PHP class and method

A combination of three Vim plugins and a PHP phar file gives me the possibility to show in the status bar the current PHP class and method.

Lightline provides the status bar, tagbar to get the current tag, tagbar-phpctags to generate the tags for the current file using phpctags.

Use the following setting to tell tagbar about the phpctags binary:

let g:tagbar_phpctags_bin='~/.vim/phpctags'

This is the tagbar line for lightline:

'tagbar': '%{tagbar#currenttag("[%s]", "", "f")}',

See here how it all fits together.

Now that I wrote it all down, I should take a look if I could use the gutentags tag files for this. Seems like an awful lot of moving parts for a relatively small feature.

PHP namespaces

The vim-php-namespace plugin provides support for inserting use statements in PHP code. It will use the tag files generated by gutentags, there’s no need to set up anything for that.

I have the following mapping to insert the use statements:

function! IPhpInsertUse()
    call PhpInsertUse()
    call feedkeys('a',  'n')
endfunction
autocmd FileType php inoremap <Leader>pnu <Esc>:call IPhpInsertUse()<CR>
autocmd FileType php noremap <Leader>pnu :call PhpInsertUse()<CR>

and this one for expanding classes to get their fully qualified names:

function! IPhpExpandClass()
    call PhpExpandClass()
    call feedkeys('a', 'n')
endfunction
autocmd FileType php inoremap <Leader>pne <Esc>:call IPhpExpandClass()<CR>
autocmd FileType php noremap <Leader>pne :call PhpExpandClass()<CR>

I also let it to automatically sort the namespaces after inserting:

let g:php_namespace_sort_after_insert=1

Linting

ALE is an Asynchronous Lint Engine and it provides linting for Vim 8. It can do linting for a bunch of languages.

My configuration for it is:

let g:ale_linters = {
\   'php': ['php'],
\}
let g:ale_lint_on_save = 1
let g:ale_lint_on_text_changed = 0

Lints PHP files on save, in the background. A must have!

A promising completion engine for PHP

php.cd finally provides useful completion for PHP. It’s still rough around the edges, misses a feature or two, but I find it a lot better than any other completion engine I used before. No need to configure anything for it, just follow their installation instructions and that’s it. ^X^O all the things!

PHPStan in Vim

PHPStan is a static analysis tool for PHP and I wrote a small plugin for it, vim-phpstan. It calls phpstan from Vim and populates Vim’s quickfix list with the errors.

For now, the only possible configuration is to set the analyse level:

let g:phpstan_analyse_level = 2

Debugging

Sadly, there is no good PHP debugging client for Vim. Or none that I know of. There are a couple of them out there, but they seem long abandoned. I work on a standalone PHP debugging client, pugdebug, but it has it’s own set of problems as well (packaging on Linux is a nightmare).

Supporting plugins

Other “supporting” plugins are 2072/PHP-Indenting-for-VIm, 2072/vim-syntax-for-PHP, sirver/ultisnips, plugins from the sniphpets organization, ddrscott/vim-side-search, robertbasic/vim-argument-swapper.

I’m pretty happy with the current setup. Do you know maybe of any interesting plugin I’m missing? Let me know!

Happy hackin’!

Vimprovements

published on March 26, 2015.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

One part where I always felt (and still feel) that I could improve on my Vim usage, is when moving around. I don’t use the mouse, I don’t use the arrow keys, but for a while now I felt that spamming hjkl to get from one place to another is not really efficient. To be honest, it is kind of easy to just press down jjjjjjj … to move down lines. Went a bit too far? Just kk back. But there has to be a better way.

First off, I installed vim-hardtime to break the habit of spamming hjkl. That plugin limits the number of times one can press hjkl in a set time frame. I have it set up to block me from moving for a second after pressing the same motion twice in a row. jj is OK, but if I want to do jjj, well…

And this gets annoying. I want to edit code, not sit around and wait!

Faster moving around

I know about relative line numbers. I tried using them. And I guess my brain is just not wired in such a way that I could find relative numbers easy to use.

The second plugin I installed is vim-easymotion. It is supposed to make moving around in Vim much easier. There’s a lot of stuff going on in there, it has a lot of features. For now, I use 2 bits from it - jump to anywhere with s and k and j to move in lines up and down. I especially like these in visual mode, makes selecting text real nice.

I started using : more to jump to specific lines, f and F to search on the current line, { and } to move in paragraphs. I’m also more comfortable with general search with / and ?.

One other pair of commands that help me reduce spamming jk is u and d to scroll the window up and down.

Faster working with files

The third plugin I installed is CtrlP. I stumbled upon it in a /r/vim thread and decided to try it out. Up until then I was using NERDTree exclusively to navigate around files and I can’t really remember when was the last time I hit n to open it since I have CtrlP. It’s just amazing to work with files now.

NERDTree still has it’s use cases, for example in a project where I don’t really know what files are in there, but for projects on which I work frequently… Oh boy! Wonderful stuff.

I left the default p for searching for files, but remapped <tab> to open CtrlP’s find buffer mode.

Misc

Other stuff I did to make working in Vim more comfortable…

I set leader to be . I had it as , for a long while, but realised that it’s to close on the keyboard to the . and I kept repeating the last command when I actually wanted to do some magical incantation.

I learned about :m[ove] to move and :co[py] to copy lines. Good stuff.

dap and cap pairs are also useful from time to time. They just simply delete, or change, around the current paragraph.

Finally, I added the following bit to my .vimrc to highlight the current line:

augroup CursorLine
    au!
    au VimEnter,WinEnter,BufWinEnter * setlocal cursorline
    au WinLeave * setlocal nocursorline
augroup End

So, that’s pretty much it. I don’t think that I’m super fast navigating around in vim, but it’s definitely better than some 3 months ago.

Next thing I want to tackle is using registers, and especially macros.

And read more of that nice :help holy-grail.

Ack in vim

published on December 09, 2014.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

I started using vim 3, 4 years ago. The way I use it is that I started out with no plugins and with a handful of lines in .vimrc. It is far too easy to cram all kind of stuff into it and then get lost in the myriads of key combinations. To prevent that, I decided to slowly add in bits and pieces I find lacking in my day to day usage of vim. Also allows me to first learn the editor and later the plugins.

Today was an exceptional day as I added not one, but two plugins to vim! And that is a big change for me as the total number of plugins I now use is 4.

The first one I added is ack.vim. It’s a nice little plugin to run ack from within vim and show the results in a split window. It’s rather easy to use, one just basically types :Ack search_string and that’s it. The one thing I immediately disliked is that it sort of gets lost in the subdirectories of a project.

A quick google search found a solution for that little problem as well: vim-rooter. It doesn’t do much, just changes the current working directory of vim to the project root when you open a file.

And that’s basically it. Nice and fast searching with ack from vim.

Tags: ack, vim, plugins, rooter.
Categories: Development, Software, Programming.
Robert Basic

Robert Basic

Software engineer, consultant, open source contributor.

Let's work together!

If you require outsourcing or consulting help on your projects, I'm available!

Robert Basic © 2008 — 2019
Get the feed