How to open STDIN in Sublime Text with syntax formatting? - command-line

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" }'

Related

Show current branch on terminal

Is there any way to show in Terminal of VS Code to show in brackets current branch? I saw it somewhere but not sure how it can be done. By some extension or whatever..
C:/myUser/project> git status
I would like to see it something like:
C:/myUser/project>(master) git status
Open zshrc file
open ~/.zshrc
Add this text in the end of zshrc file
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats 'on branch %b'
setopt PROMPT_SUBST
PROMPT='%n in ${PWD/#$HOME/~} ${vcs_info_msg_0_} > '
Source zshrc file
source ~/.zshrc
For Linux Terminal
You can modify the PS1 variable. PS1 is a Bash Environment Variable that represents the primary prompt string which is displayed when the shell is ready.
You can achieve your result by modifying this variable with a script.
First, get the output of your current value of the variable by running
$ echo $PS1
Sample output:[\u#\h \W]$
Now you save the following code in a bash file(Remember to replace the initial string of export PS1 with the output of the above command).
#!/bin/bash
source ~/.bashrc
get_cur_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="[\u#\h \W]\$(get_cur_branch)\$ "
Let's say path of the file is "/home/samar/Documents/my_vs_script.sh"
Now change your VS code settings by adding the following lines in 'settings.json'
"terminal.integrated.shellArgs.linux": [
"--init-file",
"/home/samar/Documents/my_vs_script.sh"
]
Now each time you open a new terminal in VS code, script file "my_vs_script.sh" will execute and you get the desired output.
For Windows-Powershell
The solution above works well for the Linux terminal. But if you want to do it for another command-line shell-like Powershell, you can change the 'setting.json' to
{
"terminal.integrated.shellArgs.windows": [
"-NoExit",
"-Command", "c:/scripts/myscript.ps1"
]
}
where 'myscript.ps1' must have a function 'prompt' definition to add git branch to your prompt.
You can refer this question for your 'myscript.ps1' code.
You don't need to change 'Microsoft.PowerShell_profile.ps1'. Defining it in another file works too.
I hope it helps.

Compile text using command line compiler, not a file

I'm using luac -p file.lua to parse files to check for syntax errors. Is it possible to do something like this:
luac -p | [a bunch of text]
Someone mentioned something about 'piping' but I couldn't figure out how that would help.
What I'm wanting to do is take text from a program I am writing and put all that text into the compiler with -p so it just parses the text. Basically I want to check syntax of my program's textarea without having to write it to a file first.
In bash you can do
luac -p - << EOF
Then type your text. To indicate end, just type
EOF
on new line and press enter.

bash script to build complex command syntax, print it first then execute - problems with variable expansion

I want to create scipt to faciliate producing local text file extracts from Hive.
This is to basically execute commands like below:
hive -e "SET hive.cli.print.header=true;SELECT * FROM dropme"|perl -pe 's/(?:\t|^)\KNULL(?=\t|$)//g'>extract/outbound/dropme.txt
While the above works like a charm I find it quite problematic to implement through the parametrized following script (much simplified):
#!/bin/sh
TNAME=dropme
SQL="SELECT * FROM $TNAME"
echo $SQL
echo "SQL: $SQL"
EXTRACMD="hive -e \"SET hive.cli.print.header=true;$SQL\"|perl -pe 'BEGIN{if(defined(\$_=<ARGV>)){s/\b\w+\.//g;print}}s/(?:\t|^)\KNULL(?=\t|$)//g'>extract/outbound/$TNAME.txt"
echo "CMD: $EXTRACMD";
${EXTRACMD}
When run I get: Exception in thread "main" java.lang.NumberFormatException: For input string: "e"
I know there may be many flavours you can print the text or execute command. For instance the line echo $SQL prints me list of files in the directory instead:
SELECT file1.txt file2.txt file3.txt file4.txt FROM dropme
while the next one: echo "SQL: $SQL" gives just what I want: SQL: SELECT * FROM dropme
echo "CMD: $EXTRACMD" prints the (almost) the command to be executed. Almost, as I see \t in perl code being expanded:
CMD: hive -e "SET hive.cli.print.header=true;SELECT * FROM dropme"|perl -pe 'BEGIN{if(defined($_=<ARGV>)){s\w+\.//g;print}}s/(?: |^)\KNULL(?= |$)//g'>extract/outbound/dropme.txt
Maybe that's still ok, but what I want is to be able to copy&paste this command into (other) terminal and execute as the command I put at the top. Ideally I would like that command to be exactly the same (so with \t there)
Biggest problem I have comes when I try to execute it (${EXTRACMD} line). I'm getting the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "e" …and so on, irrelevant as bash treats every 'word' as single command here. I assume as I don't even know what is really tries to run (prior print attempt obviously doesn't help)
I'm aware that I have multiple options, like:
escaping special characters in the command definition string (like I did with doublequotes)
experimenting with echo and $VAR, '$VAR' or "$VAR"
experimenting with "${EXTRACMD}" or evaluating through eval "${EXTRACMD}"
experimenting with shopt -s extglob or set -f
but as number of combinations is quite large and with my little bash experience I feel it's better to ask for good practice here so my question is:
Is there a way to print a (complex/compound shell) command first and subsequently be able to execute it (exactly as per printed output)? In this case it would be printing the exact command from the top, then executing it the same way as by manually copying that output into terminal prompt and pressing Enter.
Do not construct commands as strings. See http://mywiki.wooledge.org/BashFAQ/050 for details.
That page also talks about a built-in way of getting the shell to tell you what it is running (section 6).
If that doesn't do what you want you can also, with bash, try using printf %q\\n "${arr[*]}".

Single line create file with content

The OS is Ubuntu. I want to create file.txt in /home/z/Desktop where the content of the file is some text here.
The first and usual way is run nano /home/z/Desktop/file.txt and type some text here. after that, press ctrl+x, pressy followed by Enter.
The second way is run cat > /home/z/Desktop/file.txt, type some text here and press Enter followed by ctrl+c
I hope I can run single line of command to make it faster. I thought xdotool will work with the cat (the second way), but no, it not works
You can use "echo" in bash. e.g.:
echo "some text here" > file.txt
If you don't want a new line character at the end of the file, use the -n argument:
echo -n "some text here" > file.txt

Cygwin shortcut for command history

How can I search the command history in cygwin?
I don't want to keep pressing the arrow keys to execute a command from console command history.
If you are using the default editing mode, do ctrl+R to search back through your history.
If you have done set -o vi to use vi editing mode, then it is esc-/
The history command is the way to go. I use
h ()
{
history | cut -f 2- | sort -u | grep -P --color=auto -e "$*"
}
so that I can type something like h git.*MyProgram, h ^tar -c, h svn:ignore, etc to pull up a sorted list of past commands matching a regex.
You might also want to add the following lines to ~/.inputrc:
# Ctrl+Up/Down for searching command history
"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward
With these in place, you can type a partial command prefix (such as gi or sql) then use Ctrl+Up to scroll back through the list of just your command history entries that match that prefix (such as git clone https://code.google.com/p/double-conversion/ and sqlite3 .svn/wc.db .tables). This can be a lot faster than searching and then cutting and pasting if you want to edit or re-execute a command that was fairly recent.
I use the history command in combination with grep, e.g. history | grep vi shows all commands where vi was used.
Checkout the "Gnu Bash Manual" (man bash) for the command "fc". E.g.fc -l -80 would list the last 80 commands, while other options let you search with RegEx...
Do
vi ~/.inputrc
Add
For arrow up/down bindings:
"\e[A": history-search-backward
"\e[B": history-search-forward
Or for page up/down bindings:
"\e[5~": history-search-backward
"\e[6~": history-search-forward
Close and open cygwin.
Voila.
I think one of the easiest way is to pipeline it with less and press search character ("/") and then type the command you wanna find.
history | less
and then
/text to find
to find the desired command
Another way
is to append the stdout form history command to a file: history > /cygdrive/path/file.txt
and then search in the document.