Matlab-like command history retrieval in unix command line - matlab

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).

Related

Nothing happens when I try to generate a key

I'm trying to generate a key for my computer, but I notice that when I try running the command that I see on GitHub.com, nothing happens, and I only get a prompt. I'm wondering what could be happening? Advice is appreciated, thanks!
I tried searching up how to generate an SSH key, and it seems that I'm supposed to be getting a different output. I tried going into different directories but got the same issue.
Edit: Adding text output, since I can't seem to produce an image.
[username]$ ssh-keygen -t ed25519 -C “[Github email]"
(Output): ">"
One of your quote symbols is the wrong type of quote symbol - specifically, it's U+201C LEFT DOUBLE QUOTATION MARK. Re-type the command - it was probably messed up by copying it from somewhere.
If your shell prompts you with a >, that means you are not done entering your command yet. Try typing something simple like
echo "test
Bash will display a > and wait for you to finish the command. In this case it's because you've opened a quote (the last character in your command) and not yet closed it.

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.

In Scala REPL, how to discard partly written multi-line command?

On Unix Shell terminal, you can discard a typed command just by typing ctrl-c.
Is there a way to discard a typed command on Scala REPL?
Ctrl-C still works but it would exit the Scala REPL, which is probably not what you prefer.
A couple of cases:
If you're in the middle of a single-line command, you can hit Up button then Down to an empty command line.
If you're in the middle of a multi-line command and the command is incomplete, you can simply hit return a couple of times to let the REPL interpret the consecutive blank lines as intention to start a new command. But, in case you worry about whatever you've typed might get executed, safest way would still be to hit Ctrl-C.
When you're writing an unfinished block you can enter two blank lines and the REPL will then skip your command and start a new one.
You may then still use up buttons to retrieve the lines you want to continue with in the new command.

Emacs: How to read command help just as one would read Man pages?

It's nice to run M-x man foo for command foo within Emacs. It's then possible to navigate easily within the man page and to find details quickly.
When the help of a command (such as git) produces limited output, one can just use a terminal instead of emacs.
But occasionally, a command (such as aws help—run in a terminal) produces extensive output. Yet the output is not compatible with the emacs Man mode. An option is to use M-x shell within emacs, but that will not display the page at once. It will report "WARNING: terminal is not fully functional" and require pressing a key endlessly until the complete help appears, or, for Emacs 25, "Could not find executable named 'groff'".
What is a good way to read long manual pages produced by commands within emacs?
I just ran into this exact problem a few days ago.
Type escape + ! then type (for example) “aws ec2 help”. This brings the help text into a new buffer called Shell Command Output, with all of the control-h characters, etc.
Switch to the new buffer with control-x then lowercase ‘o’ (for other buffer)
Type escape + lowercase ‘x’ to run an emacs function, then type ‘man’ and hit Enter. It will prompt for man page entry and default to EC2, just hit Enter. In my case, it displays an error in the status line, “error in process sentinel: Can’t find the EC2 manpage”.
However, the “man page” functions are now available, so now (in that buffer)you can type escape + x and run the function Man-fontify-manpage. The page should now look like a nice man page with bold, etc.
You can then rename the buffer (escape + x then something like ec2) so the buffer isn’t replaced if you run another shell command.
I you just want the output in a buffer, you can simply use:
M-! aws help RET
If you want the output in a shell buffer, you can do git help commit | cat (so no more "terminal is not fully functional").
Probably you can do M-! aws help | cat RET also. I do not have aws, but hopefully the piping will remove the escape characters if aws output formatting is done right. You should try also TERM=dumb aws help. Any command should know better than using fancy output when TERM is set to dumb. If aws is dumb that way itself, you could pipe its output to something that filters out control characters -- try this
For forcing man mode in an arbitrary buffer, M-x Man-mode (yes, uppercase). I am not sure if that will play nice with aws's output.
By the way, for git I suppose you know you can do man git-commit (or man git-any_git_command, in general), so you have a nice alternative to git help when using emacs (output of help and man page is the same).

How can one provide colour to tab completion in tcsh?

(Crossposted to unix.stackexchange.com)
This question and the answer teach us how to introduce colour into tcsh prompts.
This webpage explains nicely how to get colour into any output of the echo command:
> echo \\e[1\;30mBLACK\\e[0m
BLACK
> echo '\e[1;30mBLACK\e[0m'
BLACK
The word 'BLACK' in the example above is printed with a black (or darkgrey) foreground colour (depending on the overall color scheme).
Now I'd like to introduce this into the [TAB] command autocompletion feature of tcsh. I tried:
complete testcmd 'p/*/`echo '"'"'\e[1;30mf834fef\e[0m'"'"'`/'
And I get:
> testcmd [TAB]
> testcmd ^[\[1\;30mf834fef^[\[0m
Obviously the characters lose their special meaning. Hopefully I just did not get the escaping right. But I tried several other ways. So any help is appreciated.
The real use case is that I've got a command completion that offers three different types of completions and I'd like to visually distinguish the types. Also the alternatives are computed by an external command. That is why I need the completion to use the backticks with an external command, such as echo. I don't care about the details of this command. If you make it work in any way with the tcsh's complete command I'll probably be able to adapt (thinking perl -pe wrappers and such).
The reason why I believe this has to work somehow is that the tcsh itself offers coloured command completion if you e.g. type ls [TAB]. That works correctly in my setup. Also you can use ls -1F inside the autocompletion and the colours that ls outputs are also piped through. An example would be:
complete testcmd 'p/*/`ls -1F`/'
Update: As user mavin points out, the colourization of ls in this example is indeed not piped through. The colours of ls are lost, but the auto completion can reapply colours according to LS_COLOURS variable based on hints such as the / and * marker endings as added by the ls. This can be verified by doing
complete testcmd 'p/*/`ls --color -1`/'
which fails to provide colour, and only provides garbled output. (Literally pipes through the escape character sequences)
I'm on tcsh version 6.13.00
Any ideas? Pointers?
In your example, complete testcmd 'p/*/ls -1F/', the colours aren't getting passed through from ls. You'll find that you get colour here even if you set ls to produce monochrome output, but not if you leave off the -F. What's happening is that tcsh is doing its own colouring based on the symbols added to the end of each filename by ls -F. For example:
complete testcmd 'p%*%`echo dir/ symlink# device# socket=`%'
You could exploit this in your completion generator, e.g.,
complete testcmd 'p/*/`echo foo bar | perl -lane '"'"'print join " ", map { $_. "%" } #F'"'"'`/'
The snag is that you end up with these symbols in your completed command-line, and will have to manually backspace each time.
tcsh will colour filenames based on their suffix, dependent on the $LS_COLORS environment variable (e.g., show all *.gz files in red). You could pre-calculate the list of potential completions, place them all in $LS_COLORS, then set up dummy files for the completion to use. If you use the precmd alias, you can have the completions automatically recalculated every time the prompt is shown.
complete testcmd "p#*#F:$HOME/.cache/testcmd-completions#"
alias prep-testcmd "setenv LS_COLORS '*red=01;31:*green=01;32:' && rm -r ~/.cache/testcmd-completions && mkdir -p ~/.cache/testcmd-completions && touch ~/.cache/testcmd-completions/red ~/.cache/testcmd-completions/green"
alias precmd prep-testcmd
Aside: it'd be nice to use this with a ``-style completion rather than an F-style completion; that way you wouldn't need to create the dummy files. However, I tried that in tcsh 6.17 and it didn't work.