Plugin for Indentation in Sublime Text 3 [duplicate] - sublimetext3

I've got some poorly-formatted HTML code that I'd like to reformat. Is there a command that will automatically reformat HTML code in Sublime Text 2 so it looks better and is easier to read?

You don't need any plugins to do this.
Just select all lines (CTRL+A) and then from the menu select Edit → Line → Reindent.
This will work if your file is saved with an extension that contains HTML like .html or .php.
If you do this often, you may find this key mapping useful:
{ "keys": ["ctrl+shift+r"], "command": "reindent" , "args": { "single_line": false } }
If your file is not saved (e.g. you just pasted in a snippet to a new window), you can manually set the language for indentation by selecting the menu View → Syntax → language of choice before selecting the reindent option.

There are half a dozen or so ways to format HTML in Sublime. I've tested each of the most popular plugins (see the writeup I did on my blog for full details), but here's a quick overview of some of the most popular options:
Reindent command
Pros:
Ships with Sublime, so no plugin install needed
Cons:
Doesn't delete extra blank lines
Can't handle minified HTML, lines with multiple open tags
Doesn't properly format <script> blocks
Tag
Pros:
Supports ST2/ST3
Removes extra blank lines
No binary dependencies
Cons:
Chokes on PHP tags
Doesn't handle <script> blocks correctly
HTMLTidy
Pros:
Handles PHP tags
Some settings to tweak formatting
Cons:
Requires PHP (falls back to web service)
ST2 only
Abandoned?
HTMLBeautify
Pros:
Supports ST2/ST3
Simple and no binaray dependencies
Support for OS X, Win and Linux
Cons:
Chokes a bit with inline comments
Does not expand minimized or compressed code
HTML-CSS-JS Prettify
Pros:
Supports ST2/ST3
Handles HTML, CSS, JS
Great integration with Sublime's menus
Highly customizable
Per-project settings
Format on save option
Cons:
Requires Node.js
Not great for embedded PHP
Which is best?
HTML-CSS-JS Prettify is the winner in my book. Lots of great features, not much to complain about.

The only package I've been able to find is Tag.
You can install it using the package control. https://sublime.wbond.net
After installing package control. Go to package control (Preferences -> Package Control) then type install, hit enter. Then type tag and hit enter.
After installing Tag, highlight the text and press the shortcut Ctrl+Alt+F.

I recommend this plugin: HTML/CSS/JS Prettify, It really works.
After the installation, just select the code and press CTRL+SHIFT+H.
Done!

Just a general tip. What I did to auto-tidy up my HTML, was install the package HTML_Tidy, and then add the following keybinding to the default settings (which I use):
{ "keys": ["enter"], "command": "html_tidy" },
this runs HTML Tidy with every enter. There may be drawbacks to this, I'm quite new to Sublime myself, but it seems to do what I want :)

Altough the question is for HTML, I would also additionally like to give info about how to auto-format your Javascript code for Sublime Text 2;
You can select all your code(CTRL+A) and use the in-app functionality, reindent(Edit -> Line -> Reindent) or you can use JsFormat formatting plugin for Sublime Text 2 if you would like to have more customizable settings on how to format your code to addition to the Sublime Text's default tab/indent settings.
https://github.com/jdc0589/JsFormat
You can easily install JsFormat with using Package Control (Preferences -> Package Control) Open package control then type install, hit ENTER. Then type js format and hit ENTER, you're done.
(The package controller will show the status of the installation with success and errors on the bottom left bar of Sublime)
Add the following line to your key bindings (Preferences -> Key Bindings User)
{ "keys": ["ctrl+alt+2"], "command": "js_format"}
I'm using CTRL+ALT+2, you can change this shortcut key whatever you want to. So far, JsFormat is a good plugin, worth to try it!
Hope this will help someone.

For me, the HTML Prettify solution was extremely simple. I went to the HTML Prettify page.
Needed the Sublime Package Manager
Followed the Instructions for installing the package manager here
typed CMD+SHIFT+P to bring up the menu
Typed prettify
Chose the HTML prettify selection in the menu
Boom. Done. Looks great

There's a plugin called SublimeHtmlTidy which works pretty well
https://github.com/welovewordpress/SublimeHtmlTidy

