How to suppress output in Sublime Text cmd? - windows

How can I suppress the output in Sublime Text Build System output?
I tried this but it says Error: unrecognized or incomplete command line.
{
"cmd": ["ipconfig", ">nul 2>&1"],
"name": "ipconfig",
"shell": "true"
}

{
"name": "ipconfig",
"shell_cmd": "ipconfig >nul 2>&1"
}
I always use shell_cmd because you can just write the command like you would on the command line instead of trying to figure out the multiple arguments nonsense in the array. This worked for me.

Related

Redirect error messages to a file in sublime text

Reference image
Is it possible to redirect the message in the shell to a file say error.txt. I use the following build system
{
"cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<input.txt>output.txt2>error.txt"],
"selector": "source.cpp",
"shell": true,
"working_dir": "$file_path"
}
Yes it possible to redirect the message in the shell to a file "error.txt" using following build system.
{
"cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<input.txt","2>error.txt>output.txt"],
"selector": "source.cpp",
"shell": true,
"working_dir": "$file_path"
}
There must be a space between input.txt and error.txt so we separate them with a ',' delimeter . 2>error.txt redirects error stream to error.txt file then output stream is redirected to output.txt file.

Appending to a configuration file

I am creating a script which updates hosts in an application, the config file for each host looks like that below. The script generates the hosts correctly but I need to append every } with a comma , except the last host.
I have tried numerous things but the closest I have got is putting the hosts content on a single line and running a IFS statement against it. Im also not sure how best to approach this, can anyone advise?
{
"cmd": "ssh user#webserver",
"inTerminal": "new",
"name": "webserver",
"theme": "basic",
"title": "Webserver",
}
example of what I am trying to achieve
{
"cmd": "ssh user#webserver",
"inTerminal": "new",
"name": "webserver",
"theme": "basic",
"title": "Webserver",
},
{
"cmd": "ssh user#db",
"inTerminal": "new",
"name": "db server",
"theme": "basic",
"title": "db",
},
{
"cmd": "ssh user#mail",
"inTerminal": "new",
"name": "mail server",
"theme": "basic",
"title": "mail server",
}
You can do things like:
#!/bin/sh
for f in $(generate-host-list); do
read -d \000 c < "$f"
list="$list${list+,
}$c"
done
echo "$list"
If you are just writing to a file that can be simpler (no need for the read, just cat the file). Similarly, if you don't care about munging whitespace, you could do list="$list${list+,}$(cat "$f"). If you are using bash or some other shells you can do non-portable things like += to clean it up.
You can do it like this:
sed '$q; s/^}$/},/' <in_file >out_file
The above sed command works as follows: First check if you've reached the last
line, and if so quit. Otherwise, it'll check if the only character on the line
is }, and if so replace it with },.

Sublime build system to open file at local server address

For a website I'm developing with the help of a local server (running at http://127.0.0.1:4000), I try to write a build system for my Sublime Text project settings.
I have access to these variables (provided by Sublime Text):
$file: Stores the absolute path to the file on disk:C:\Users\User\dev\repos\base\dir\index.html
$project_path: Stores the location of the project file (usually the root of the project):C:\Users\User\dev\repos\base
Now what I want is the content of $file but instead of the $project_path I want http://127.0.0.1:4000/~base. For that task I tried the following with Bash cmd.exe:
CALL SET result=%file:%project_path%=http://127.0.0.1:4000\~base\% && echo %result%
This gives the desired result, however I can't seem to be able to apply it to the build system inside Sublime Text.
For now, I try to generate the correct address and output it via cmd.exe:
{
"build_systems":
[
{
"name": "Preview in browser",
"selector": "text.html",
"windows":
{
"shell": true,
"cmd": [
"start", "cmd", "/k",
"CALL SET result=$file:$project_path=http://127.0.0.1:4000\\~base\\ && echo $result"
]
}
}
]
}
Result:
> echo %result%
C:\Users\Philipp\dev\repos\base\dir\index.html:C:\Users\Philipp\dev\repos
\base=http://127.0.0.1:4000\~base\
So the substitution is not working when doing it in the build system, but in cmd.exe it does. I'm confused.
You can do something like this:
{
"build_systems":
[
{
"name": "Preview in browser",
"selector": "text.html",
"windows":
{
"shell_cmd": "CALL SET filePath=$file && CALL SET result=%filePath:$project_path=http://127.0.0.1:4000\\~base% && CALL C:/Progra~2/Google/Chrome/Application/chrome.exe %result%"
}
}
]
}
The problem was with windows replacement and variable asignation rather than with sublime builds.
As it has been said in chat and comments multiple CALL are necessary to use the real value variable because if not used windows will expand their value at parse time before the wanted value is asigned at execution time. In addition, shell_cmd can be used to run a unique command in shell.

Error in custom sublime build for knitr markdown

