Sublime Forum

Autocomplete "this" in PHP files

#1

Writing $this-> in PHP files can be a bit tedious, as it requires pressing [Shift]+$ and then [Shift]+>.

It would be great if Sublime Text suggested $this-> in the autocompletion list once we type this . Pressing [Tab] would then replace this with $this-> .

This idea is somehow similar to the function that replaces php with <?php ?> after pressing [Tab]

0 Likes

#2

You can create a snippet to do this.

<snippet>
	<content><![CDATA[\$this->]]></content>
	<tabTrigger>this</tabTrigger>
	<scope>source.php</scope>
</snippet>
2 Likes

#3

Thanks, that is very good to know, I just tested and it works well!

But still, I suggest adding this in standard Sublime Text as PHP is a popular language and classes are full of $this.

0 Likes

#4

feel free to make a Pull Request. Note that there is a this snippet, but it only triggers when in the HTML code, not the PHP code…

0 Likes

#5

For those interested in automatically replacing this with $this-> when typing without pressing Tab, I created a code snippet. It works great and makes life easier when developing a PHP class :slightly_smiling_face:

0 Likes

#6

Alternatively php-this.sublime-snippet

<snippet>
	<content><![CDATA[\$this->]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>this</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.php</scope>
</snippet>
0 Likes

#7

Yes, it works, but it requires pressing [Tab] each time. While over 99% of the time, when I type this in a PHP class, it’s for $this-> . :slightly_smiling_face:

0 Likes

#8

Just to provide my current solution (not exactly the ones I use but the same concept).

    {
        "keys": ["t", "h", "i", "s", "."],
        "command": "insert_snippet",
        "args": { "contents": "\\$this->" },
        "context": [{"key": "selector", "operator": "equal", "operand": "source.php"}],
    },
1 Like