在 Vim 中格式化 PHP 代码

发布于 2024-07-19 12:31:46 字数 228 浏览 5 评论 0原文

我目前使用 Vim 作为轻量级 IDE。 我有 NERDTree、bufexplorer、supertab 和 ctags 插件,它们几乎可以完成我想要的一切。 对我来说唯一缺少的是自动代码格式化。

我正在处理一些混乱的 PHP 代码,这些代码的缩进和代码格式不一致,理想情况下我可以突出显示我想要格式化的代码(整个文件也可以)并运行命令来整理它。

有人有一个在 Vim 中工作的好解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

_畞蕅 2024-07-26 12:31:46

在 vim 中修复 PHP 缩进的快速方法是使用 shift-v 直观地选择要使用的行,然后按等于 (=) 触发自动格式化。

至于其他格式问题,您可能正在考虑使用一些正则表达式搜索和替换,例如 :%s/^M/\r/g (即 ctrl-< kbd>V ctrl-m,而不是 插入符-M)来修复行结尾

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

猫瑾少女 2024-07-26 12:31:46

在vim中进入正常模式然后输入

1GVG=

Enter normal mode in vim and then type

1GVG=
嘿咻 2024-07-26 12:31:46

PSR-2 风格的格式

对于新标准 编码风格指南 PSR-2使用 PHP-CS-Fixer

有一个 Vim 插件: Vim-php-cs-fixer

如何安装:

安装 PHP-CS-Fixer (全局使用 Composer):

composer global require friendsofphp/php-cs-fixer

然后添加 Vim 插件 (Pathogen):

cd ~/.vim/bundle
git clone [email protected]:stephpy/vim-php-cs-fixer.git

重新启动 Vim。

默认映射:

<leader>pcd " For directory
<leader>pcf " For flie

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 [email protected]:stephpy/vim-php-cs-fixer.git

Restart Vim.

Default mapping:

<leader>pcd " For directory
<leader>pcf " For flie
那请放手 2024-07-26 12:31:46

有一个 vim 插件,可以在 vim 中对代码进行格式化。 它称为 vim-autoformat,您可以在此处阅读并下载它:

https://github。 com/vim-autoformat/vim-autoformat

它将外部代码格式化程序集成到 vim 中。 安装此插件后,您只需安装外部代码格式化程序即可开箱即用。 它支持 php 格式化程序 phpCB,这是迄今为止我见过的最好的 php 格式化程序。

更新: 由于代码破坏行为,不再支持 phpCB。 但是,vim 的缩进文件始终用作后备,允许您在没有可用的格式化程序时至少缩进代码。

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.

不即不离 2024-07-26 12:31:46

vim 网站 不是最容易浏览的,但那里有丰富的耐嚼牛轧糖中心。

例如,我在那里找到了 这个 php 缩进脚本。 试一试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文