Is it possible to add multiple commands to the readline .inputrc file? - macos

I'm trying to configure my Terminal and I would like to insert #{} at one key-stroke. This works with the following code
# .inputrc
"\e\"": "#{}"
But I also want the cursor to end up inside the braces. How can I do this? The following doesn't work.
# .inputrc
"\e\"": "#{}": backward-char

Try:
"\e\"": "#{}\e[D"

My immediate way to fix your overall goal (not really answering your question, but hopefully helping you anyway): write a bash alias or function for it. grev() perhaps, or something similar - at least, this is what I would do were I in your situation.
I am interested to see if what you originally asked is possible, however, so voting up your question in hopes that you can get a 'real answer'!

Related

Quick question about Environment Variables use in SH scripts

I have found a script to set the background image and do some more stuff, but that's not important.
Look at this line, it's supposed to set a route, nothing complicated.
#!/bin/sh
bgloc="${XDG_DATA_HOME:-$HOME/.local/share/}bg"
I didn't understand what was it doing, because of the env variable and the :- that comes after it. Now, after some trial and error, I've figured it out. It sets the variable bgloc with the value of that env variable and, if that variable is not set —which is the case—, then, it use the route that comes after the :- .
So, the question is: why does that happen? I mean, I've searched it on the internet, and I haven't found anything. Is it supposed to work like this? In that case, where can I find information about that? I'd like to learn more about it, but I don't find any information on the internet. Some help would be great. Thanks.
A lot can be done with values within ${} in bash.
To find out more about these features I would recommend to read TLDP manual on this topic.

Refreshing Bash prompt after invocation of a readline bound command

