iPython line continuation of command shell string literal - ipython

In iPython, when using the command shell command (the exclamation point operator), is there a way to do line continuation of long string literals?
E.g.
t = !echo "Hello"\
"World"
t
produces ['Hello'] as the value for t instead of the expected ['HelloWorld'].

You can use a "cell magic" (which is basically a multi-line version of other magics like !). If you enter %%bash at the command prompt, it will give you a new line with ..., meaning that you should type your command. Press enter again, and you get another ..., until you just hit enter twice in a row, and it runs your commands. For example, you can type just the following.
%%bash --out t
echo "Hello "\
"World"
(and hit enter twice at the end). It works as expected. In particular, note the --out t, which stores the standard output in the variable t. If you evaluate that now, you get 'Hello World\n'. You can also get the standard error output with --err, as described nicely on this page.
In the ipython notebook, you just put the whole thing in one cell and evaluate it all together. (If you haven't tried the notebook, I highly recommend it.)

Related

Tab-complete herbstclient with fish shell?

I'd like to be able to simply type herb and then press tab and have fish-shell tab-complete it to herbstclient. I've tried looking it up, but every result I can find has to do with overiding fish's autocomplete with tab rather than ctrl + f.
Do I need to create a fish script for this? If so, what should it look like and where should I put it?
Assuming that is a command you want an abbreviation: abbr herb herbstclient. Although you don't use [tab] to complete an abbreviation; you have to press [space] or [enter]. Note that the expansion can consist of multiple tokens. For example, this is one of my most often used abbreviations: abbr -a -g -- gcm 'git checkout master'. Also, abbreviations only get expanded in the command position; i.e., start of line or after a pipe, |, or semicolon. If you want that expansion elsewhere in a command line there are ways to achieve that but it's a bit more complicated.

How to return to the `re>` prompt to start testing a new pattern when using `pcre2test`?

$ ./pcre2test
PCRE2 version 10.32 2018-09-10
re> /this bar/
data> this bar
0: this bar
When trying to hit ^D or ^C, it just simply exits or when trying different strings, that usually work on other REPLs, get simply interpreted as string literals. (Which makes sense, given the purpose of the library.)
From the pcre2test documentation:
An empty line or the end of the file signals the end of the subject
lines for a test, at which point a new pattern or command line is
expected if there is still input to be read.
That is, simply press enter on the data> prompt.

How do you do multiple line entry in the command line REPL of SuperCollider?

Just as the title says, How do you do multiple line entry in the command line REPL of SuperCollider? For example, in Haskell, you use :{ and :} to open and close multiple line entry in ghci.
If you're using sclang with the -i flag (meaning e.g. sclang -i something), the keycode to execute previously entered text is esc, followed by a newline. For example:
~a = {
"test".postln;
}^[
~a.();^[
outputs: test
This works file if you're driving sclang from an IDE or another scripting context (this is used for sclang's unit tests). If you're using the repl, it appears that there ISN'T a way to do multiline entries - the repl uses readline which doesn't have multiline support out of the box. This should probably be filed as a bug.

How to open a file and select/highlight several lines on Sublime from the command line?

I know subl myfile.txt:5 would open “myfile.txt” on line 5. I however want to be able to, from the command line, open a file with say lines 5,9,15 highlighted or selected. I know adding –command should enable me to do that, but how? What would the command be?
There's no built-in command that I know of that can do this, but one can easily create one.
(Technically, it could be done using the bookmarks functionality from the Default package, and the built-in "Expand Selection to Line" functionality. However, experience shows that it would be better and more reliable to write a command in ST specifically for this purpose.)
In ST:
from the Tools menu -> Developer -> New Plugin...
select all and replace with the following
import sublime
import sublime_plugin
class SelectSpecificLinesCommand(sublime_plugin.TextCommand):
def run(self, edit, lines):
self.view.sel().clear()
for line in lines:
position = self.view.text_point(int(line) - 1, 0) # internally, line numbers start from 0
self.view.sel().add(self.view.line(position))
save it, in the folder ST recommends (Packages/User/) as something like select_lines.py (file extension is important).
subl myfile.txt
subl --command "select_specific_lines { \"lines\": [5, 9, 15] }" (this style of escaping the quotes for JSON strings works from the Windows Command Prompt and Linux's Bash)
Why did I specify the command on a separate line / call to subl? Because of these 2 caveats:
ST must already be running, otherwise commands specified on the command line may not get executed, because the plugins haven't loaded yet.
the command could get executed before the file is loaded.
Arguably, point 2 could still happen with multiple invocations of subl, but hopefully it is less likely. There is an open issue on the ST bug tracker for better command line command handling: https://github.com/SublimeTextIssues/Core/issues/1457.

Matlab-like command history retrieval in unix command line

In Matlab, there is a very nice feature that I like. Suppose I typed the command very-long-command and then a few several commands afterwards. Then later if I need the long command again, I just type very and press the up arrow key, my long command appears. It finds the last command that starts with very. I couldn't do the same in unix command line, when I try to do it, it disregards whatever I typed, and goes back to the last commands in chronological order. Is there a way to do it?
In bash this functionality is provided by the commands history-search-forward and history-search-backward, which by default are not bound to any keys (see here). If you run
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
it will make up-arrow and down-arrow search backward and forward through the history for the string of characters between the start of the current line and the point. See also this related Stack Overflow question.
In bash, hitting ctrl-r will let you do a history search:
$ echo 'something very long'
something very long
$ # blah
$ # many commands later...
(reverse-i-search)`ec': echo 'something very long'
In the above snippet, I hit ctrl-r on the next line after # many commands later..., and then typed ec which brought me back to the echo command. At that point hitting Enter will execute the command.
You can do the same thing by using "!". For example:
$ echo "Hello"
Hello
$ !echo
echo "Hello"
Hello
However, it is generally a bad idea to do this sort of thing (what if the last command did something destructive?). If you expect you will reuse something, then I suggest you create a shell script and save it away somewhere (whenever I plan to reuse something, I create a script in ~/.local/bin).