Simply go to
Edit -> Tag -> Auto-format tags on document

I created a Package called HTMLBeautify that does a decent job of reformatting HTML. I based it off of a Perl script I found back in 1997—I updated it to work with all the new fangled modern tags. :)
Check it out and let me know what you think!
https://github.com/rareyman/HTMLBeautify

I think this is what you're looking for:
https://github.com/victorporof/Sublime-HTMLPrettify

I am yet to have the privilege to comment so this is simply additional information related to #peter's answer above answer.
I found HTML did not align as expected if IE conditional comments in the header were not completely in-line e.g. flush to the left:
<!--[if lt IE 7]>
<p class='chromeframe'>Your browser is <em>unsupported</em>. Upgrade to a different browser or install Google Chrome Frame to experience this site.</p>
<![endif]-->
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

There is a nice open source CodeFormatter plugin, which(along reindenting) can beautify dirty code even all of it is in single line.

I'm using tidy together with custom build system to prettify HTML.
I have HTMLTidy.sublime-build in my Packages/User/ directory:
{
"cmd": ["tidy", "-config", "$packages/User/tidy_config.cfg", "$file"]
}
and tidy_config.cfg file in the same directory:
indent: auto
tab-size: 4
show-warnings: no
write-back: yes
quiet: yes
indent-cdata: yes
tidy-mark: no
wrap: 0
And just select build system and press CTRL+B or CMD+B to reformat file content. One minor issue with that is that ST2 does not automatically reload the file so to see the results you have to switch to some other file and back (or to other application and back).
On Mac I've used macports to install tidy, on Windows you'd have to download it yourself and specify working directory in the build system, where tidy is located:
"working_dir": "c:\\HTMLTidy\\"
or add it to the PATH.

you can set shortcut key F12 easy!!!
{ "keys": ["f12"], "command": "reindent" , "args": { "single_line": false } }
see detail here.

HTML-CSS-JS Prettify - Hands down the best.
Install Package Control
⌘ + left shift + p (or ctrl + alt + left shift + p) -> Package Control: Install Package
Enter HTML-CSS-JS Prettify
Install node
Restart Sublime Text
Enjoy.

Related

Sublime text 3 package to view JSON response like in Chrome network view

I want long JSON files to collapse in Sublime Text 3 like the Chrome network tab Preview section. Is there any Sublime Text package for this.
Something similar to following picture
Some of the answers here suggested Pretty JSON, but it didn't work for me. Although the installation was successful but still after pressing the shortcut key [command + control + j], it didn't work for me. I double-checked the setting, everything was correct. I was using Sublime version 3.2.2 and my macOS version was Catalina Version 10.15.3. I also found a thread in Github where multiple people mentioned the same issue with Sublime version 3.
Added a screenshot of my settings for reference.
Even though Pretty JSONdidn't work for me what worked for me was HTML-CSS-JS Prettify.
Following are the steps to install through Sublime Package Manager:
1. Cmd+Shift+P
2. type install, select Package Control: Install Package
3. type prettify, select HTML-CSS-JS Prettify
After installing it, for formatting the JSON text we need to use the shortcut key [command + shift + h]. That will format our JSON text.
Note: HTML-CSS-JS Prettify requires NodeJS in your system to work.
Hope this helps.
You can try Pretty JSON. It is the best I have found to prettify and validade JSON codes.
But it won't give list indexes like chrome. At most you can fold those list itens folding by levels.
We will use Pretty JSON package from package control in Sublime Text 3:
Installation
Install this sublime text 2/3 package via Package Control search for package: “Pretty JSON”
Usage
To prettify JSON, make selection of json (or else it will try to use full view buffer) and press keys:
Linux: ctrl+alt+j
OS X: cmd+ctrl+j
Windows:ctrl+alt+j
or through Command Palette Ctrl+Shift+P find “Pretty JSON: Format (Pretty Print) JSON” (you can search for part of it like 'pretty format')
If selection is empty and configuration entry use_entire_file_if_no_selection is true, tries to prettify whole file
If JSON is not valid it will be displayed in status bar of Sublime Text
Had the same problem: Pretty JSON did not work for me (Ubuntu 16.04 LTS and ST 3.2.2).
The alternative suggested by Rito, HTML-CSS-JS Prettify, requires NodeJS in your system to work.
Another alternative that does not require NodeJS which worked fine for me is the Pretty YAML ST-plugin which uses PyYAML internally.
Install is analogous to Rito's comment (and detailed on the github page).
Use Alt+Ctrl+Y to prettify a selection (or whole file)

