Bash - bind key to a string (not a function) - bash

How do I get a key binding to result in inserting arbitrary text at the command line? In zsh I do this:
bindkey -s "^[m" 'myspecialscript '
In bash, is there an equivalent? None of the built-in functions will do what I want.

bind '"\e[[E": "/home/yourscript.sh\n"' binds to f5 replace the first part with your key pref

Related

Zsh color inside command substitution

Coloring output of commands in zsh is kind of simple. Consider the following example in zsh prompt:
print -P "%F{cyan}$(date +'%H:%M:%S')$reset"
You get cyan HH:MM:SS as expected. It works in prompt as expected as well. Now suppose I want to color minutes and seconds in a different color. I didn't manage to achieve it using %F{color}, is it possible?
I can make it work using ANSI codes, but even then it works with print and does not work when used as prompt in ~/.zshrc:
print -P "%F{cyan}$(date +'%H:\e[38;5;82m%M:%S')" - works in zsh
RPS1="%F{cyan}$(date +'%H:\e[38;5;82m%M:%S')" as a right prompt gives 17:\e[38;5;82m14:11
What am I missing? How do I escape the color code or even better use zsh %F{color} construct?
It would have some quoting problems.
It could not been used double quoets; the $(date...) part would be expanded, RPS1 would not be updated for each prompts.
It could be unescaped any escape(\e)s. (especially \e[38;5;82m part for date command)
So, for PS-like strings, it would be useful to quote using $'...' forms like this:
setopt promptsubst
RPS1=$'%F{cyan}$(date +"%H:%%{\e[38;5;82m%%}%M:%S")%{\e[0m%}'
If you can find the color index for \e[38;5;82m:
RPS1=$'%F{cyan}$(date +"%H:%%{%%F{82}%%}%M:%S")%{\e[0m%}'
It could be found by some tools like https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
Note: \e[38;5;82m and \e[0m are surrounded with %{...%}.
%{...%}
Include a string as a literal escape sequence. The string within the braces should not change the cursor position. Brace pairs can nest.
--- zshmisc(1), Visual Effects, Prompt Expansion
Note2: setopt promptsubst. without this option, print -P ... nor RPS1=... does not work.
PROMPT_SUBST
If set, parameter expansion, command substitution and arithmetic expansion are performed in prompts. Substitutions within prompts do not affect the command status.
--- zshoptions(1), PROMPT_SUBST, zsh options
setopt promptsubst
print -P $'%F{cyan}$(date +"%H:%%{\e[38;5;82m%%}%M:%S")%{\e[0m%}'
;# => 23:54:18
PS: %F{color} constructs would be easier for copy-pasting variables with dumping them on screen.
> print $RPS1 ;# this output could not been used for copy-pasting
%F{cyan}$(date +"%H:%%{%%}%M:%S")%{%}
> print $RPS1 | cat -v ;# this either (but close to)
%F{cyan}$(date +"%H:%%{^[[38;5;82m%%}%M:%S")%{^[[0m%}
Version 1 - Calling date only once:
d=$(date +'%H:%M:%S');h=${d:0:2};ms=${d:3:5};
RPS1="%F{cyan}$h:%F{green}$ms%F{default}"
Version 2 - Calling date twice:
RPS1="%F{cyan}$(date +'%H'):%F{green}$(date +'%M:%S')%F{default}"
There is no need to use the external command date: Zsh has built-in prompt escapes for displaying date and time:
[…]
%D{string}
string is formatted using the strftime function. See man page strftime(3) for more details.
[…]
So the simple coloring can be achieved with
RPS1='%F{cyan}%D{%H:%M:%S}%f'
In order to have two colors, you can just use two %D{…} blocks and color them differently
RPS1="%F{cyan}%D{%H}:%F{82}%D{%M:%S}"
This can be as complex as you need (want):
RPS1='%F{154}%D{%H}%F{155}:%F{156}%D{%M}%F{157}:%F{158}%D{%S}'

How to obtain variable values from a text file for use by a bash script?

I have a bash script, and I'd like it to read in the values of variables from a text file.
I'm thinking that in the text file where the values are stored, I'd use a different line for each variable / value pair and use the equals sign.
VARIABLE1NAME=VARIABLE1VALUE
VARIABLE2NAME=VARIABLE2VALUE
I'd then like my bash script to assign the value VARIABLE1VALUE to the variable VARIABLE1NAME, and the same for VARIABLE2VALUE / VARIABLE2NAME.
Since the syntax of the file is the syntax you would use in the script itself, the source command should do the trick:
source text-file-with-assignments.txt
alternately, you can use . instead of source, but in a case like this, using the full name is more clear.
The documentation can be found in the GNU Bash Reference Manual.

Bash Set cursor in prompt using expansion

I'm trying to set the cursor in place to make an autocompletion later in prompt, but I'm not achieving the desired result. The action goes like this:
First, I define a variable to expand like a command I use very often
my_command="script_name parameter1 /some/path/ parameterN"
As I want to place the cursor at the end of /some/path/ then I define another variable as:
set_cursor='$my_command; $(echo -en "\033[10D")'
Then I type $set_cursor on the prompt and the first expansion goes well, but the second just prints
script_name parameter1 /some/path/ parameterN; ^[[10D
Instead of setting the cursor in place.
What is the point i'm missing? I suppose this can be achieved, but I'm misunderstanding the use or having some misconception.
Thanks.

is any way to make sublime bind one single key

I use sublime text 3, it's a great tool, as I know sublime can easily bind a combined key (like CtrlA), but if I only one bind like \ key, is that possible?
and I have tried to make the config to the keymap file, but it didn't work.
It is possible to assign key bindings to single keys. In your case, since the \ symbol escapes the symbol following it (for example, \n is the escape sequence for a newline), you need to escape it with another \:
{ "keys": ["\\"], "command": "my_command" }
This will bind the command my_command to the \ key.

Bash keybinding with "bind" - avoid recursion like noremap in Vim

I've remapped the following keys in Bash:
bind '"a" "b"'
bind '"b" "c"'
If I press a or b both times a c will be printed.
How can I map the keys so that by pressing a and b will be printed and only by pressing a and c will be printed (like with Vims **nore**-map)?
You can make a char in the right part a literal with a preceeding "^V":
bind '"x":"^Vx "'
The key sequence to enter the text after colon is:
" Ctrl-v Ctrl-v x space "
In bash "Ctrl-v x" results in a literal x, without key-mapping interpretation .
You may find it useful to build your own keymapping file which maps the numeric signal you get from the keyboard to a character. This is done with the loadkeys command i.e. if you're switching to the dvorak layout you can do (this is a verbose way to show you the location of the map files:
loadkeys /usr/share/keymaps/i386/dvorak/dvorak.map.gz
You could copy the map file for the layout you use and change the relavent chars, and load your modified mapping. I think loadkeys only affects the command line, though there are similar methods for changing the behaviour in X I believe.

Resources