How to run a function in the terminal everytime a key is pressed(bind the whole keyboard) - bash

I have been playing with bash for the past week and would like to create a function that is called every time I press a key on the keyboard.
I read about bind but it forces me to bind to a specific key.
If anybody can assist me I would appreciate it
I would like to accomplish this without new shells like zsh or fish

Related

How to setup Toggle Key to display the shell script output during execution?

In my team they have asked me to create a shell script with toggle key option to display the command output/errors when a arrow key is pressed. And it need to hide the output when the same arrow key is pressed. Is there a way we can achieve this using a shell script?
I don't have a solution yet but the script which we are using now is throwing so many output and its making the screen loaded without so many lines of std output/errors during execution. And we would like to hide the output using the toggle key and when needed if we press a key it needs to display the output during execution.
No idea on how to start with this. Need some idea or thoughts on how to achieve this?
Expected:
Shrink the output using toggle key (say for example UP arrow key) and when needed if we press the same UP arrow key it need to display the output again.
Actual:
No code available as of now to do this function.

Doing something if any key is pressed

I was wondering how to make a script so every time I click a key on my keyboard the script runs my wanted commands and loop

Bash Alias for Pressing Up in the Command Line

I personally hate having to go down to press the up-arrow-key when I want to repeat a command on the command line.
Is there a way to have map a bash alias to pressing the up-arrow-key? Or something that's also as convenient? I would like to just press 'n' then 'enter' instead of 'up-arrow-key' and then 'enter'
By default, bash uses the emacs keybindings. Ctrl-p and ctrl-n (for previous and next) do the same as up and down arrow.
To view and edit interactively, bash gives you the builtin bind command. help bind shows you usage, and, for example, bind -P shows you all the current bindings. Any remapping you do can be put into your .inputrc file for next time.
Also, bash provides what it calls HISTORY EXPANSION, similar to what was present in older shells. Just type !! and hit enter to execute the previous command. There's an entire section in the documentation with extra features.
Finally, I'll note that if you are repeating commands so often that hitting up-arrow enter is annoying, you might consider writing a script to do whatever you're doing.
You can issue the command set -o vi, or add it to your startup file. Then, you can hit the escape key and use the movement keys from vi to scroll. You can use k to scroll backwards, j to scroll forward, and h and `l' to move left and right.

Change Color of Text Being Currently Used (Terminal - OSX)

For reference purposes: I am using a Macbook Pro Retina (2014) and all software is up to date as of the time I am posting this question. (Using OSX 10.10.2 Yosemite)
I have been struggling for the past few days as I attempted to customized the Terminal window. Everything was going fine... until I decided I only wanted to change the color for the text that you are currently typing (text input) which will then be executed.
I have no problems getting into my .bash_profile and adjusting anything. I simply cannot seem to grasp the color concept well enough such that it only does this one thing for me. Currently my .bash_profile looks like this:
export PS1="\n\n\njboned$ "
export PATH="/usr/local/mysql/bin:$PATH"
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
Any assistance would be greatly appreciated. As a sidenote: I understand the 8-bit snippets used to represent the individual colors, however it seems I cannot figure out how to use them to address only the text input only.
To do what you are asking, these steps would be needed:
at the end of the prompt, turn on the text color which you want to show
when you press return (to complete editing) turn the text-color off.
bash does not reset attributes while you edit, so the color "should" work — while editing. As you edit, bash is likely to use escape sequences which clear the current line (which may fill it with whatever background color you have selected).
The real problem is how to reset the colors when you press Enter. That does not appear to have a straightforward solution: I do not see a way to rebind the Enter key to add features—no distinction is made between levels of interpretation, and you may not find it possible to enhance the Enter key. The key binding feature in bash talks mainly to the readline library; leftovers are sent to bash. In a binding you may be able to do these things:
send the name of a macro to bash, or a full echo command which resets colors (since readline has no echo of its own, it seems)
the Enter key (i.e., ^M), and
to readline directly, the accept-line function
Alternatively, what you could do is bind another key, say control/L to do the bash accept-line function as well as resetting color. Here are a couple of links which you would find useful to investigate how to do this:
how to bind the 'Enter key'
Complex keybinding in bash
In bash, how do I bind a function key to a command?
Smart preparsing with the bash shell

Remap Caps lock key to Esc in Mma 7

TLDR: How do I get CapsLock to translate to "ShortNameDelimiter" in Mma 7?
I like pretty text in my mma notebooks, and often define functions as f[\[Alpha]_] =... so as to match the exact equation that I'm working with. As such, it involves a lot of Esc-letter-Esc sequences, and reaching for Esc every other stroke breaks my flow of typing.
Now, the CapsLock key is seldom used (I can't remember the last time I needed it), but conveniently placed (your pinky is right there!). Remapping it to Esc on vim worked wonders for me and I was wondering if there was a way to do the same in mma, without having to modify the system's keyboard layout.
I tried editing KeyEventTranslations.tr by adding the following in EventTranslations[{...
Item[KeyEvent["CapsLock"], "ShortNameDelimiter"]
but that had no effect. Is there another way to do it? Is CapsLock not the correct identifier? If it helps, I'm using Mma7 student version on a Mac.
Modifier keys are handled quite specially, and I doubt Mathematica will be able to override the system. You probably have to do this in a layer between Mathematica and the OS. BUT, it is possible to make the key behave different depending on the application you are in. Thus with a bit of work, it MAY be possible to have the capslock key behave differently only in Mathematica.
edit: I did not see you say which operating system you had, so I've added Mac instructions.
Windows
For example, if you have Windows, you can use the program called http://www.autohotkey.com/ . It specifically has a feature where you can bind a key to a script, specifically the following script:
How can a hotkey or hotstring be made exclusive to certain program(s)?
In other words, I want a certain key to act as it normally does except when a specific window is active.
In the following example, NumpadEnter is made to perform normally except when a window titled "CAD Editor" is active. Note the use of the $ prefix in "$NumpadEnter", which is required to let the hotkey "send itself":
$NumpadEnter::
IfWinNotActive, CAD Editor
{
Send, {NumpadEnter}
return
}
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
This next example is more pure than the above, but it will only work if the "CAD Editor" application is designed to ignore the NumpadEnter key itself. The tilde prefix (~) makes NumpadEnter into a non-suppressed hotkey, meaning that the NumpadEnter keystroke itself is always sent to the active window, the only difference being that it triggers a hotkey action. The ~ feature requires Windows NT/2k/XP.
~NumpadEnter::
IfWinNotActive, CAD Editor
return
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
To quote from "MRCS" in this forum post, you may find the following useful:
The first one I named CapsLockR.ahk and contains the following script:
CapsLock UP::Run C:\Documents and Sett...[path to script]...\CapsLock.ahk
The second one is named CapsLock.ahk and has this script:
GetKeyState, state, CapsLock, T
if state = D
SetCapsLockState, off
else
SetCapsLockState, on
exit
Thus worse comes to worst, if you are having trouble modifying the "Behave like Foo if Active Window = Mathematica else behave like Bar" script, you can tack on this to manually toggle the CapsLock state I think. Googling will also reveal more results.
Linux
I know that on Linux, you can use the program called xbindkeys to bind the CapsLock to a script, from which you can in turn call xdo if you detect Mathematica is one of the topmost windows (e.g. via Getting pid and details for topmost window , or xdotool getwindowfocus) or worse-comes-to-worst, you can just have a script which toggles your configuration between CapsLock -> xdotool key Escape, xdotool type "whatever", xdotool key Escape ("Mathematica mode") and "normal mode"... though that may prevent you from YELLING AT MATHEMATICIANS OVER INSTANT MESSAGING WHILE DOING MATHEMATICS. Unless you You may need to find some way to programatically toggle CapsLock, perhaps by creating a dummy CapsLock key (though that's an extreme hack, it is likely one can find some kind of library; perhaps Anybody know how to toggle caps lock on/off in Python? may be useful). (This issue could be avoided by using a key besides CapsLock, or not caring that you want to keep your CapsLock functionality; you could also just turn another key you never use into CapsLock.)
Mac
Mac may have similar tools. For example, you can get xdotool like on Linux above via the MacPorts project. I hear the CapLock key cannot normally be rebound as easily on Mac, so if you can deal with another key it may be much easier. But theoretically it should be possible...
If you wish to use CapsLock, you can use PCKeyboardHack http://pqrs.org/macosx/keyremap4macbook/extra.html to remap the CapLock key to something which will tell OS X to let you remap the CapsLock. Then you remap it, then bind the key using Quicksilver to a script that makes calls xdotool to check if you're in Mathematica also also to issue the :esc:...:esc: if you are (see the Linux section of this answer). Otherwise you simulate a keypress on the CapsLock. But you remapped CapsLock! So you might need to make another dummy key you never use into the CapsLock key, and trigger a keypress on that using Cocoa libraries or a simple AppleScript. If you wish to pursue the CapsLock route, you might find Using Caps Lock as Esc in Mac OS X useful.

Resources