(重新)在Vim中格式化PHP代码 [英] (Re)formatting PHP code in Vim

查看:126
本文介绍了(重新)在Vim中格式化PHP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何格式化如下所示的PHP代码

How do I format PHP code that looks like this

class SomeClass
{
    function insideclass()
    {


    }
} 

在没有任何外部工具的情况下使用Vim吗?

into this, using Vim without any external tool?

class SomeClass {
    function insideclass() {


    }
} 

我并不是说这是格式化代码的最佳方法,但这就是我们团队所遵循的.

I am not arguing that this is the best way to format the code, but this is what we are following in the team.

编辑:删除了对代码段插件的引用,这引起了对该问题的困惑.

Edit: Removed reference to the snippet addon, which caused some confusion about the question.

推荐答案

您有两个不相关的问题,您将不会找到针对这两个问题的唯一解决方案.

You have two unrelated problems and you won't find a unique solution to both.

  1. 您不喜欢默认的class代码段.

  1. You don't like the default class snippet.

没问题.如果尚不存在,请创建~/.vim/snippets/php.snippets并通过遵循其他答案对其进行自定义,以使其符合团队的规则. Snipmate不够聪明,无法适应您的编码风格.

No problem. If it's not already there, create ~/.vim/snippets/php.snippets and customize it to fit your team's rules by following the other answers. Snipmate is not smart enough to adapt itself to your coding style.

您的某些现有代码不符合您的编码规则.

Snipmate完全没有帮助,因为它仅处理插入,而不处理转换.您将需要一些外部美化器或一些宏.

Snipmate won't help at all since it only deals with insertion, not transformation. You are going to need either some external beautifier or a few macros.

编辑

EDIT

这是一个非常简单的命令,可以完全按照您的示例执行您想要的.我不保证它会在所有地方都适用.将此作为一个简单的起点.

Here is a very simple command that does exactly what you want on your example. I offer no guarantee that it's going to work for everything everywhere. Take it as a naive starting point.

:g/^\s*{\s*$/normal kJ

  • :g/pattern作用于包含pattern的所有行,请参见:h :global.

    • :g/pattern acts on all lines containing pattern, see :h :global.

      ^\s*{\s*$匹配所有单个{,无论它们与行首之间的空白量如何.

      ^\s*{\s*$ matches all single { whatever the amount of whitespace between them and the beginning of the line.

      normal执行normal命令,请参见:h :normal.

      kJ上移一行,并且J用匹配的行显示该行.

      kJ goes up one line and Joins this line with the matched line.

      完成.

      ENDEDIT

      ENDEDIT

      这篇关于(重新)在Vim中格式化PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