How can I use a right to left language in "Sublime Text" editor

I want to use some strings which are in my language (Persian) in "Sublime Text", but the editor does not show them correctly, for example:
it should be:
<title>عنوان صفحه</title>
but this is how sublime is showing it:
The encoding is set to utf-8.
What should I do to fix it?
There is This Plugin But It Does Not Work Properly
https://packagecontrol.io/packages/Bidirectional%20text%20support
added by barlop
Important Note- As comments indicate, this plugin doesn't really work. People have issues copy/pasting from sublime into notepad. It's OK for viewing but that's not much of an answer.
Note, sublime with this plugin might show leters in the correct order but copy/paste in notepad and it might not so try copy/pasting in notepad first before typing a whole load in there. Also this plugin might not move the cursor in a right to left fashion. (i.e. type a letter the cursor when typing in a right to left language should move to the left)
To install,
i've tested this on ST3 portable.
if you go to the tools menu you see there's no bidirectional option
Then if you go here
https://packagecontrol.io/packages/Bidirectional%20text%20support
see it mentions
their github page here
https://github.com/praveenvijayan/Sublime-Text-2-BIDI
Click on Clone/Download then download zip
extract it and Copy the folder to the packages folder.. i.e. open sublime, do preferences..browse packages , and it gives a folder like C:\Users\harvey\Downloads\ST3\Data\Packages Paste the folder there. (maybe it's better to put it in the user subdirectory, I don't know).
Then go to the tools menu and you see bidirectional options like bidirectional text that weren't there before
And while you're at it you may want to install "package control" if it's not already installed. Go to tools..command palette.. type 'package' see if package control appears. Or go to preferences, see if there is package control listed. If not then you don't have 'package control' installed. You can install package control by going to https://packagecontrol.io/ and click install and it gives you some python code in 2 tabs, one for ST2, one for ST3, and you can paste it into your console(view..console). But it's not strictly necessary you can install the bidirectional package without doing that.
The RTL-Mirror plugin for sublime 3 will show you the correct text by hitting Ctrl+F1 on Windows and OSX and Shift+F1 on Linux. it's not the perefect solution but it helps.
For the last few years my workflow has to been use VS Code as an IDE, for coding, and Sublime Text 3 (sorry, 4) for editing plain text because of its startup speed and because VS Code would be overkill.
However, since it's pretty clear by this point that the ST devs don't intend to implement RTL support, and because I would rather not have to install a third text editor just to deal with RTL text (which there are very few of anyway), there were only two convenient options left for me to open up RTL files on Windows.
Use Windows 10's Notepad (yes, really)
This is the superior solution since the new Notepad works well, starts up faster than Sublime, and not only renders RTL text but renders it quite well. The only downside is the lack of a dark mode.
Use VS Code (or whatever your preferred IDE is)
Both free IDEs like VS Code and paid ones like PHPStorm and IntelliJ seem to support RTL out of the box, but pretty much the only good reason to do this is if you absolutely need dark mode or are not on Windows - otherwise all of them up start up far slower than Sublime, and in the case of VS Code, render RTL text worse than Notepad does (I can't speak for JetStorm IDEs on that front).
Who knows, if I find a better alternative text editor that I can move my custom theme over to I may eventually move away from Sublime completely to another editor, but until then, sticking to two editors.

Editing e-mails in your browser using (g)vim

Long ago, I was using the hack given in http://vim.wikia.com/wiki/VimTip805 . This hack allowed me to edit any active window frame using gvim. For instance, I was able to edit my gmail replies using gvim.
Unfortunately, the hack does not work to me any more. Is there any other way to achieve the same? Or at least to achieve the same using some concrete browser (for example, firefox)?
Google Chrome
you can use GhostText with running server vim-ghost. Its not ideal, but allow to interactively use external editor (text is automatically copied to a web textarea element each return to normal mode).
Vim-ghost is written in tcl so you need install tcl.
In my case I have tcl installed but without standard library tcllib. So I found lacking packages in tcllib (tcllib/module/sha1 and tcllib/module/json) and copy them to a folder listed in tcl path (the first result of echo $tcl_pkgPath). More info: man pkg_mkIndex or here.
Of course vim-ghost server must be run (eg. in autorun script):
~/.ghost-text-server.tcl &
You can also like Vimium to navigate through web using links, tabs, j, gg and many vim-like features.
You should check vimperator( pentadactyl, which is a fork of vimperator). They offer the ability to edit text boxes, but also offers several additional functions that allow you to control firefox in a similar way to Vim.
If you prefer Vim only for edit text boxes you could try the firefox plugin "It's all text", as mentioned by Kent.
I'd recommend It's All Text plugin for Firefox.

sublime text 3 html autocompletion not working correctly

I am trying to use sublimetext3 for editing html.
If I do
ul tab, then it generates <ul></ul>
if I do
ul.temp tab, then it generates <ul class="temp"></ul>
however, when I am trying
ul>li.temp tab, it is generating ul><li class="temp"></li>
What I am expecting to see is <ul><li class="temp"></li></ul>
I have package control. What am I missing in getting this functionality?
Note: Moved to an answer at the request of the original poster.
Perhaps the behavior you are looking for comes from the Emmet plugin
What you are describing it the expected behavior for Sublime Text.
The functionality that you are looking for comes from Emmet as skuroda said. Just install it using Package Control and you should be good to go.

VIM Omni Completion: Pattern Not Found

Ive been trying to get the VIM auto completion working, but am running into issues unfortunately.
Right now I tried just starting with the basics. trying to get the auto completion to work on HTML documents first. But even this is becoming an issue.
I set the correct DOCTYPE, and have even specified different ones to see if it will work on Transitional, or Strict, or even XHTML or HTML5, but none are seeming to work.
The htmlcomplete.vim file is indeed in the "autoload" directory.
In my vimrc file, the correct setting is applied:
filetype plugin indent on
But still this isn't working, and no amount of research is helping me, because the only relevant results are issues with C++ auto completion, in VIM. (which vim does not fully support at this time, but it DOES support HTML auto completion, as I know because I have had it work on multiple instances of different systems before.)
What do you guys think I should do next? Im not sure where I should go from here..
No matter what, when I use the "omni completion" option. AKA (Ctrl+X, Ctrl+O), it always says:
Omni Completion (^O^N^P) Pattern not found
The syntax highlighting works by default without even using:
:syntax on/:syntax enable
it just recognizes by the filetype as it should. so that works.
The indentation has started working ever since I uncommented the "filetype plugin indent on". but I just cant get this to work correctly.
But maybe, I am missing the point here, and thinking about this the wrong way. So please correct me if I am wrong.
What function I am looking for, is when I type "<" it will automatically bring up a drop down list with all the tags it could be, and say I type "<" it will further refine to "div", etc. This is the functionality I am used to in Vim, without any configuring, just by default.
When I use the command "Ctrl+N" or "Ctrl+P" I get a very small (12 tags) list of possible tags, but that is manual, and not automatically detecting the typed "<".
Am I looking at the wrong thing? Is Omni Completion not the right option I should be looking at configuring? Or was this a different plugin to provide this functionality?
Whenever I look into it, it always refers to "Ctrl+X + Ctrl+O" as the syntax auto completion, so I assumed this is the feature I have come to know. but maybe I am wrong.
So can someone help to explain this better to me, point me in the right direction. Or let me know I am on the right path (if I am) and help me fix this issue?
Thank you guys.
Take care.
As requested here is the information:
The vimrc file is here: http://pastebin.com/QfUDVvdP
My version is 7.3 (aka vim73)
I am using the CLI version, as I find GVIM actually more confusing. but I have both. and they use the same vim runtime.
I have not added any more plugins or extra feautres, as this is a fresh install on Ubuntu 12.04, which is also a fresh install. The only thing that has been altered or added is "smali.vim" syntax highlighting to syntax folder, as well as opa.vim, and opajs.vim. also opacomplete.vim to autoload.
there is vimrc, and vimrc.tiny, as well as gvimrc in /etc/vim/ folder.
and all the files for vim are located in the default directory, /usr/share/vim/vim73/ and /usr/share/vim/. None of the files are moved, changed, or altered besides what was already specified.
hope that helps.
First of all, Omni Completion never worked (or works) automatically. You said popup should appear when you insert '<' - this is not working in Vim by default. You have to press Ctrl X O combination. At least it is not working for me.
http://vimdoc.sourceforge.net/htmldoc/version7.html#new-omni-completion
I guess you have a plugin for that or something. It's not hard to implement it. Start Vim without loading any plugins to check it out. Anyway, to the question.
Many folks do not know, that Vim 7+ has decent support for XML/XHTML/HTML languages (no plugins needed!) with possibilities to extend it with any XML-based language you want. What you can do is to use DTD/RNG converters that prepares Vim definition which is used to give you omni completion.
For example, my Vim installation contains support for HTML4 and XHTML languages by default:
$ rpm -ql vim vim-common | grep xml
/usr/share/vim/vim73/autoload/xml
/usr/share/vim/vim73/autoload/xml/html32.vim
/usr/share/vim/vim73/autoload/xml/html401f.vim
/usr/share/vim/vim73/autoload/xml/html401s.vim
/usr/share/vim/vim73/autoload/xml/html401t.vim
/usr/share/vim/vim73/autoload/xml/html40f.vim
/usr/share/vim/vim73/autoload/xml/html40s.vim
/usr/share/vim/vim73/autoload/xml/html40t.vim
/usr/share/vim/vim73/autoload/xml/xhtml10f.vim
/usr/share/vim/vim73/autoload/xml/xhtml10s.vim
/usr/share/vim/vim73/autoload/xml/xhtml10t.vim
/usr/share/vim/vim73/autoload/xml/xhtml11.vim
/usr/share/vim/vim73/autoload/xml/xsd.vim
/usr/share/vim/vim73/autoload/xml/xsl.vim
/usr/share/vim/vim73/autoload/xmlcomplete.vim
/usr/share/vim/vim73/compiler/xmllint.vim
/usr/share/vim/vim73/compiler/xmlwf.vim
/usr/share/vim/vim73/ftplugin/xml.vim
/usr/share/vim/vim73/indent/xml.vim
/usr/share/vim/vim73/syntax/docbkxml.vim
/usr/share/vim/vim73/syntax/xml.vim
The trick is Vim's autoloading feature. You need to make sure the file you are opening has the proper DOCTYPE definition which is correct. So use that for HTML and XHTML files, then Vim 7+ will automatically enable XML/HTML omni completion for you. Example for HTML4:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
</html>
Now try to insert body tag, type "bo" and hit Ctrl X O. Bang. Try to add an attribute, type "on" and hit it again. Bingo.
You can use Ctrl X O and other features:
after "<" complete the tag name, depending on context
inside of a tag complete proper attributes
when an attribute has a limited number of possible values help to complete
them
complete names of entities (defined in |xml-omni-datafile| and in the
current file with "
when used after "
More info (and possible user customization with own XML definitioins) here:
http://vimdoc.sourceforge.net/htmldoc/insert.html#ft-xml-omni
Omnicompletion is the right feature and <C-x><C-o> is the right shortcut. You should stop calling it autocompletion, by the way, because it's far from being automatic.
You don't need anything in ~/.vim/autoload because htmlcomplete.vim is already in $VIMRUNTIME.
In brief, given what I know of your settings, completion should work.
But it isn't. Please append the content of your ~/.vimrc, a list of installed plugins (and how/where they are installed) and details on your Vim version, platform and if you use the GUI or the CLI version.
edit
Omnicompletion is the right feature but you are looking for a specific plugin that uses omnicompletion under the hood on each couple of keystroke as there's no built-in setting to enable Auto completion. AutoComplPop is one such plugin (and the one I use), there are others. Pick the one that most closely matches with your previous experience.
You shouldn't touch anything in /usr/share/vim/ or /etc/vim/. All your settings should go into ~/.vimrc and your plugins should go into ~/.vim/.
~
.vim/
autoload/
opacomplete.vim
syntax/
opa.vim
opajs.vim
smali.vim
.vimrc
You must revert the default files and directories to their original state before going further.
endedit

Resources