Katrina
Katrina

Reputation: 1925

Sublime Text autocomplete function with variable in parentheses?

If I already have this written

$company_id = isset($_POST['cid']) ? $_POST['cid'] : null;

And I want to wrap a function call around $_POST['cid'], is there a way to put that inside the autocompleted function's parentheses?

Instead of me typing this:

$company_id = isset($_POST['cid']) ? Validate::isId()$_POST['cid'] : null;

and then having to erase the right parenthesis, is there a shortcut to wrap the param when sublime autocompletes the function for me?

$company_id = isset($_POST['cid']) ? Validate::isId($_POST['cid']) : null;

Using Mac Yosemite and SublimeText 3.

Upvotes: 0

Views: 1013

Answers (1)

Enteleform
Enteleform

Reputation: 3823

You can create a custom snippet that accepts a SELECTION argument:

SnippetDemo


Save The Code Below @:

Packages/___Your_Snippet_Folder___/SnippetName.sublime-snippet

 

<snippet>

<tabTrigger>testFunction()</tabTrigger>
<description>testFunction</description>

<scope>source.php</scope>

<content>
testFunction(${1:$SELECTION}, ${2:PlaceHolder_2})
</content>

</snippet>

The use of placeholders, for example: ${1:placeholder_text_goes_here} , allows you to assign descriptive pre-selected regions throughout your snippet that can be navigated with Tab & Shift + Tab


Additionally, you can replace one of the placeholders with $SELECTION, for example: ${1:$SELECTION} , which will pass the currently selected text as an argument if you execute the snippet from the command palette or a key-binding.


To insert the snippet:

  • select the text to be passed as the $SELECTION argument
  • use Ctrl + Shift + P to launch the command palette
  • type Snippet: followed by the value of the <description> key in your sublime-snippet file

For more information on snippets, see:

SublimeText / Unofficial Docs / Snippets

Upvotes: 1

Related Questions