Sublime Forum

From command line: create a buffer with specified text?

#1

Using the command line, is it possible to create a new file with some pre-defined text?

E.g. something like…

subl path/to/new/file.md "hello there!"

…which should open a sublime window corresponding to (an as of yet unsaved) file at path/to/new/file.md with the text from the command line inserted into the first line:

hello there!
0 Likes

#2

You might be able to do this using subl --command.

Just trigger the command ‘insert_snippet’

0 Likes

#3

Nice. Ok-- just tried it, but no luck. Perhaps I don’t understand how to use --command (just discovered it this afternoon, and can’t find much for docs on it).

I tried:

subl --command 'insert_snippet{"woohoo"}' ~/Desktop/yay.md

and

subl --command 'insert_snippet{"contents":"woohoo"}' ~/Desktop/yay.md

Both open a file called yay.md, but no snippet is inside of it. I also found that there’s a sublime command called “insertSnippet” here: http://www.sublimetext.com/docs/commands, tried with “insertSnippet” instead of “insert_snippet” but still no dice.

Am I just misusing the --command argument?

0 Likes

#4

Update…
It seems I’m using the --command argument properly, as I can set the color_scheme successfully this way:

subl --command 'set_setting {"setting": "color_scheme", "value": "Packages/Color Scheme - Default/Solarized (Dark).tmTheme"}'

However, I’ve found two commands, both “insert” and “insert_snippet”, neither of which work when used in the following way (the file is opened, but no string is present in the newly opened sublime tab):

subl --command 'insert {"string" : "woohoo"}' ~/Desktop/yay.md subl --command 'insert_snippet {"contents" : "woohoo"}' ~/Desktop/yay.md

Commands found at: http://docs.sublimetext.info/en/latest/reference/commands.html

Any insight very much appreciated! :smile:

0 Likes

#5

Someone might generously write a plugin that creates a new file and inserts text as an argument. That way you can trigger it from the command line. I’m supposed to be working on something else at the moment, or else I would help. Sorry.

0 Likes

#6

If you are starting it from the shell anyway, why not just use the shell functionality?

echo "woohoo" > ~/Desktop/yay.md && subl ~/Desktop/yay.md
0 Likes

#7

quarnster: there you go making me look bad again… Stop trying to make everything simpler :imp:

0 Likes

#8

It’s not a competition mate…

0 Likes

#9

[quote=“quarnster”]If you are starting it from the shell anyway, why not just use the shell functionality?
CODE: SELECT ALL
echo “woohoo” > ~/Desktop/yay.md && subl ~/Desktop/yay.md
[/quote]

Jeez, of course. Thanks a lot for being the guiding light here, I wound up in a maze there, shell somehow eluded me. Thanks again.

0 Likes