Use backtick/Grave/Tilde as modifier key - linux

I'm looking for a way to create keyboard shortcuts in Linux using backtick (`) / tilde (~) key and some other key. In an ideal case:
pressing tilde down does nothing
pressing another key while the tilde is pressed down triggers a (customizable) shortcut
when releasing the tilde before/without pressing another key, just send the tilde keystroke.
I have something similar in AutoHotKey for Windows, and i've been searching for a way to recreate this in a (any) Linux environment. I would consider using any GUI if i could get this working, but of course a more "general" solution would be even better.

I think i finally got it!!
I use xmodmap to turn the grave key into the modifier Hyper_L, and XCape to send the grave if the key is released without another key being pressed.
Xcape was intended to open the app menu ("Start menu") when the meta-key ("windows key") is pressed and released without another key, so as an added bonus, it does that too. Meaning you can both use Meta as a modifier, like Meta-F to open the file manager AND use the meta-key seperately to open the whiskers menu.
If all is right, you can open the keyboard settings manager using ~-k, and you can make new shortcuts using the ~-key. Because that's still tiresome and not easily portable between different systems, i've included some shortcuts using xfconf-query, which will probably only work in Xfce.
Here's the basics of my script:
#!/bin/sh
# reset pretty much ALL keyboard settings
setxkbmap
# Free up the mod3 and mod4 flags from all keys it may be associated with:
xmodmap -e "clear mod3"
xmodmap -e "clear mod4"
# Add Hyper_L to the grave key (49)
xmodmap -e "keycode 49 = Hyper_L asciitilde grave asciitilde"
# You need a grave key somewhere else (!) so, bind it to an unused key:
xmodmap -e "keycode 250 = grave"
# Restore Mod4 but without Hyper_L (which was at location 4)
xmodmap -e "add mod4 = Super_L Super_R Super_L"
# Assign the mod3 to Hyper_L:
xmodmap -e "add mod3 = Hyper_L"
dist=100
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Right -s "xdotool mousemove_relative -- $dist 0" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Down -s "xdotool mousemove_relative -- 0 $dist" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Left -s "xdotool mousemove_relative -- -$dist 0" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Up -s "xdotool mousemove_relative -- 0 -$dist" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>space -s "xdotool click 1" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>k -s "xfce4-keyboard-settings" --create -t string
# (re)starting xcape to produce a ` after key-up if no other key was pressed
killall xcape
xcape -t5000 -e "#49=grave;Super_L=Control_L|Escape" &
A more extended version of the script, with some more shortcuts can be found here.

