简体   繁体   中英

How to open STDIN in Sublime Text with syntax formatting?

I'm getting some JSON from curl and I want to open it using Sublime Text.

I've tried

curl ... | subl --command "Set Syntax: JSON" -

Data is opened, but syntax is not applied. Is my command syntax wrong?

Yes. The command subl expects is not the human readable text from the Command Palette, but the actual underlying command and arguments.

To see what these are, you could open the ST console (View menu -> Show Console) and type sublime.log_commands(True) Enter , then switch input focus back to the main file and set the syntax from the command palette. In the console, you'd see the command that was invoked.

Then, you'd end up trying something like (replacing echo[...] with your curl command):

echo '{ "test": 123 }' | subl --command 'set_file_type { "syntax": "Packages/JSON/JSON.sublime-syntax" }' -

...and it still wouldn't work. And you'd notice something odd if you had a non-JSON file focused previously. That file would now be highlighted as if it were JSON.

Why? Because it applies the command before it finishes opening the file (or in this case, opens a tab for the STDIN stream).

Can it be worked around? Yes, generally a separate invocation of subl to execute the command gives Sublime Text enough time to switch the focus to the newly created tab:

echo '{ "test": 123 }' | subl - && subl --command 'set_file_type { "syntax": "Packages/JSON/JSON.sublime-syntax" }'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM