How to run command on save in Sublime Text 3? - sublimetext3

Sublime Text offers built-in commands, such as paste, new_window, toggle_comment etc. In addition, some plugins offer their own commands that can be used for key binding.
Can I force any of these commands to be ran on file save? The reason I need is because I'd like to run CSScomb on file save instead of / in addition to having key binding for it. The command name is css_comb.

Sublime-hooks package allows you to run packages based on event (on new, on save, etc.), so you can use it to achieve your goal. Just add this code to CSS syntax settings:
"on_pre_save_language": [
{
"command": "css_comb"
}
]
If you are familiar with plugins maybe you can make a plugin that extends EventListener and override on_post_save or on_pre_save methods.

Related

How to change VIM PHP auto formatting options

I have tried googling this extensively, but all I can find are plugins which format code in the author's preferred way. What I would like to do is change the auto format options so that I can setup VIM to use the same formatting as the other editors my team uses.
For example, I would like:
public function test($param)
{
// code here
}
rather than:
public function test($param){
// code here
}
or
public function test($param)
{
// code here
}
Is this possible without a plugin? Are there formatting templates somewhere that I can edit? Thanks :)
Is this possible without a plugin?
Yes.
Are there formatting templates somewhere that I can edit?
Sure. Vim is the most customizable text editor in universe. :)
So, let's start understanding snippets.
Snippets are blocks of text that can be inserted in your text using some pre-defined keys. The idea of snippets is to easily put in your file some chunk of text you use often. Snippets are these "templates" you mentioned.
To use snippets with Vim, you need to install the garbas/vim-snipmate plugin. You probably had it installed, since it seems that you can use them. This plugin search in you .vim folder for .snippets files and open them every time you open a file with predetermined extension. For example, when you create the foo.html file, vim-snipmate plugin searches for the html.snippets file and load it. After that, everytime you type, for example, html and press tab, Vim will write the <html> tag, because in your html.snippets file there's a snippet telling Vim to do so. Every programming language needs its own .snippets file, and loads it at the start. It's common to have a _.snippets file too, that loads with all file extension. It's a global snippet file.
To edit your snippets, you have to find where are your .snippets files. In Linux, open your terminal and type:
cd ~/.vim
find -name *.snippets
And then you'll see where are your snippet files. Assuming they are ~/.vim/snippets, for example, you open your java snippets with a:
vim ~/.vim/snippets/java.snippets
A .snippets file commonly looks like this: java.snippets file
These +-- lines are compressed lines you can expand and contract typing za in normal mode. In the blue line you always see snippet something written. The something is the shortcut you need to type and press tab when you're editing a file to use the snippet. For example in this java.snippets file there is a snippet called snippet po. So, when you're editing a java file, type po and press tab, Vim will inserted protected {}.
Snippets have a simple language, you can understand a lot just by seeing them in the .snippets file and typing them in another one. If you want to understand more about creating snippets, Google about vim snippets, and you'll find lots of stuff about it.
If you find that you don't have snippets in your .vim folder, or have insufficient ones, you can install a lot of excelent scripts with the honza/vim-snippets extension on Github.

Sublime Text Keybinds Context Within Unsaved Changes