I am not sure if it will work out for you but you should check:
xdotool (http://manpages.ubuntu.com/manpages/precise/man1/xdotool.1.html)
zenity (http://manpages.ubuntu.com/manpages/precise/man1/zenity.1.html)
Both these tools let you create some custom actions and shortcuts.
Here is an example of xdotool: https://askubuntu.com/questions/212154/create-a-custom-shortcut-that-types-clipboard-contents
Hope it helps and good luck :)
Bruno

After switching to Ubuntu on another machine, I also wanted to use tilde as a modifier key the way I use it in my AHK scripts.
I did quite some research on different tools for ex. xdotool, xev, autotools, xbindkeys and so on and finally found a solution to that. Here are the steps.
Install Autokey, python, python evded module, xte(sudo apt-get install xautomation).
Read a little about Autokey and how it launches python scripts or create hotstrings. In Autokey, we can set a hotkey to call a python script. So you can assign the python script down below to your tilde key or for that matter whatever custom hotkey you plan to create.
Here is the required custom functionality(thought not yet completely ported to linux, I have it scripted in autohotkey and just love it. It keeps the hand glued to keyboard ;))
Tilde + Up arrow: Move mouse pointer upwards by 100 positions
Tilde + Down arrow: Move mouse pointer downwards by 100 positions
Tilde + Right arrow: Move mouse pointer to right by 100 positions
Tilde + Left arrow: Move mouse pointer to left by 100 positions
Tilde + Enter: Left Mouse click (not present in the python script)
Tilde + Alt + Enter: Right Mouse Click
I use the tilde (KEY_GRAVE) as my modifier key. When this key is pressed, Autokey launches the python script. The scripts runs a loop until the tilde key is released. In the loop, the script keeps on detecting the keyboard inputs. On a UP arrow key(KEY_UP) press, the script sends a command to move a mouse by relative position (0, -100) utilizing 'xte' and so on.
from evdev import InputDevice, categorize, ecodes
from select import select
dev = InputDevice('/dev/input/event4')
releasekey = False
while releasekey==False:
r,w,x = select([dev], [], [])
for event in dev.read():
if event.type == ecodes.EV_KEY:
#system.exec_command("xte 'mousermove 0 3'", False)
#break
if event.code == ecodes.KEY_UP:
if event.value == 1:
system.exec_command("xte 'mousermove 0 -100'", False)
if event.code == ecodes.KEY_DOWN:
if event.value == 1:
system.exec_command("xte 'mousermove 0 100'", False)
if event.code == ecodes.KEY_RIGHT:
if event.value == 1:
system.exec_command("xte 'mousermove 100 0'", False)
if event.code == ecodes.KEY_LEFT:
if event.value == 1:
system.exec_command("xte 'mousermove -100 0'", False)
if event.code == ecodes.KEY_GRAVE:
if event.value == 0:
releasekey = True
break
You have to adjust the dev = InputDevice('/dev/input/event4') line to assign the correct name of your keyboard. In my case, event4 is my keyboard. Yours might be different. You can check out the handy tutorial "Reading Events" on python-evdev. That code actually outputs the name of your keyboard listed under /dev/input. Actually, my script is an extention to that tutorial script.
The only problem is that the python script must be started as a root user otherwise the keyboard input device can not be accessed. You can overcome this by creating a udev rule file which changes the permission of the device to make it available for reading writing eg. create a rule file and add this line
KERNEL=='event4', MODE="0660" and load the rule . At the end you must add yourself in the GROUP which have read/write permission for the device. The information regarding file permission can be found using ls -la in the /dev/input folder.
I hope it works for you. In it doesn´t work at first go, then get a cup of coffee and fight on till it works ;)

Related

Copy Terminal Output For GUI Pasting [duplicate]

