Sublime Forum

Add string to file via command line

#1

I would like to add a textstring to an existing file with the command line. I’m working on a windows 10 machine.

With the cmd.exe I’m already in the sublime-folder.

I’m trying this:
subl test.js --comand insert hello

But it is not inserting hello to the file test.js. This command opens test.js and is creating a new file with the name hello.
What I’m doing wrong?

0 Likes

#2

you need to pass the command arguments as JSON, try something like:

subl test.js --command "insert { \"characters\": \"hello\" }"

note that if the file is not already open, it may be better to do it as two separate operations:

subl test.js && subl --command "insert { \"characters\": \"hello\" }"

otherwise its possible it could execute the command before it opens the file you specify

2 Likes

#3

Thanks for your help.
It’s not working in one line.

But if write it in two lines it sends the string to the file.
subl test.js
subl --command “insert { “characters”: “hello” }”

0 Likes