I am trying to create a keybind within sublime text 3 that changes its behavior depending if a file has unsaved changes or not.
Sublime already supports a context option within the creation of keybinds such as this:
{"keys":[":","e"],"command":"revert","context":[{"key": "setting.command_mode", "operand": true}]},
However I can't find if there is a way to detect if the file is saved or dirty.
Anyone have some insights on this?
Looking at the unofficial documentation (the official documentation is out-of-date), there seems to be no context that can be used in keybindings to determine if a file is saved or dirty.
I think it would therefore be necessary to create a plugin in Python, with a command which would perform the actions you require based on whether the file has unsaved changes or not. You could then set the keybinding to execute this command regardless of context, as the plugin will contain the necessary logic.
The official documentation mentions that a plugin can determine whether a file is saved or not using the is_dirty() method on the view.
I see from your question that you want to execute the revert command, so I have drawn up a quick and simple Sublime Text plugin / python script that will achieve this:
import sublime, sublime_plugin
class RevertIfUnsavedCommand(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.is_dirty():
self.view.run_command('revert')
else:
print('TODO: do something else here')

Override the default tab autocomplete content in Sublime Text 3

What's the correct way to override the JavaScript "fun" tab completion for example?
I've created a new file in my user package called: my.sublime-completions and I understand that file requires two properties (scope & completions)?
Thanks in advance for the help :)
The documentation on completions and the two See Also links at the top should provide plenty of detail on how to structure your .sublime-completions file for use in JavaScript. For example, the following exactly replicates the default snippet (use \n for newlines and \t for tabs, if needed):
{
"scope": "source.js",
"completions":
[
{ "trigger": "fun", "contents": "function ${1:function_name} (${2:argument}) {\n ${0:// body...}\n}" }
]
}
The easiest way to override the default fun JavaScript snippet is to first install the PackageResourceViewer plugin via Package Control. Once that's set up, restart Sublime and open the Command Palette with ⌘⇧P (OS X) or CtrlShiftP (Windows/Linux). Type prv to bring up the PackageResourceViewer options, and choose PackageResourceViewer: Open Resource. Scroll down the list and select JavaScript, then open the function-(fun).sublime-snippet file, using XML for syntax highlighting if you wish. If you are using a more recent version of Sublime, the file may be located under JavaScript/Snippets.
Next, set the contents of the file to the following:
<snippet>
<content><![CDATA[]]></content>
<tabTrigger>fun</tabTrigger>
<scope>source.js</scope>
<description>Don't Use</description>
</snippet>
Save the file, and you should be all set. Assuming your .sublime-completions file is set up correctly, when you type fun in a JS file you should only see your completion, not the default one.
Good luck!

Prevent Sublime Text 3 from tab completing 'sup' to '<sup>' in a non-HTML file

When I type sup then TAB in Sublime Text 3, I get <sup>. That isn't what I want; I only want tab completions based on what is in my current file.
How can I limit tag style tag-completions to just HTML files?
I was hoping to find a setting called tag_complete_file_extensions or similar.
I don't currently understand the implications of these default preference settings, but they don't look like what I want:
{
// Controls what scopes auto complete will be triggered in
"auto_complete_selector": "source - comment, meta.tag - punctuation.definition.tag.begin",
// Additional situations to trigger auto complete
"auto_complete_triggers": [ {"selector": "text.html", "characters": "<"} ],
}
At the least, I'd appreciate an answer pointing me in the right direction. I don't mind forking a package or writing some custom code.
I believe this behaviour is provided by the Emmet (formerly ZenCoding), or similar package and not sublime directly.
Are you sure syntax is set accordingly for the files you get html tag completion?
By default Emmet detects that you are working on html files and enables tag completion (in my case I did not have to configure anything to get this functionality). You should have a closer look at your packages and your configuration and possibly update existing ones.
If you need any further help with the configuration files, I'm happy to help!
Cheers!

Toggle sets of Vim plugins

Is there a trick or script that allows me to switch between sets of vim-plugins and -settings easily?
I use vim for very different development and writing. It can be really annoying to have certain webdevelopment-specific plugins turn up, when writing a report in LaTeX, for example.
What I'd like to see is something like RVM.
Have a set of "global" plugins and settings; plugins and settings that are always enabled or used.
Per project plugins and settings; pluginss, configurations and settings that will be loaded after activating that "environment".
You may find localvimrc to be useful for point number two. It allows you to have a .lvimrc in your project folder with settings for that specific project. In that file you could load your project-specific plugins by manipulating the runtimepath or by using pathogen/vundle/whatever.
Using this method you would configure your "global" settings and plugins as you would normally.
Nice question IMHO. By the way, using a plugin manager could simplify this kind of stuff too. For example, with pathogen you can do something like:
" To disable a plugin, add it's bundle name to the following list
let g:pathogen_disabled = []
if your_condition
call add(g:pathogen_disabled, 'myplugin')
call add(g:pathogen_disabled, 'myplugin2')
end
See this answer for a good example about conditional loading. It would be very nice to see this feature implemented in pathogen.
I'd just make aliases
alias mvim='vim -u myvimrc'
alias ovim='gvim -U someothervimrc'
Ans yes you could use runtimepath inside the vimrc-s to setup very different configurations

Resources