Is it possible to pipe to/from the clipboard in Bash?
Whether it is piping to/from a device handle or using an auxiliary application, I can't find anything.
For example, if /dev/clip was a device linking to the clipboard we could do:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
There are a wealth of clipboards you could be dealing with. I expect you're probably a Linux user who wants to put stuff in the X Windows primary clipboard. Usually, the clipboard you want to talk to has a utility that lets you talk to it.
In the case of X, there's xclip (and others). xclip -selection c will send data to the clipboard that works with Ctrl + C, Ctrl + V in most applications.
If you're on Mac OS X, there's pbcopy. E.g., cat example.txt | pbcopy
If you're in Linux terminal mode (no X) then look into gpm or Screen which has a clipboard. Try the Screen command readreg.
Under Windows 10+ or Cygwin, use /dev/clipboard or clip.
Make sure you are using alias xclip="xclip -selection c"
or else you won't be able to paste using Ctrl+v.
Example:
After running echo -n test | xclip, Ctrl+v will paste test
Install
# You can install xclip using `apt-get`
apt-get install xclip
# or `pacman`
pacman -S xclip
# or `dnf`
dnf install xclip
If you do not have access to apt-get nor pacman, nor dnf, the sources are available on sourceforge.
Set-up
Bash
In ~/.bash_aliases, add:
alias setclip="xclip -selection c"
alias getclip="xclip -selection c -o"
Do not forget to load your new configuration using . ~/.bash_aliases or by restarting your profile.
Fish
In ~/.config/fish/config.fish, add:
abbr setclip "xclip -selection c"
abbr getclip "xclip -selection c -o"
Do not forget to restart your fish instance by restarting your terminal for changes to apply.
Usage
You can now use setclip and getclip, e.g:
$ echo foo | setclip
$ getclip
foo
On macOS, use the built-in pbcopy and pbpaste commands.
For example, if you run
cat ~/.bashrc | pbcopy
the contents of the ~/.bashrc file will be available for pasting with the Cmd + V shortcut.
To save the current clipboard to a file, redirect the output pbpaste to a file:
pbpaste > my_clipboard.txt
2018 answer
Use clipboard-cli. It works with macOS, Windows, Linux, OpenBSD, FreeBSD, and Android without any real issues.
Install it with:
npm install -g clipboard-cli
Then you can do:
echo foo | clipboard
If you want, you can alias to cb by putting the following in your .bashrc, .bash_profile, or .zshrc:
alias cb=clipboard
xsel on Debian/Ubuntu/Mint
# append to clipboard:
cat 'the file with content' | xsel -ab
# or type in the happy face :) and ...
echo 'the happy face :) and content' | xsel -ib
# show clipboard
xsel -ob
# Get more info:
man xsel
Install
sudo apt-get install xsel
Try
xclip
xclip - command line interface to X selections (clipboard)
man
On the Windows Subsystem for Linux (WSL) you can copy to the clipboard with clip.exe:
cat file | clip.exe
Keep in mind to use the | pipe command. And not a > command, since that will not work.
Install the xcopy utility and when you're in the Terminal, input:
Copy
Thing_you_want_to_copy | xclip -selection c
Paste
myvariable=$(xclip -selection clipboard -o)
I noticed a lot of answers recommended pbpaste and pbcopy. If you're into those utilities, but for some reason they are not available in your repository, you can always make an alias for the xcopy commands and call them pbpaste and pbcopy.
alias pbcopy="xclip -selection c"
alias pbpaste="xclip -selection clipboard -o"
So then it would look like this:
Thing_you_want_to_copy | pbcopy
myvariable=$(pbpaste)
An answer located in one of the comments written by a user called doug work for me. Since I found it so helpful, I decided to restate in an answer.
Here is a ready-to-use Bash script for reading the clipboard which works on multiple platforms.
Please edit the script here if you add functionality (e.g., more platforms).
#!/bin/bash
# WF 2013-10-04
#
# Multi-platform clipboard read access
#
# Supports
# Mac OS X
# Git shell / Cygwin (Windows)
# Linux (e.g., Ubuntu)
#
# Display an error
#
error() {
echo "error: $1" 1>&2
exit 1
}
#
# getClipboard
#
function getClipboard() {
os=`uname`
case $os in
# Git Bash (Windows)
MINGW32_NT-6.1)
cat /dev/clipboard;;
# Mac OS X
Darwin*)
pbpaste;;
# Linux
Linux*)
# Works only for the X clipboard - a check that X is running might be due
xclip -o;;
*)
error "unsupported os $os";;
esac
}
tmp=/tmp/clipboard$$
getClipboard >$tmp
cat $tmp
# Comment out for debugging
rm $tmp
For Mac only:
echo "Hello World" | pbcopy
pbpaste
These are located /usr/bin/pbcopy and /usr/bin/pbpaste.
On Windows (with Cygwin) try
cat /dev/clipboard or echo "foo" > /dev/clipboard as mentioned in this article.
There are different clipboards in Linux; the X server has one, the window manager might have another one, etc. There is no standard device.
Oh, yes, on CLI, the screen program has its own clipboard as well, as do some other applications like Emacs and vi.
In X, you can use xclip.
You can check this thread for other possible answers:
http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-07/0919.html
This is a simple Python script that does just what you need:
#!/usr/bin/python
import sys
# Clipboard storage
clipboard_file = '/tmp/clipboard.tmp'
if(sys.stdin.isatty()): # Should write clipboard contents out to stdout
with open(clipboard_file, 'r') as c:
sys.stdout.write(c.read())
elif(sys.stdout.isatty()): # Should save stdin to clipboard
with open(clipboard_file, 'w') as c:
c.write(sys.stdin.read())
Save this as an executable somewhere in your path (I saved it to /usr/local/bin/clip. You can pipe in stuff to be saved to your clipboard...
echo "Hello World" | clip
And you can pipe what's in your clipboard to some other program...
clip | cowsay
_____________
< Hello World >
-------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Running it by itself will simply output what's in the clipboard.
I have found a good reference: How to target multiple selections with xclip
In my case, I would like to paste content on the clipboard and also to see what is been pasted there, so I used also the tee command with a file descriptor:
echo "just a test" | tee >(xclip -i -selection clipboard)
>() is a form of process substitution. Bash replaces each with the path to a file descriptor which is connected to the standard input of the program within the parentheses.
The teecommand forks your command allowing you to "pipe its content" and see the result on standard output "stdout".
You can also create aliases to get and write on the clipboard, allowing you to use "pbcopy" and "pbpaste" as if you where on Mac. In my case, as I use Z shell (zsh), I have this in my aliases file:
(( $+commands[xclip] )) && {
alias pbpaste='xclip -i -selection clipboard -o'
alias pbcopy='xclip -selection clipboard'
}
The (( $+command[name] )) in Z shell tests if the command "name" is installed on your system, and then both aliases are grouped with {}. The && is a binary AND; if a then b, hence if you have xclip then the aliases will be set.
echo "another test" | tee >(pbcopy)
To get your clipboard content, just type:
pbpaste | "any-command-you-need-here"
I just searched the same stuff in my KDE environment.
Feel free to use clipcopy and clippaste.
KDE:
> echo "TEST CLIP FROM TERMINAL" | clipcopy
> clippaste
TEST CLIP FROM TERMINAL
xsel -b
Does the job for X Window, and it is mostly already installed.
A look in the man page of xsel is worth the effort.
Copy and paste to clipboard in Windows (Cygwin):
See:
$ clip.exe -?
CLIP
Description:
Redirects output of command line tools to the Windows clipboard.
This text output can then be pasted into other programs.
Parameter List:
/? Displays this help message.
Examples:
DIR | CLIP Places a copy of the current directory
listing into the Windows clipboard.
CLIP < README.TXT Places a copy of the text from readme.txt
on to the Windows clipboard.
Also getclip (it can be used instead of Shift + Ins!) and putclip (echo oaeuoa | putclip.exe to put it into clip) exist.
In Linux this works:
cat filename | xclip
pbcopy is built into OS X:
Copying the content of file .bash_profile:
cat ~/.bash_profile | pbcopy
There are a couple of ways. Some of the ways that have been mentioned include (I think) tmux, Screen, Vim, Emacs, and the shell. I don't know Emacs or Screen, so I'll go over the other three.
Tmux
While not an X selection, tmux has a copy mode accessible via prefix-[ (prefix is Ctrl + B by default). The buffer used for this mode is separate and exclusive to tmux, which opens up quite a few possibilities and makes it more versatile than the X selections in the right situations.
To exit this mode, hit Q; to navigate, use your Vim or Emacs binding (default = Vim), so hjkl for movement, v/V/C-v for character/line/block selection, etc. When you have your selection, hit Enter to copy and exit the mode.
To paste from this buffer, use prefix-].
Shell
Any installation of X11 seems to come with two programs by default: xclip and xsel (kind of like how it also comes with both startx and xinit). Most of the other answers mention xclip, and I really like xsel for its brevity, so I'm going to cover xsel.
From xsel(1x):
Input options \
-a, --append \
append standard input to the selection. Implies -i.
-f, --follow \
append to selection as standard input grows. Implies -i.
-i, --input \
read standard input into the selection.
Output options \
-o, --output \
write the selection to standard output.
Action options \
-c, --clear \
clear the selection. Overrides all input options.
-d, --delete \
Request that the current selection be deleted. This not only clears the selection, but also requests to the program in which the selection resides that the selected contents be deleted. Overrides all input options.
Selection options \
-p, --primary \
operate on the PRIMARY selection (default).
-s, --secondary \
operate on the SECONDARY selection.
-b, --clipboard \
operate on the CLIPBOARD selection.
And that's about all you need to know. p (or nothing) for PRIMARY, s for SECONDARY, b for CLIPBOARD, o for output.
Example: say I want to copy the output of foo from a TTY and paste it to a webpage for a bug report. To do this, it would be ideal to copy to/from the TTY/X session. So the question becomes how do I access the clipboard from the TTY?
For this example, we'll assume the X session is on display :1.
$ foo -v
Error: not a real TTY
details:
blah blah # 0x0000000040abeaf4
blah blah # 0x0000000040abeaf8
blah blah # 0x0000000040abeafc
blah blah # 0x0000000040abeb00
...
$ foo -v | DISPLAY=:1 xsel -b # copies it into clipboard of display :1
Then I can Ctrl + V it into the form as per usual.
Now say that someone on the support site gives me a command to run to fix the problem. It's complicated and long.
$ DISPLAY=:1 xsel -bo
sudo foo --update --clear-cache --source-list="http://foo-software.com/repository/foo/debian/ubuntu/xenial/164914519191464/sources.txt"
$ $(DISPLAY=:1 xsel -bo)
Password for braden:
UPDATING %%%%%%%%%%%%%%%%%%%%%%% 100.00%
Clearing cache...
Fetching sources...
Reticulating splines...
Watering trees...
Climbing mountains...
Looking advanced...
Done.
$ foo
Thank you for your order. A pizza should arrive at your house in the next 20 minutes. Your total is $6.99
Pizza ordering seems like a productive use of the command line.
...moving on.
Vim
If compiled with +clipboard (This is important! Check your vim --version), Vim should have access to the X PRIMARY and CLIPBOARD selections. The two selections are accessible from the * and + registers, respectively, and may be written to and read from at your leisure the same as any other register.
For example:
:%y+ ; copy/yank (y) everything (%) into the CLIPBOARD selection (+)
"+p ; select (") the CLIPBOARD selection (+) and paste/put it
ggVG"+y ; Alternative version of the first example
If your copy of Vim doesn't directly support access to X selections, though, it's not the end of the world. You can just use the xsel technique as described in the last section.
:r ! xsel -bo ; read (r) from the stdout of (!) `xsel -bo`
:w ! xsel -b ; write (w) to the stdin of (!) `xsel -b`
Bind a couple key combos and you should be good.
On Wayland, xcopy doesn't seem to work. Use wl-clipboard instead.
E.g., on Fedora:
sudo dnf install wl-clipboard
tree | wl-copy
wl-paste > file
The Ruby oneliner inspired me to try with Python.
Say we want a command that indents whatever is in the clipboard with four spaces. It is perfect for sharing snippets on Stack Overflow.
$ pbpaste | python -c "import sys
for line in sys.stdin:
print(f' {line}')" | pbcopy
That's not a typo. Python needs newlines to do a for loop. We want to alter the lines in one pass to avoid building up an extra array in memory.
If you don't mind building the extra array try:
$ pbpaste | python -c "import sys; print(''.join([f' {l}' for l in sys.stdin]))" | pbcopy
but honestly awk is better for this than python. I defined this alias in my ~/.bashrc file
alias indent="pbpaste | awk '{print \" \"\$0}' | pbcopy"
Now when I run indent, whatever is in my clipboard is indented.
A few Windows programs I wrote years ago. They allow you dump, push, append and print the clipboard. It works like this:
dumpclip | perl -pe "s/monkey/chimp/g;" | pushclip
It includes source code: cmd_clip.zip
Yesterday I found myself with the question: "How can I share the clipboard between different user sessions?". When switching between sessions with Ctrl + Alt + F7 - Ctrl + Alt + F8, in fact, you can't paste what you copied.
I came up with the following quick & dirty solution, based on a named pipe. It is surely quite bare and raw, but I found it functional:
user1#host:~$ mkfifo /tmp/sharedClip
then in the sending terminal
user1#host:~$ cat > /tmp/sharedClip
last, in the receiving terminal:
user2#host:~$ cat /tmp/sharedClip
Now, you type or paste anything in the first terminal, and (after hitting Return), it will appear immediately in the receiving terminal, from where you can copy and paste again anywhere you like.
Of course this doesn't just strictly take the content from user1's clipboard to make it available in user2's clipboard, but rather it requires an additional pair of Paste & Copy clicks.
From this thread, there is an option which does not require installing any gclip/xclip/xsel third-party software.
A Perl script (since Perl is usually always installed)
use Win32::Clipboard;
print Win32::Clipboard::GetText();
In macOS, use pbpaste.
For example:
Update the clipboard
pbpaste | ruby -ne ' puts "\|" + $_.split( )[1..4].join("\|") ' | pbcopy
A way to paste from the clipboard to a file without any tools except echo.
Escape single quotes in the text you want to paste: replace all occurrences of ' with '\'' and copy the result to clipboard.
Type echo -n '
Press Shift + Insert
Type ' > filename.txt
Press Enter
Basically you're doing this:
echo -n 'copied "text" with '\''single quotes'\'' escaped' > filename.txt
It works even if the copied text has new lines.
For mac you can use this function which uses pbcopy and pbpaste, but a little easier:
Add this to your .bashrc or .zshrc:
clp() {
if [[ -z "$1" ]]
then
# No input - act as paste
pbpaste;
else
# Input exists - act as copy
echo "$1" | pbcopy;
fi
}
To copy use clp "Content" and to paste use clp
If you're like me and run on a Linux server without root privileges and there isn't any xclip or GPM you could workaround this issue by just using a temporary file. For example:
$ echo "Hello, World!" > ~/clip
$ echo `cat ~/clip`
Hello, World!