I'm trying to create my own sublime-build in Sublime Text 3, since the default build in knitr package (https://sublime.wbond.net/packages/knitr) doesn't work (for me). I modified the one in knitr package as follows:
{
"selector": "text.html.markdown.knitr",
"working_dir": "${project_path:${folder}}",
"cmd": ["Rscript", "-e", "library(knitr); knit('$file')" ],
"shell": true
}
but get the error:
Error: '\G' is an unrecognized escape in character string starting "'C:\G"
Execution halted
[Finished in 0.4s with exit code 1]
[cmd: ['Rscript', '-e', "library(knitr); knit('C:\\GitHub\\Projects\\test\\testLaTeXing\\knitrRmd\\testRmd.Rmd')"]]
obviously some windows path escape issue, but how can I fix this, when I want to acces file path dynamically?
I'm working on windows7.
Try setting your "cmd" line to this:
"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\//}')" ],
Basically, it's a regular expression to match two \ characters and replace them with one / character. Each \ and / needs to be escaped by a \, hence the chicken scratch.
I'm not on Windows to test, but theoretically this should work. If it doesn't, another option to try is
"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\\/}')" ],
where it's replacing the \\ with a single \.
Please let me know how it works, I'm interested to know!
This is what I finally cooked up, thanks to #MattDMo.
{
"selector": "text.html.markdown.knitr",
"working_dir": "${project_path:${folder}}",
"cmd": [ "Rscript -e \"library(knitr); knit('$file', output='$file_path/$file_base_name.md')\"" ],
"shell": true,
"windows":
{
"selector": "text.html.markdown.knitr",
// "working_dir": "${project_path:${folder}}",
// "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\\/\/g}', output='${file_path/\\\\/\\/\/g}/$file_base_name.md' )" ],
"shell": true
}
}
where the default command is from the knitr sublime package (https://github.com/andrewheiss/SublimeKnitr/blob/master/knitr-Markdown.sublime-build)

Go-Sublime-build configuration

Im having issues trying to set up go to run the current file from Sublime text 2.
Here's what I have in my go.sublime-build file
{
"cmd": [ "go", "run", "${file}" ]
}
When I try to run build on a go source file, I get the error
[Error 6] The handle is invalid
[cmd: [u'go run', u'C:\\Users\\gprasant\\Documents\\GitHub\\programming_pearls\\src\\go\\quicksort.go']]
[dir: C:\Users\gprasant\Documents\GitHub\programming_pearls\src\go]
Is there any way to get this fixed ? Or is there another plugin in Sublime text for Go development?
Installing GoSublime should get this working for you. After installing and restarting ST2: do ctrl-B, type "run" and hit enter.
I got by with
{
"cmd": "go run $file",
"shell" : true
}
In ST3: it is changed to be:
{
"shell_cmd": "go run ${file}"
}
On my mac, I needed the following code in:
/Users/your_user_name/Library/Application Support/Sublime Text 2/Packages/User/go.sublime-build
go.sublime-build
{
"cmd": ["go run '${file}'"],
"selector": "source.go",
"path": "/usr/local/go/bin",
"shell": true
}
"cmd" line quoting is to correctly handle file paths with spaces.
"shell" line is needed since commenting it out breaks it.
"path" line is needed because the basic shell, doesn't have access to my .zshrc file include the export GOPATH statement defining the go path.
After that any .go file should build and run with command+B, leaving the stdout message in a console built into sublime text 2.
what about:
{
"cmd": ["go", "run", "${file}"],
"path": "/user/local/go/bin"
}
I like GoSublime, just hate to type run each time when click Command + B
SublimeText 2
build-system for golang, making F4/shift-F4 work (next error/prev error)
1st, create a file: ~/gosublime_build.sh
GOPATH=~/go
export GOPATH
echo "GOPATH:$GOPATH"
if [ "$3." = "RUN." ]
then
EXENAME=${1##*/}
EXENAME=$GOPATH/bin/$EXENAME
echo $EXENAME
$($EXENAME)
echo "code: $?"
exit
fi
echo "go build $2"
cd /usr/local/go/bin
./go build -o ~/temp.go.compiled $2
if [ $? -eq 0 ]
then
cd $1
echo "Project: " $1
/usr/local/go/bin/go install
echo "go install exit code: $?"
else
echo "go build exit code: $?"
fi
2nd:
chmod 777 ~/gosublime_build.sh
3rd: create a new sublime2 build-system for "go" (Tools/Build System/New)
{
"cmd": ["~/gosublime_build.sh $file_path $file"]
,"shell": true
,"selector": "source.go"
,"file_regex": "([\\w/_-]+[.]{1}[\\w./_-]+?):([0-9]+):?([0-9]+)?(.*)?"
}
4th: select your new build-system (Tools/Build System)
5th: build with Ctrl-B, F4/Shift-F4: next/prev error
If anybody knows how to instruct the go compiler to inform FULL PATH of file and line for each error, this process can be simplified

Resources