To bind clear to ^l in Bash - bash

I would like to have Ctrl-l for clear in my Bash.
How can you bind Ctrl-l to clear in Bash?

Put this in your ~/.inputrc:
C-L: backward-kill-line
(assuming by "clear" you mean "clear current input line"; if you mean "clear screen" then put clear-screen instead of backward-kill-line).

in a particular case where the clear-screen didn't work for me either, I found out that putting in ~/.bashrc the line: bind -x $'"\C-l":clear;' was better than "\C-l":'clear\n' in ~/.inputrc because it cleared the screen and left the currently typed command in place; for example (^L show where I hit the combo):
With "\C-l": clear-screen in ~/.inputrc:
user#darkstar:~$ date^L
user#darkstar:~$ date
user#darkstar:~$ ^L
user#darkstar:~$
With "\C-l":'clear\n' in ~/.inputrc:
user#darkstar:~$ date^L
-bash: dateclear: command not found
user#darkstar:~$ ^L
# screen effectively redrawn
With bind -x $'"\C-l":clear;' in ~/.bashrc:
user#darkstar:~$ date^L
# screen redrawn and the top line is now:
user#darkstar:~$ date
And for now I have not been able to get the same result as bind -x using only the inputrc file...
Edit
I found that in some cases where clear-screen wasn't working for me were caused by my attempts to get more colors in the CLI.
For example I had the issue with TERM=xterm-256color (or screen-256color, etc.) and removing the -256color part solved the problem.
I have not yet found a way to get a 256 colors term working along CTRL+l (in xterm, urxvt, etc).

Related

For further research: terminology about manipulating command before hitting RET in terminal

I just found out in zsh if I have a variable, namely abc="cba", and if I type echo ${!abc} and pressing enter, it doesnt go as command but instead open new prompt below with expanded variable echo ${abc="cba"}.
And probably included that powerful zsh plugin that let us automatically insert sudo just by double tapping Esc.
So for further studying and probably opening new possibilities, what is the right terminology about that typed-command-but-not-entered manipulation?
Thanks...
Those seem to be unrelated. The first is zsh asking you to verify history expansion. The second is a ZLE (Zsh Line Editor) widget with a keybinding to modify the current command line. You can see what this ZLE widget looks like in oh-my-zsh the source code.

zsh-like history in bash

