1

Okay, here me out.. I want to create some kind of mapping for the dollar sign $ in Bash. Its the most used special character for me and not in the least difficult place. Yes, I could create an AutoHotKey or Linux equivalent for the Capslock. But within Bash, using an alais wont work because they dont expand more than the first position $0, and arent really designed to. Which is essential because I use $var_name all the time as arguments. Functions can only be shown with the 'type' command.

I would use a key like xx or , and then expand the alias, or other similar operation, to replace that with $. The ',' key is an arithmetic character and is reserved, although I was able to create an alias with it that does expand (only in the first position).

Any solutions or suggestions? Thanks..

Related thread about alias expansion: how to expand aliases inline in bash?

Here is the answer from below using ',,' exportable into bash, plus one for last arg and the help flag:

bind '",,": "$"'
echo "bind '\",,\": \"\$\"'">>~/.bashrc
echo "bind '\",l\": \"\$_\"'">>~/.bashrc
echo "bind '\",h\": \"--help\"'">>~/.bashrc
echo "function helpany { \$1 --help || help \$1 || man \$1 || info \$1; }">>~/.bashrc

And my list of aliases, functions and binds in a public Gist: https://gist.github.com/auwsom/804b4470d22ae00d113cbf5b4eaba73c

alchemy
  • 240
  • 2
  • 9
  • 2
    Are you asking if there is a way to use a different character (other than `$`) to introduce a variable? Regardless, please could you clarify your question? – Attie Mar 18 '20 at 17:48
  • @Attie I'm asking how to create a mapping for the $ character in Bash. I will add 'character' to the title. And did add two sentences about a minute ago. Thanks – alchemy Mar 18 '20 at 17:51
  • @glennjackman, I'm trying to expand some other key *to* $ so I can prepend a variable name with an easier key to reach, and then have it replaced (similar to the expansion of aliases). – alchemy Mar 18 '20 at 18:00
  • Please read [*Can I answer my own question?*](https://superuser.com/help/self-answer) If you want to post an answer, post it as *an answer*, not as a part of the question body. – Kamil Maciorowski Mar 18 '20 at 22:14

1 Answers1

0

There is a way in bash: the bind command.

This will map Ctrle to "$"

bind '"\C-e": "$"'

Look at the current bash keybindings with bind -p and pick a key sequence that's easy for you to type and is not used for anything else (that you want to keep).

glenn jackman
  • 25,463
  • 6
  • 46
  • 69
  • glenn, thanks.. that makes sense, I might have deduced that from the bind to expand an alias, that bind can be used for single characters. Great! This adds bind to the toolbox with alias, function and variable.. – alchemy Mar 18 '20 at 18:14
  • The problem with a binding single characters is that if you need to remove it, you cant, because you cant type the character. Any suggestions for that? I've tried looking in .bashrc and .inputrc for the file and tried 'set -o emacs' to reset default bindings without success. Another option might be to use the key number? ..Bind does accept double characters, but slows the recognition of the single character slightly. – alchemy Mar 18 '20 at 19:50
  • yes, there is 'bind -r ' to remove.. at least one of these methods worked, but only after restarting the shell. – alchemy Mar 18 '20 at 20:30