
CodeFormatter is a Sublime Text 2/3 plugin that supports format (beautify) source code.
CodeFormatter has support for the following languages:
No sponsors yet… :(
If you like the software, don’t forget to donate to further development of it!

Installing
With the Package Control plugin: The easiest way to install CodeFormatter is through Package Control, which can be found at this site: http://wbond.net/sublime_packages/package_control
Once you install Package Control, restart Sublime Text and bring up the Command Palette (Command+Shift+P on OS X, Control+Shift+P on Linux/Windows). Select “Package Control: Install Package”, wait while Package Control fetches the latest package list, then select CodeFormatter when the list appears. The advantage of using this method is that Package Control will automatically keep CodeFormatter up to date with the latest version.
Without Git: Download the latest source from GitHub and copy the CodeFormatter folder to your Sublime Text “Packages” directory.
With Git: Clone the repository in your Sublime Text “Packages” directory:
git clone https://github.com/akalongman/sublimetext-codeformatter.git CodeFormatter
The “Packages” directory is located at:
-
OS X:
ST2: ~/Library/Application Support/Sublime Text 2/Packages/
ST3: ~/Library/Application Support/Sublime Text 3/Packages/
-
Linux:
ST2: ~/.config/sublime-text-2/Packages/
ST3: ~/.config/sublime-text-3/Packages/
-
Windows:
ST2: %APPDATA%/Sublime Text 2/Packages/
ST3: %APPDATA%/Sublime Text 3/Packages/
Configuration
To change the default configurations you have to update the CodeFormatter - User Preferences file. You can find this file in the Sublime Text menu bar under: Sublime Text > Package Settings > CodeFormatter > Settings - User.
Make sure that you wrap all the configurations into a single root object.
{
"codeformatter_php_options": {...},
"codeformatter_js_options": {...},
..
}
Following are notes specific to individual formatters that you should be aware of:
PHP
PHP - Used phpF by @subins2000
Getting and installing PHP - http://www.php.net/manual/en/install.general.php
You must install 5.6 or above (https://github.com/subins2000/phpF#requirements)
On Linux/OSx after installation of package, you must set chmod +x to file fmt.phar in folder %PACKAGESDIR%/CodeFormatter/codeformatter/lib/phpbeautifier
You can list all available transformations from Command Palette: CodeFormatter: Show PHP Transformations
Examples of many transformations can be found here: PHP Transformation Examples
Language specific options:
"codeformatter_php_options":
{
"syntaxes": "php",
"php_path": "",
"format_on_save": false,
"psr1": false,
"psr1_naming": false,
"psr2": true,
"indent_with_space": 4,
"enable_auto_align": true,
"visibility_order": true,
"smart_linebreak_after_curly": true,
"passes": [],
"excludes": []
}
Javascript/JSON
Javascript/JSON - used [JSBeautifier] (http://jsbeautifier.org/) by Einar Lielmanis
Language specific options:
"codeformatter_js_options":
{
"syntaxes": "javascript,json",
"format_on_save": false,
"indent_size": 4,
"indent_char": " ",
"indent_with_tabs": false,
"eol": "\n",
"preserve_newlines": false,
"max_preserve_newlines": 10,
"space_in_paren": false,
"space_in_empty_paren": false,
"e4x": false,
"jslint_happy": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 0,
"break_chained_methods": false,
"end_with_newline": false,
"comma_first": false
}
HTML
HTML - used custom python port, please use it with caution, feature in early beta
Language specific options:
"codeformatter_html_options":
{
"syntaxes": "html,asp,xml",
"format_on_save": false,
"indent_size": 4,
"indent_char": " ",
"indent_with_tabs": false,
"exception_on_tag_mismatch": false,
"expand_javascript": false,
"expand_tags": false,
"minimum_attribute_count": 2,
"first_attribute_on_new_line": false,
"reduce_empty_tags": false,
"reduce_whole_word_tags": false,
"custom_singletons": ""
}
CSS
CSS - used [JSBeautifier] (http://jsbeautifier.org/) by Einar Lielmanis and Style Css by Harutyun Amirjanyan
Language specific options:
"codeformatter_css_options":
{
"syntaxes": "css,less",
"format_on_save": false,
"indent_size": 4,
"indent_char": " ",
"indent_with_tabs": false,
"selector_separator_newline": false,
"end_with_newline": false,
"newline_between_rules": false,
"eol": "\n"
}
SCSS
SCSS - Simply modified the CSS formatter module as per the response from scss-lint (Gives way to modify further)
Language specific options:
"codeformatter_scss_options":
{
"syntaxes": "scss",
"indent_size": 2,
"format_on_save": false,
"indent_char": " ",
"indent_with_tabs": false,
"selector_separator_newline": true,
"newline_between_rules": true,
"end_with_newline": true
}
Python
Python - used ruff
Language specific options:
"codeformatter_python_options":
{
"syntaxes": "python",
"format_on_save": false,
"ruff_path": ""
}
Style comes from the project’s ruff configuration: the closest .ruff.toml, ruff.toml or pyproject.toml with a [tool.ruff] section above the file. CodeFormatter has no Python style options of its own. Files that this configuration excludes are left unchanged: its exclude list (by default ruff’s own list, such as .venv, dist and node_modules), its extend-exclude list, and its [tool.ruff.format] exclude list ([format] in a ruff.toml).
For a buffer that was never saved, ruff looks for a configuration upward from Sublime Text’s working directory, which is usually not the project folder, and otherwise falls back to its user-level configuration or its defaults. Save the file inside the project to format it with the project’s style.
If you format Python files with another package, set "syntaxes": "" in codeformatter_python_options so CodeFormatter leaves them alone.
Installing ruff:
- macOS:
brew install ruff
- Debian/Ubuntu:
sudo apt install pipx, then pipx install ruff, then pipx ensurepath, then log out and back in so that Sublime Text started from the desktop sees ~/.local/bin
- Other Linux:
curl -LsSf https://astral.sh/ruff/install.sh | sh
- Windows (PowerShell):
powershell -c "irm https://astral.sh/ruff/install.ps1 | iex"
- WSL: if Sublime Text runs inside WSL, install ruff inside WSL with the Linux steps. If Windows Sublime Text edits files under
\\wsl.localhost, install ruff on Windows with the Windows steps. A ruff installed only inside WSL cannot be used by Windows Sublime Text.
Restart Sublime Text after installing ruff, and open a new terminal before running which ruff or where.exe ruff below, because a program that is already running keeps the PATH it started with.
Sublime Text started from the macOS Dock may not see the same PATH as your terminal, so it may not find ruff. In that case, or whenever CodeFormatter reports that ruff is not found, set ruff_path to the output of which ruff in a terminal. On Windows, run where.exe ruff (in PowerShell or the Command Prompt) and use the first line it prints. Write that path with forward slashes or doubled backslashes, because a single backslash starts an escape sequence in the settings file: "ruff_path": "C:/Users/you/.local/bin/ruff.exe" or "ruff_path": "C:\\Users\\you\\.local\\bin\\ruff.exe". Copy the whole codeformatter_python_options block into Settings - User and change ruff_path there, because a block that holds only ruff_path replaces the default block, which drops syntaxes and turns Python formatting off.
Go
Go - used gofmt
Currently no options are supported:
"codeformatter_go_options":
{
"syntaxes": "go",
"format_on_save": false
}
Visual Basic/VBScript
Visual Basic/VBScript - used custom approach using the HTML beautifier as a guide
Language specific options:
"codeformatter_vbscript_options":
{
"syntaxes": "vbscript",
"format_on_save": false,
"indent_size": 1,
"indent_char": "\t",
"indent_with_tabs": true,
"preserve_newlines": true,
"max_preserve_newlines": 10,
"opening_tags": "^(Function .*|Sub .*|If .* Then|For .*|Do While .*|Select Case.*)",
"middle_tags": "^(Else|ElseIf .* Then|Case .*)$",
"closing_tags": "(End Function|End Sub|End If|Next|Loop|End Select)$"
}
Coldfusion Markup Language
Language specific options:
"codeformatter_coldfusion_options":
{
"syntaxes": "coldfusion,cfm,cfml",
"format_on_save": false,
"indent_size": 2,
"indent_char": " ",
"indent_with_tabs": false,
"exception_on_tag_mismatch": false,
"expand_javascript": false,
"expand_tags": false,
"minimum_attribute_count": 2,
"first_attribute_on_new_line": false,
"reduce_empty_tags": false,
"reduce_whole_word_tags": false,
"custom_singletons": ""
}
Usage
Tools -> Command Palette (Cmd+Shift+P or Ctrl+Shift+P) and type Format Code.
You can set up your own key combo for this, by going to Preferences -> Key Bindings - User, and adding a command in that huge array: { "keys": ["ctrl+alt+f"], "command": "code_formatter" },. Default keybinding is ctrl+alt+f. You can use any other key you want, thought most of them are already taken.
TODO
Add other languages support:
Add tests
Troubleshooting
If you like living on the edge, please report any bugs you find on the CodeFormatter issues page.
Contributing
Pull requests are welcome.
See CONTRIBUTING.md for information.
License
Please see the LICENSE included in this repository for a full copy of the MIT license,
which this project is licensed under.
评论