Bash: Call Script with Customized Keyboard Shortcuts

Bash: call script with customized keyboard shortcuts?

Simple version:

This command at a shell prompt:

bind '"\ee": "${PWD##*/}\e\C-e"'

or this line added to your ~/.inputrc:

"\ee": "${PWD##*/}\e\C-e"

will cause Alt-e to insert the basename of the current directory on the command line. It requires that the default binding of the readline function shell-expand-line which is \e\C-e be present (this could be adapted if it's different). I'm also making the assumption that you're using Bash's emacs mode.

Unfortunately, it causes things that have already been typed to be expanded as well. One of the affects of this is that after having typed:

svn ci -m "

and pressing Alt-e, the quotation mark will have disappeared. There are a couple of ways to deal with this.

One, assume that all you'll lose is the quote and either manually add it back or have the readline macro add it for you:

bind '"\ee": "${PWD##*/}\e\C-e\eb\"\C-e"'

which just isn't very satisfactory.

Advanced version:

Or, two, kill the line, do the insertion, then yank the line back:

bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b"'

or

bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b\ef\C-f"'

This leaves the rest of the line intact (nothing else is expanded or deleted), but it uses the kill ring, so it may leave it in a state that's different than you expect (if you're using it). It also inserts a space after the inserted directory name (the spaces in the macro are used to ensure that older kill-ring contents are not regurgitated if the macro is executed at the beginning or end of the line). The macro should work regardless of the position of the cursor in the line. The insertion will be made at the cursor's position, leaving the cursor in the same position [in the first version].

Edit: The second version leaves the cursor after the dirname and space that are inserted.

Edit 2:

The readline function shell-forward-word (unbound) does a better job than forward-word (\ef) for this. You can make use of that like this:

bind '"\ew":shell-forward-word'
bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b\ew\C-f"'

By the way, you should know that Bash keyboard shortcuts are not active in other programs such as nano.

Bash: how to configure a keyboard shortcut to run a program and insert its output at the position of cursor?

The answer lies in READLINE_LINE and READLINE_POINT variables set by bind:

-x keyseq:shell-command

Cause shell-command to be executed whenever keyseq is entered. When shell-command is executed, the shell sets the READLINE_LINE variable to the contents of the Readline line buffer and the READLINE_POINT variable to the current location of the insertion point. If the executed command changes the value of READLINE_LINE or READLINE_POINT, those new values will be reflected in the editing state.

For example:

stuff() { 
local pre="${READLINE_LINE:0:$READLINE_POINT}"
local suf="${READLINE_LINE:$READLINE_POINT}"
local stuff='my string here'
READLINE_LINE="${pre}$stuff$suf"
((READLINE_POINT += ${#stuff}))
}

bind -x '"\C-g":"stuff;"'

Credits to Greg Wooledge who replied to my question in help-bash mailing list

String comparison in a Bash script called from terminal or keyboard shortcut gives different output

Jdamian's suggestion to use:
if [ "$currentWindowPosition" = below ]
works. Notice the single equal sign.

Is there a way to trigger a Hot Key/Keyboard Shortcut via a Shell Script, a AppleScript or a Automator Workflow?

tell application "System Events" to keystroke "l" using command down & shift down

Bind keyboard shortcut to bash function to interrupt sleep?

Unless you modified your bash with a built-in sleep, you can kill the sleep command. The script will then proceed to the next command as if sleep terminated normally. The only tricky part is to identify the correct process to kill. Here I assume that there is only one randomwallpaper process running on your system:

exec --no-startup-id randomwallpaper
bindsym $mod+i exec --no-startup-id sh -c 'pkill -P $(pgrep -ox randomwallpaper) sleep'

By the way; Your script could use some improvement. For instance, the variable file is unused and --randomize has no effect since you only supply one picture.

#!/bin/bash
shopt -s nullglob
cd /home/user/Pictures/bgart
while true; do
i=$(shuf -en1 ./*.jpg)
if [ -n "$i" ]; then
feh --bg-scale "$i"
wal -q -i "$i"
fi
sleep 300
done

Launch script from Kodi menu

This partially answer your question: instead of creating an item in the main menu, it allows creating a keyboard shortcut to your script.

Two built-in functions allows executing bash scripts:

System.Exec(exec)

Execute shell commands. The full path to the script has to be placed inside the parentheses.

System.ExecWait(exec)

Execute shell commands and freezes Kodi until shell is closed. As well as for "System.Exec(exec)", the full path to the script has to be placed inside the parentheses.

The information comes from here. I've tested the second one which works flawlesly with a bash script located in ~/.local/bin but with one limitation: you can't pass argument to the script. ~/.local/bin is in the default user $PATH (on Raspbian) which allowed me to use only the script name and not the full path.

You can call this function by creating a user keyboard shortcut file

nano ~/.kodi/userdata/keymaps/keyboard.xml

and adding into it

<keymap>
<global>
<keyboard>
<a mod=shift,ctrl>System.ExecWait(script.sh)</a>
</keyboard>
</global>
</keymap>
  • keymap is the overall description of the file and should not be changed.
  • global refers to which window the link can be available (global is a fallback if the shortcut is not used in the current specific window)
  • keyboards refers to the keyboard shortcuts (you can add gamepad, mouse, etc.)
  • a is the key activating the shortcut, here with the modifiers shift+ctrl (that can be dropped for a single key shortcut, but beware of conflicts with existing shortcuts). Don't forget to also modify it at the end of the line.
  • script.sh is your script name (full path if not in the user $PATH) that must be executable, as goes for all shell scripts.

How to execute Python code from within Visual Studio Code

Here is how to configure Task Runner in Visual Studio Code to run a .py file.

In your console, press Ctrl + Shift + P (Windows) or Cmd + Shift + P (Apple). This brings up a search box where you search for "Configure Task Runner"

Sample Image

If this is the first time you open the "Task: Configure Task Runner", you need to select "other" at the bottom of the next selection list.

This will bring up the properties which you can then change to suit your preference. In this case you want to change the following properties;

  1. Change the Command property from "tsc" (TypeScript) to "Python"
  2. Change showOutput from "silent" to "Always"
  3. Change args (Arguments) from ["Helloworld.ts"] to ["${file}"] (filename)
  4. Delete the last property problemMatcher
  5. Save the changes made

Sample Image

You can now open your .py file and run it nicely with the shortcut Ctrl + Shift + B (Windows) or Cmd + Shift + B (Apple).



Related Topics



Leave a reply



Submit