toggle between 2 tmux layouts

I often run Vim in a tmux session to I can run tests in an adjacent pane. On a smaller monitor, I either have to sacrifice more Vim screen real estate than I'd like, or make the test pane too small to read the full results (which is fine if everything passes, but not when there are failures and I need to see the details).
Then my workflow becomes:
trigger tests from within Vim
switch to test pane (last-pane)
zoom pane to occupy full window (resize-pane -Z)
read the results
restore original layout (resize-pane -Z)
switch back to Vim pane (last-pane)
I wanted to add a key binding that I could use when I'm in the Vim pane to zoom the test pane (hiding Vim), and be able to use the same binding once zoomed to restore the original layout, returning me to Vim. This is what I came up with, but I wonder if there's a better way I can do it. I had to set, check, and unset an environment variable to save the state that would support toggling back and forth with the same key binding. I also haven't figured out how to make the toggle state specific to a window (right now, any multi-window session shares the state across all its windows, so this doesn't work correctly)
bind Space if-shell '[ -z "${ALT_PANE_ZOOM+x}" ]' \
'select-pane -t :.+; resize-pane -Z; set-environment ALT_PANE_ZOOM 1' \
'set-environment -u ALT_PANE_ZOOM; last-pane'
Update:
I found a simpler solution. Rather than relying on a per-window environment variable, I can leverage -F and the window_zoomed_flag format variable:
bind Space if-shell -F '#{window_zoomed_flag}' \
'last-pane' \
'select-pane -t :.+; resize-pane -Z'
In your tmux.conf, create a keybind which:
Saves the zoom state
Switch to the last pane, unzooming if a pane was zoomed in
Conditionally zooms depending on the zoomed state in #1
-
bind key run-shell "tmux setenv zoomed $(tmux display -p '#{window_zoomed_flag}')"\; \
last-pane\; \
run-shell "test $(tmux display -p '#{zoomed}') -ne 0 || tmux resize-pane -Z"
Note that the backslash escapes on the semicolons command separators are required.