I really like zsh's history autocomplete feature. Namely, when I press up, I get the previous command. When I type emacs and press up, I get the last emacs command I used. When I type git and press up, I get the last git command I used. When I try to do this in bash, it just goes to the last generic command I used. Essentially, I want to be able to half-type a command and press up to get the last command I used that matches what I've typed so far. I don't know how to achieve this in bash. I can't use zsh on this system, so is it possible to replicate this functionality in bash?
The up arrow is bound to the previous-history command. You want to rebind it to history-search-backward (which is unbound by default) instead. You can check which keys previous-history is currently bound to:
$ bind -p | grep previous-history
"\C-p": previous-history
"\eOA": previous-history
"\e[A": previous-history
In my case, the last two both represent up arrow (the exact escape sequence may differ from terminal to terminal, or depending on what mode the terminal is in, but these two are fairly standard). Especially since previous-history will still be available with Control-P, it's safe to change the behavior of the up arrow.
Add this to your .inputrc file (creating the file if necessary):
"\e[A": history-search-backward
"\eOA": history-previous-history
Or, you can add call bind from your .bashrc:
bind '"\e[A": history-search-backward'
bind '"\eOA": history-previous-history'
You may also want to similarly bind history-search-forward to the down arrow key, \e[B and \eOB.
You can search your history with ctrl+R.
When you first press it, the prompt will change and you will be able to enter the characters you want to search for in the history. The new prompt disregards characters that were entered in the previous prompt and will overwrite them when something from the history is matched.
It will display the last command that matches your input and you can press ctrl+R again to navigate the results from the latest to the earliest.
When you've found the entry you're interested in, you can either press Enter to execute it or the left or right arrows to return to the standard prompt to edit the command line. Pressing the up or down arrows will return to the standard prompt but navigate the history one step forward or backward, which I find more confusing than anything.

history-limit is not working after C-l in tmux

My environment is Fedora20 (32-bit) + Yakuake + fish + tmux.
Months ago, I started to use tmux, it is a great terminal multiplexer, but the scrollback (Use prefix then PageUp to scroll back) is not working as expected, the history limit is always under 2000 (around 1980 on the right above corner) even if I already set the scrollback limit to "unlimited scrollback" in Yakuake Setting.
Finally I could take it anymore and started to goole it for solution, found out that I could put set -g history-limit 30000 int my tmux.conf file, then check again, it worked, but I realized that every time I typed C-l to clear the screen(and I use it a lot), the scrollback history will be only under/around 30.
Then I found out at How do i clear tmux screen while tailing logs? that I could put bind l send-keys -R into .tmux.conf and use prefix-l to clear the terminal history buffer temporarily and still be able to scroll back, it worked just as I wanted. But What I really want is I use only C-l to do the trick without the prefix key, replacing the shell built-in C-l to do it...
Update:
Short ask, after setting a bind in .tmux.conf, in tmux, you have to type the prefix key before typing that key, then how to bind a key without the need to add the prefix key by default??
In order to bind a key without the need to add the prefix, use the -n flag for the tmux bind command.
Here's the example that should be put in tmux.conf:
bind -n C-l display-message 'foo'
The example binding is triggered when Ctrl-l is pressed (no prefix) and it displays message 'foo' in the tmux prompt.
All that said, be careful & conservative with setting these "direct" key bindings because the key will become unusable for any other terminal program.

Why am I seeing only 8 colors in terminal (xfce-terminal)?

I'm running Xubuntu 13.04 and I want to use Vim as my default editor for everything. I have downloaded many vim color schemas and tried them out, but all of them don't look like the official screenshot.
For example, vim's own color schema - desert should look like this:
But in my vim, many colors won't display, for example the background.
So this means a fighting with xfce's Terminal and I can't force it to use 256 colors.
the command tput colors gives me
8.
At the same time the code for ((x=0; x<=255; x++));do echo -e "${x}:\033[48;5;${x}mcolor\033[000m";done shows me nice colors. it seems i missed something. If I run
**$ echo $TERM**
I get xterm. It should be 'xterm-256color'
When I try
set term=xterm-256color
and
export TERM=xterm-256color
Then: echo $TERM
I get the message
xterm-256color.
But after signout/signup, I'm still not getting the right colors in Vim. And I see the Xterm is changed to xterm again.
I added:
if $TERM == "xterm-256color" set t_Co=256 endif
and
t_Co=256
to my .vimrc file and it didn't seem to help. Then I customized the xterm entries; added this to ~/.Xdefaults:
*customization: -color
XTerm*termName: xterm-256color
Add this to ~/.xsession to apply to new terminals:
if [ -f $HOME/.Xdefaults ]; then
xrdb -merge $HOME/.Xdefaults
fi
When I changed in preferences of terminal, emulate terminal environment, the 'xterm' to 'xterm-256color'
I get the message:
'*** VTE ***: Failed to load terminal capabilities from '/etc/termcap'
When I check /usr/share/vte/termcap/xterm, the file xterm-256color is missing. Same in folder xterm0.0. I tried to find this file on internet to download and put in the folder, but I couldn't find it.
This is driving me crazy the whole day... Have anyone suggestions?
Quick (Temporary) Way
Enter this whenever you open a new terminal:
export TERM=xterm-256color
Works for as long as the window is open.
Works-but-dirty Way
Append the line above to ~/.bashrc.
The problem with that, though, is that editing $TERM in .bashrc is a bad idea because doing that automatically makes any terminal using bash try to use it regardless of whether it actually supports 256 colors or not (like when SSH-ing or accessing the terminals with Ctrl+Alt+F1 to F6).
What I did, though is that since xfce4-terminal sets the $COLORTERM value to xfce4-terminal, I, instead appended the following to .bashrc:
if [ "$COLORTERM" == "xfce4-terminal" ] ; then
export TERM=xterm-256color
fi
That way, the relevant $TERM edit only happens if you're using xfce4-terminal, which just sets it to xterm anyway (and changing the emulation environment results in that "VTE" message).
References:
https://bbs.archlinux.org/viewtopic.php?id=175581
http://vim.wikia.com/wiki/256_colors_in_vim#Comments
http://promberger.info/linux/2008/04/05/getting-256-color-support-working-for-mutt-in-xfce4-terminal/
http://docs.xfce.org/apps/terminal/getting-started

How can I make bash tab completion behave like vim tab completion and cycle through matching matches?

I've been meaning to find a solution for this for YEARS.
I am sooo much more productive in vim when manipulating files than bash for this reason.
If I have
file_12390983421
file_12391983421
file_12340983421
file_12390986421
In bash and type file_1->tab , it obviously lists:
file_12390983421 file_12391983421 file_12340983421 file_12390986421
And this is horrible and painful to work with.
The same sequence in vim will loop through the files one at a time.
Please someone tell me how to do this in bash, or if there is another shell that can do this, I'll switch tomorrow.
By default TAB is bound to the complete readline command. Your desired behavior would be menu-complete instead. You can change your readlines settings by editing ~/.inputrc. To rebind TAB, add this line:
TAB: menu-complete
For more details see the READLINE section in man bash.
For bash >= 4 you might like these settings. You can try them directly on the command-line, and put them in your ~/.bash_profile if you like them.
# If there are multiple matches for completion, Tab should cycle through them
bind 'TAB:menu-complete'
# And Shift-Tab should cycle backwards
bind '"\e[Z": menu-complete-backward'
# Display a list of the matching files
bind "set show-all-if-ambiguous on"
# Perform partial (common) completion on the first Tab press, only start
# cycling full results on the second Tab press (from bash version 5)
bind "set menu-complete-display-prefix on"
This setup is similar to Vim's set wildmode=longest:full:list,full
I pulled these settings from this question on the Unix & Linux site.
By the way, since you are here, here are some other great bindings:
# Cycle through history based on characters already typed on the line
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
# Keep Ctrl-Left and Ctrl-Right working when the above are used
bind '"\e[1;5C":forward-word'
bind '"\e[1;5D":backward-word'
This means if you type ssh<Up> it will cycle through previous lines where you ran ssh
If you don't like what you got, you can clear the line with Ctrl-K Ctrl-U
I pulled these settings from this question on AskUbuntu.
On top of
# cycle forward
Control-k: menu-complete
# cycle backward
Control-j: menu-complete-backward
you may also consider adding
# display one column with matches
set completion-display-width 1
This way you would preserve the current Tab functionality and make bash display the possibilities in one column. So instead of
file_12340983421 file_12390983421 file_12390986421 file_12391983421
you would get
file_12340983421
file_12390983421
file_12390986421
file_12391983421
P.S. You can get up to date readline library from this The GNU Readline Library website.
Thanks to #sth I found what works best for me:
To keep normal bash tab completion, and then use ctl-f to cycle through when needed using menu-complete
put this in your .inputrc file:
"\C-f": menu-complete
In my experience, the solution provided in sth's answer has never completely worked for me. TL;DR: Add set -o vi to your ~/.bashrc.
When using menu-complete in conjunction with vi keybindings, I have to make sure that my ~/.bashrc has:
set -o vi
It's never been enough for my ~/.inputrc just to have:
TAB: menu-complete
set editing-mode vi
set keymap vi
My guess is that somehow set editing-mode and set keymap are clobbering the TAB: ... setting, but I haven't looked into the documentation thoroughly to figure out why this is the case.

Resources