My shell is GNU Bash 4.3.11, and I currently have M-h bound to cd .. by calling the builtin
bind -x '"\eh": "cd .."'
This gives me a nifty way to navigate up the directory tree, as I can repeatedly hit M-h instead of the incredibly time-consuming cd ... It has the downside, however, either of not resetting my $PS1 or of not redrawing my prompt, so I lose the context of my current working directory until I enter a new command.
One alternative I'm aware of is to put a macro like
"\eh": "\C-a\C-kcd ..\C-m"
in my .inputrc directly. This, however, has the downside of not only losing the context of any existing command I'm typing in (which I think can probably be worked around) but also of printing out cd .. (which I don't think can be).
My desired behavior is to be able to be in a directory ~/one/two with prompt ~/one/two$; hit M-h; and then be in ~/one and have the prompt be ~/one$, ideally keeping any command I had initially. How can I achieve this?
Figured this out.
# maintain state
bind -x '"\200": TEMP_LINE=$READLINE_LINE; TEMP_POINT=$READLINE_POINT'
bind -x '"\201": READLINE_LINE=$TEMP_LINE; READLINE_POINT=$TEMP_POINT; unset TEMP_POINT; unset TEMP_LINE'
# "cd .." use case.
bind -x '"\206": "cd .."'
bind '"\eh":"\200\C-a\C-k\206\C-m\201"'
I'm quite late to that party - and came here looking for that answer also. First of all: As you were the only one providing information on this: thanks for not letting it come to this: https://xkcd.com/979/ ;) instead you pointed me to the on corner in that fractal that seems to hold a solution.
This approach, in my opinion backed up by hours of trying, is the only one where you can a) replace content on the line, and b) execute bash code. Let me offer up some more suggestions to a specific problem:
If you are trying to have it both ways: insert something on the command line, or executing code, things can get very tricky. for both, there exist bindings, and I let the reader figure out things with help bind. But in the case you e.g. have FZF produce some directory as output, and you'd either cd to it, or have it pasted into your command -- depending on the keystroke done in FZF -- things will get near impossible. you'll face either the not-updated-promt problem, or not be able to execute the cd command in the top shell (where it has effect).
Your solution would be a multiplexing -x binding, inspecting the output for "macros" (get extracted and eval'd) or the default pass-through (manipulating READLINE_LINE/POINT).
Because the solution has some enormity, and the audience may be limited (closed answer...), I'll leave it at a haphazard gist where I pasted my code which works now. To make up for the brevity and uncommented-ness, I welcome any questions in comment or elsewhere. Hope someone may be pointed in the right direction.
- The code related to this question starts in function bindInsertEvalWithMacrosVi
- It is designed for Vi keybindings but the same principles apply for normal readline mode
- It depends on some \C-x\C-... combinations to do redrawing in places that are not related to this post.
https://gist.github.com/simlei/032470cfcd23641987f97a96749128d7

vim advice on getting around

I have been using vim for a few days now and I really like the key bindings and the separate modes. I would really like to be able to be a little more efficient when using the various shortcuts etc.. that it has to offer.
For example I got a vim extension for visual studio and I had this line of code:
SqlCommand sqlcmd = new SqlCommand();
I wanted to get into the braces so I tried 5W, this took me to the first brace, then I pressed 'L' to go in and 'I' to go into insert mode.
I don't feel that I am using it to its potential, does anyone have any suggestions as to a quicker way they would have done that? or suggest things that I can look at to get even more efficient at editing using what vim has to offer, I would be really grateful.
Those two cheat-sheets will get you pretty far:
if your cursor is before the () on that line, you can try pressing:
%i
I think it would be the best way to go there. Of course you could do this as well:
f(a
to the question "how to get more productive"
think about those operations, you thought "complex/not productive" (like the one in your question)
try to find out a better solution by google/vim help/doc
use the new solution in your daily edit
if you cannot find better solution, ask here or other vim community, like vim-use mailing list.

Extending tcsh completion

I must work with tcsh.
I am using an internal tool that provides basic completion for some of its commands.
I would like to extend the completion.
I mean that in future releases the default completion may evolve.
I tried something like this:
set def_cmpl = complete tool
complete tool $def_cmpl 'n/-l/(reg short long gui)/'
But I don't understand the result I get.
Indeed, the quotes inside $def_cmpl are doubled:
tcsh> complete tool
''n#-t#$script#'' n/-l/(reg short long gui)/'
I tried some tricks with echo, sed, etc. but I can't avoid those ''.
Could somebody help me?
Please don't say go on bash... The tool doesn't support it...
Finally, I did not find a solution to keep the data inside the script. So, the solution was to redirect the output of the complete command inside a file and then to append new lines to the file.

How many ways can I get Bash alias completion on a partial substring?

Question: I have a question that is apparently not answered by this already-asked Bash completion question on Stack Overflow. The question is, how to get Bash alias completion (for any alias) on a partial substring.
Example:
For example, assume I have the following aliases:
open.alicehome="cd /usr/home/alice"
open.bakerhome="cd /usr/home/baker"
open.charliehome="cd /usr/home/charlie"
gohomenow="shutdown -now"
I would like to lazily just type "baker{{TAB}}" to invoke the second alias.
I would like to lazily just type "home{{TAB}}" to get a list of all of the above aliases that I can then choose from with the keyboard (optimal) or choose by typing an unambiguous substring that distinguishes among the three options (less than optimal).
.. OR ..
I would like to lazily just type "home" and then repeatedly press {{TAB}} until the specific alias I want shows up, then press {{ENTER}} to execute it.
Feel free to be creative:
If you have a way to do this, even if it requires resorting to extreme guru hackery, please feel free to share it and feel free to explain your guru hackery to the level of a five-year-old who will have to try to implement your idea for himself.
Links to existing web-pages or RTFMs are welcome.
If you really want to change bash's tab behavior,
# ~/.inputrc
# The default binding for [Tab] is ``complete'', which is what we're used to.
# ``menu-complete'' gives you irssi-like behavior: cycle through completions.
"\t": menu-complete
This is documented in The GNU Readline Library # Letting Readline Type For You.
You can generate the matches for a specified substring with compgen -c -X'!*substring*'
To include this in bash autocompletion you can create a .bash_completion file in your home directory containing something like this:
_comp() {
local cur
cur=${COMP_WORDS[$COMP_CWORD]}
COMPREPLY=( $( compgen -c -X '!*'$cur'*' ) )
}
complete -F _comp $nospace eval
This match function will run when you press <TAB> on the arguments to the eval command. I haven't found a way to include a new matching function for the first command search in bash, I've only found documentation on how to add custom completions for a specific command, like eval above.
But I'm a bit skeptical to how useful this is... There is no easy way to select which match you want from the list.
Probably you can achieve what you want with custom bash_completion settings, but I'm not going to do the job for you, ok? :-)
If you don't know what I'm talking about, see here and here for details. By the way, I would find very annoying this
I would like to lazily just type
"home" and then repeatedly press
{{TAB}} until the specific alias I
want shows up, then press {{ENTER}} to
execute it.
which I think is the M$ style of "completion" (are you coming for DOS/windows environment?)

Resources