I'm currently using Vim as a lightweight IDE. I have NERDTree, bufexplorer, supertab, and ctags plugins which do almost everything I want. Only big thing missing for me is auto code formatting.
I'm working with some messy PHP code which has inconsistent indenting and code formatting, ideally I could highlight the code I want formatted (whole files would be fine too) and run a command to tidy it.
Does anybody have a nice solution for this working in Vim?
Quick way to fix PHP indentation in vim is to visually select the lines you want to work with using shift-v, and then press equals (=) to trigger auto-formatting.
As for other formatting issues you're probably looking at employing some regex search and replaces, such as :%s/^M/\r/g (that's ctrl-V ctrl-m, not caret-M) to fix line endings
Enter normal mode in vim and then type
1GVG=
Format in PSR-2 style
For the new standard Coding Style Guide PSR-2 use the PHP-CS-Fixer.
There is a Vim plugin: Vim-php-cs-fixer
How to install:
Install PHP-CS-Fixer (globally with Composer):
composer global require friendsofphp/php-cs-fixer
Then add the Vim plugin (Pathogen):
cd ~/.vim/bundle
git clone git#github.com:stephpy/vim-php-cs-fixer.git
Restart Vim.
Default mapping:
<leader>pcd " For directory
<leader>pcf " For flie
There is a vim plugin that enables formatting on your code from within vim. It's called vim-autoformat and you can read about it and download it here:
https://github.com/vim-autoformat/vim-autoformat
It integrates external code-formatting programs into vim. When this plugin is installed, you only have to install an external code formatter to get everything to work out of the box. It supports the php formatter phpCB, which is the best php formatter i've seen so far.
UPDATE: phpCB is not supported anymore, due to code breaking behaviour. However, vim's indentfile is always used as fallback, allowing you to at least indent your code when there's is no formatter available.
The vim website is not the easiest to navigate, but there is a wealth of chewy nougat center there.
For instance I found this php indenting script there. Give it a try.
Related
Is there any way to minify (or remove comments) in PHP using Visual Studio 2015? I use PHP Tools and when i release php scripts to public server, i would like at least remove all comments from code. I know, is there way to remove comments with content menu, but i wish to have comments in my project and no comments (or minimal and unread code) in publish scripts (some like Bundler & Minifier tool, but for php). Thanks
There is basically no point in minifying Php since there is no performance gain in doing so. Although if you insist, there are a few ways to remove comments/whitespaces from source. (and these are not limited to just VisualStudio) -
Use Gulp.
Use Command line options: php -w file.php => generates file without comments & whitespaces. Equivalent to php_strip_whitespace()
Use Regular Expression in Find-Replace Function of your IDE. You can use the following inside Find FieldBox. (and keep Replace fieldbox empty)
//.* or /.
Use a Library/tool like Php-Minify
Hope that helps!
Why aren't you going to use online tool to do it?
There are many online tools you can use instead of VS 2015.
Here is one tool for you.
http://beta.phpformatter.com/
Hope it helps you and check this answer as solved if it helps you.
Thanks!
You can check Comment Remover tool to remove all the comments from a file with a single button click. It also remove #regions and preserve XML Doc comments.
I think there is no tool for visual studio to do this. But you may use external tools to do the job. It's a common task to do this in build system like jenkins. The build system e. g. is able to react on many version control events.
But I also think that you want to make php code unreadable (like compiling). There are only a few options to hide your php code to other people. You could use ionCube for encrypting php files. But then you have to make sure, that the ionCube extension is also installed on the public web server. Also, ionCube is currently not available for php 7.2, only up to 7.1. Another option is to compile php code using HipHop, a PHP to C++ compiler engine: https://de.wikipedia.org/wiki/HipHop
In Sublime Text 2, I am trying to type the code and have auto format command to rearrange and auto indent source code. If it is not possible to have a automatic formatter, would be nice to have a shortcut command to do it.
I am searching for something else besides the built-in "Reindent" option and the hard-to-make-work "PHP Beautifier" plugin.
I would like to know if someone found any nice auto formatter for PHP: any formatter that won't mess our PHP code. JSFormat seems to be not useful.
Is there any easy to "install and use" plugin?
PHPTidy is definitely good choice for PHP code formatting in SublimeText2.
You need to install package control package to enable you to install packaged in sublime text: link.
ctrl+shift+p -> type -> Package Control: Install Package
search for phpTidy -> hit enter
that is it - now you can format your code in WordPress coding convention.
How to format you code:
ctrl+shift+p -> type -> tidy -> Hit Enter
ctrl+alt+t
Hope this help.
There is a PHP code formatter named PhpBeautifier. Here you can find a Sublime Text plugin for this program and you can also install it via package control.
I have this set up as a keybind:
[
{
"keys": ["alt+shift+f"], "command": "reindent",
"args": {"single_line": false}
}
]
If you're looking for a bit more than that there is Tidy PHP under package management.
Update: Been some time, I'm now using PHP Coding Standards along with PHP CS Fixer which automates a lot of the cleanup done by autoformatting. This takes a fair bit of work / understanding of php cs to setup but its not too hard, and well worth it. Link
I've been searching this before also, but with no avail. Sublime text 2 probably doesn't support formatting PHP code natively. I have looked into ST2 packages - http://wbond.net/sublime_packages/community, but there is also nothing. Probably you'll have to do this in another editor.
Of course it would be nice, if someone would write a package for this, 'cause packages for formatting JS, JSON, HTML.. already exist.
This is an old subject, but as I found it via Google, here's an answer :
You can use the phpfmt plugin for Sublime Text 3, and configure it to respect code conventions (Such as PSR-0, PSR-1 or PSR-2).
The files will be beautified on save.
I'm using sublimetext-codeformatter
It supports PHP, JavaScript/JSON, HTML, CSS, SCSS, Python, Visual Basic/VBScript and maybe more in the feature. For PHP formatting it uses a fork of the phpfmt which works well. All languages-specific formatting can be set in one config file.
Not sure if this is what you are looking for, but are you aware that when you save your file as .php, Sublime Text 2 automatically highlights and indents according to its built in PHP rules. You can also select this from going to View, and then Syntax, and selecting PHP. But again, this is automatically done when you save the file as .php.
I'm looking for a indent application to fix all aspects of PHP code formatting - convert spaces, perform indentation in PHP and HTML code, leave HEREDOC alone, etc.
Please recommend a decent indenter for PHP.
find . -name '*.php' -printf "echo -e \"G=gg\n:wq\n\" | vim %p\n" | sh
I have found few things. First, the best way to mass-edit files is to use bash scripting and vim.
Second, vim indentation is not perfect and is not working always. So I had to rather do "=G" manually then review the source.
Finally, vim's command "retab" in collaboration with tab settings is great if only looking to change white-spaces into tabs.
I'd like to also point towards an awesome podcasts where I learned more about Vim:
http://vimcasts.org/episodes/show-invisibles/
http://vimcasts.org/episodes/tabs-and-spaces/
http://vimcasts.org/episodes/whitespace-preferences-and-filetypes/
http://vimcasts.org/episodes/tidying-whitespace/
Squizlabs has develop PHP_CodeSniffer, which tokenizes PHP, JavaScript and CSS files to detect and fix violations of a defined set of coding standards. It have phpcbf cli, that ensures your code remains clean and consistent. By their definition it should satisfied your need. Consider read their wiki, for further step.
2018 answer
Since originally finding solution, I've moved on to use https://styleci.io/. I am not affiliated with them, but I really like the simplicity and it keeps the project nice and tidy. Doesn't cost anything for my open-source project either.
I'd recomment PHPStorm, its not a command-line per se, but it does allow to specify custom code formatting and apply it to multiple files
I checked "Settings > Prefences > Backup/Auto-Completion > Enable Auto-completion on each input" and "Function parameters hint on input" options. I have Notepad++ 5.9 (unicode) version. It auto completes function names but not their arguments. Also i tried re-install. I know it has this feature but not working. Any idea why?
I mean it auto completes print_r when i write "pri" but i want it to work like in the image;
http://i51.tinypic.com/3322auc.jpg
Hmm, this is not an answer to your question but I want to share it anyway.
I have used Notepad++ for years. I didn't like Eclipse and NetBeans is just so damn slow.
I never used the auto complete function for anything.
Notepad++ can't really do any code analysis, it can only give you auto-completion on the known PHP functions.
I've since switched to an IDE that does full code analysis and can do auto-completion for your own code as well.
Notepad++ is great if you just want to write something real quick. It's also a great tool if you want to learn about PHP since it doesn't do auto-completion very well ;) but if you really want to be productive when you're working on a large application I recommend you use an IDE that does code analysis. Productivity is the key here. I spend less time looking through files and more time actually writing code, which is great! :D
Good IDEs for PHP: NetBeans, PHPStorm, Eclipse.
For this feature to work, you need to supply an .xml file which contains the appropriate functions and parameters.
An example for Javascript with installation instructions can be found here.
I have not yet found xml files for PHP (or Python, which I'm looking for). But it seems this can work in npp, you just need to locate the right xml file.
Best of luck!
(Oh, and don't listen to the naysayers who are too quick to give up, npp works fine as an IDE.)
Notepad++ it's very good text editor, but it's not IDE.
Try to use IDE - NetBeans or PhpStorm, and you will get much more than just smart autocomplete.
I have a same question about this, function hint is Ok,but param hints not display until I download a new release of notepad++(a not install release), all is ok. before, I tryed many time, and edit the xml files.
If you will edit you own language, Can copy from a exist language xml ,for cpp.xml in plugins/APIs folder. It's easy to understand its rule.
It is very easy to have a error, if you config file is changed.
How to get code completion to work for PHP in Netbeans 6.9.1?
I want Netbeans to suggest native PHP functions.
EDIT:
The auto complete only works for reserved vars and reserved keywords but not for native functions. Looking at the example above, it should suggest e.g str_replace, strlen, etc...that doesnt happen even after CTRL + SPC.
Just make sure you have enabled the PHP plugin, that should do the trick. BTW autocomplete might not work while NetBeans is checking for project changes though...
This happened to me from time to time when I used NetBeans. I used the CTRL+SPACE shortcut to invoke the code completion window.
UPDATE:
If you're interested, you can also use VIM, which has a code completion feature for php as well.
Assuming you have VIM 7 (Full version) installed, add the following to your .vimrc file:
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
To use the code completion window, simply use CTRL+X CTRL+O
Install NetBeans with PHP support. OR if you already have netbeans, you need to install PHP plugin by going to Plugins Manager.
If you want code completion for external libraries like Zend, add them in the Gobal Include Path (available in project properties)
Apparently deleting the contents of .netbeans/var/cache/index in your user directory (application data on windows etc) fixed it.