Does tmux support relative line-numbers?

Background.
I am a frequent vim-user, I just love the way you can navigate the buffers without ever having to reach for the mouse. I'm especially fond of the relative line numbers that let's me jump to specific lines with perfect accurecy, it just makes navigating that much faster. I also use tmux quite a bit since I often have a lot of stuff going on in my terminal.
The thing that bugs me the most though is when I use tmux copy-mode, it just takes forever to navigate to the line(s) you want to copy if you are using the arrow-keys, ctrl+p or k.
Searching for a unique keyword in the buffer is also not ideal but it might be faster if you already know what to search for. A lot of the time you make a search only to discover that the keyword you searched wasn't so unique after all and you didn't end up on the line you wished for anyway.
My question is this:
Does tmux support relative line-numbers?
..or line-numbers at all for that matter?
I can't find any information about this on the web. Nobody seems to be mentioning anything about this anywhere. Is there a better way?
Any other tips for ultra-speedy navigation in tmux copy-mode using the keyboard would also be very much appreciated.
tmux has a linenumber system in copy mode. however the first line is very bottom line.
In copy mode you can press : to go to line but there is no option to show linenumber. You can use some vim motions (key-mode was set as vi) in copy-mode, e.g. j k 20j 20k f F t T gg G 20G H L M ^ $ / ? ctrl-u ctrl-d w b ....
I think for copy a block of text, it is enough.. If you think you still cannot "ultra-speedy navigation", make a scenario, let's see how could we copy faster.
check man-page of tmux for details.
I found this tip. It will take you to your line with less keystrokes.
# super fast way to reach copy-mode and search upwards
bind-key / copy-mode \; send-key ?
This is a total hack but it works:
tmux split-window -h -l 3 -b "printf '\e[38;5;0m\e[48;5;226m' \
&& seq 200 1 \
&& echo -n 0 \
&& read" \
&& tmux select-pane -l
(newlines added for readablitiy)
To break this down:
tmux split-window -h -l 3 "command..." splits the pane -h horizontally (that is places a new pane next to the current one rather than above or below) with a -l width of 3 (you're unlikely to need more than 3 digits of line number... 0-999) to the -b left of the current pane and runs the command in it:
printf ... just sets the background colour to yellow and the foreground colour to black... You can omit this bit if you're not feeling fancy :)
seq 200 1 prints line numbers from 200 to 1 - extend if you have a tall screen!
echo -n 0 prints the 0 on the last line, because seq will print a trailing newline and we don't want that
read waits for you to press enter - this is how we block it from closing after the echo has completed
tmux select-pane -l jumps you back to focus on the pane you were working on
Select the pane and press enter to close it.
I would imagine that you can do something add a name for the new pane and create a keybinding for both opening and closing it from the pane you're actually trying to count line numbers on, but for now I'm just using the binding:
bind N split-window -h -l 3 -b "printf '\e[38;5;0m\e[48;5;226m' && seq 200 1 && echo -n 0 && read" \; select-pane -l

Keyboard script for pressing a key

I am looking for a simple linux program that will allow my keyboard to press a key for few seconds and then another one for few other seconds.
Thank you.
Check out xdotool, which lets you simulate keyboard input.
sudo apt-get install xdotool
#!/bin/bash
for i in `seq 0 9`;
do
xdotool key $i
done
UPDATE
To simulate a pressing a key for six seconds in Bash, then simulate pressing a different one for a further six seconds will be painful.
Therefore, I would use Ruby (you might have to install this from the repositories).
ruby -e 't = Time.new.to_i; key = "a"; while true do `xdotool key #{key}`; key = "b" if Time.new.to_i > t + 6; break if Time.new.to_i > t + 12; end'
If you run this from the command line it will simulate pressing key "a" for six seconds, after which it'll simulate a pressing key "b" for a further six.

How do I change bash history completion to complete what's already on the line?

I found a command a couple of months ago that made my bash history auto-complete on what's already on the line when pressing the up arrow:
$ vim fi
Press ↑
$ vim file.py
I'd like to set this up on my new computer, because it saves a lot of time when keeping a big history. The problem is that I can't for the life of me remember where it was mentioned and reading through endless bash references and tutorials unfortunately didn't help either.
Does anybody know the command?
Probably something like
# ~/.inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
or equivalently,
# ~/.bashrc
if [[ $- == *i* ]]
then
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
fi
(the if statement checks for interactive mode)
Normally, Up and Down are bound to the Readline functions previous-history and next-history respectively. I prefer to bind PgUp/PgDn to these functions, instead of displacing the normal operation of Up/Down.
# ~/.inputrc
"\e[5~": history-search-backward
"\e[6~": history-search-forward
After you modify ~/.inputrc, restart your shell or use Ctrl+X, Ctrl+R to tell it to re-read ~/.inputrc.
By the way, if you're looking for relevant documentation:
Bash uses The GNU Readline Library for the shell prompt and history.
Update .inputrc with the following:
"\C-[OA": history-search-backward
"\C-[[A": history-search-backward
"\C-[OB": history-search-forward
"\C-[[B": history-search-forward
If set enable-keypad on is in your ~/.inputrc as some st (suckless simple terminal) users might, be aware that the arrows keys are in keypad mode. Ubuntu ships with this useful /usr/share/doc/bash/inputrc.arrows:
# This file controls the behaviour of line input editing for
# programs that use the Gnu Readline library.
#
# Arrow keys in keypad mode
#
"\C-[OD" backward-char
"\C-[OC" forward-char
"\C-[OA" previous-history
"\C-[OB" next-history
#
# Arrow keys in ANSI mode
#
"\C-[[D" backward-char
"\C-[[C" forward-char
"\C-[[A" previous-history
"\C-[[B" next-history
#
# Arrow keys in 8 bit keypad mode
#
"\C-M-OD" backward-char
"\C-M-OC" forward-char
"\C-M-OA" previous-history
"\C-M-OB" next-history
#
# Arrow keys in 8 bit ANSI mode
#
"\C-M-[D" backward-char
"\C-M-[C" forward-char
"\C-M-[A" previous-history
"\C-M-[B" next-history
So I'm not sure if you'll need all, but it might not hurt to have in your ~/.inputrc:
# Arrow keys in keypad mode
"\C-[OA": history-search-backward
"\C-[OB": history-search-forward
"\C-[OC": forward-char
"\C-[OD": backward-char
# Arrow keys in ANSI mode
"\C-[[A": history-search-backward
"\C-[[B": history-search-forward
"\C-[[C": forward-char
"\C-[[D": backward-char
This is also on the same topic: My cursor keys do not work and also this xterm: special keys
With ohmyzsh, use this in your .zshrc :
bindkey '\e[A' history-search-backward
bindkey '\e[B' history-search-forward
To reload, source ~/.zshrc or relaunch terminal.
Source: https://superuser.com/a/418299/71680
You may need to enabled bash completion.
Check
/etc/profile
/etc/bash.bashrc
~/.bashrc
to see if any of the above files source /etc/bash_completion. i.e.
. /etc/bash_completion
If /etc/bash___completion is not sourced by any of the above files you will need to add it to one of them.
If you want all bash users on your machine to have bash completion, source /etc/bash_completion from /etc/bash.bashrc.
If it's just you who wants bash completion, source /etc/bash_completion from your ~/.bashrc.

Resources