I'd like to know if it is possible to make bash auto-completion highlight the matched part of the auto-complete suggestion.
For example,
I am have directories with files such as these:
LSFJOB_647169535/ LSFJOB_647158534/
In this case, if I type LSF and hit Tab, then I get:
LSFJOB_6471
But then I have to focus hard to get which character should I type next.
I'd like bash to suggest me something like:
LSFJOB_647169535/ LSFJOB_647158534/
or
LSFJOB_647169535/ LSFJOB_647158534/
Do you know a way of doing it?
That is a good question!
Dennis Williamson already answered it there (SuperUser).
So it turns out that there is a "ReadLine Variable" that does exactly that: colored-completion-prefix.
Sadly it's only available in Bash v4.4 :c Link to the diff
You can check its value with bind -v|grep color
I tried to play with compgen but it appears that it strips colors away /:
Instead of colored-completion-prefix, which requires Bash 4.4, you could add the older (Bash 4.0)
set completion-prefix-display-length 2
to your ~/.inputrc (see manual). This replaces any common prefix longer than 2 characters with an ellipsis when showing the completions:
$ ls
LSFJOB_647158534 LSFJOB_647169535
$ cd LSFJOB_6471<tab>
...58534/ ...69535/
Related
There seems to be quite a lot of information on how to edit and execute a command using your editor using "edit-and-execute-command (C-x C-e)", but what I would like to achieve is take the current shell command, apply certain filtering (using a script) and then return it to prompt for further approval/manual changes before execution. Is this possible with bash?
Latest update based on my experience
The part 0"+y$dd in the following mapping is really something that you should carefully think about and tailor it to your taste/workflow/experience.
For instance, very frequently I've found myself ending up with multiple lines in the buffer, where I only want to execute the one the cursor is on; in this case I can use 0"+y$dd:%d<CR> instead of 0"+y$dd.
And this is just one of the possible scenarios.
Final answer for those who like vim
Set vim as your EDITOR/VISUAL, so that when editing a command line, you will use vim to edit it.
Put au BufEnter /tmp/bash-fc.* nn <Leader>d 0"+y$dd:wq<CR> in your ~/.vimrc file to map Leaderd (which you will rarely use when editing a command) to the action "delete the current line into the + register without the trailing EOL".
you can use either the + or the * register in the mapping above; the ways to paste into the terminal will likely differ; you need the +clipboard option for these registers to be available.
When finished editing a command in the vim editor, hit EscapeLeaderd.
Paste the clipboard into the terminal (this is terminal-dependent).
Original answer
I often need to do the same, and I do it as follows. (I normally use the set -o vi in bash, so points 1 and 2 in the following are different if you use set -o emacs, the default; based on your question it looks like points 1 and 2 are unified in Ctrl+x followed by Ctrl+e, which is harder to type, imho.)
hit Escape to be in normal mode,
hit v to enter the editor to edit the command,
edit the command as I like,
(This is where you ask the question.)
hit Escape0"+y$dd:wq,
Note: 0"+y$, not simply "+yy, as the latter would copy the newline too, and this would result in executing the command upon pasting it in the command line,
paste the clipboard on the command line
how to do this depends on the terminal you are using, I guess; I hit Ctrl+Alt+v in URxvt.
proceed to approval/manual edit.
Clearly this is just a workaround, consisting in copying the edited command into the clipboard before deleting the whole command, so that nothing gets executed upon exiting the editor; however it's the best I can get for myself.
Update
As my EDITOR (and VISUAL) is equal to vim, when I edit the command, I edit it in vim.
In this respect, I have noticed that the buffer is named /tmp/bash-fc.random, where random is a 6-characters alphanumeric random string.
This gives space to a lot of possiblities, if you use vim as your editor, as you can define some mapping in your .vimrc to execute the whole sequence Escape0"+y$dd:wq. For instance, one command that you'd rarely use when editing a command line is Leaderd; therefore you can put the following mapping in your .vimrc file
au BufEnter /tmp/bash-fc.* nn <Leader>d 0"+y$dd:wq<CR>
so that step 4 in the above recipe becomes
hit EscapeLeaderd
It's not possible to do that in Bash/readline but it's possible in zsh
using edit-command-line command:
darkstar% autoload edit-command-line; zle -N edit-command-line
darkstar% bindkey "^X^E" edit-command-line
Now press Control-x Control-e to open your editor, edit line, leave the editor - you will see the updated command line but it will not be executed automatically.
Now that I think about it, maybe a variation of what #kenorb suggested in a comment is the best workaround (as it seems no solution exists), if we want to stick to bash.
What you can do is prepend a # (the comment character in bash) to the command, rather than echo. Then when you exit the editor, the command will be ineffective, and you will only have to press arrow up (or k, if you use set -o vi), remove the # and confirming.
Note that this strategy adds just a few keystrokes, so it can be fairly efficient, depending on your typing level.
These pieces might get you closer:
a) replace the the normal binding for newline newline (ctrl-M)
bind -x '"\C-M":date"
b) grab the current line from the history using !#
replace date with whatever script you want.
c) edit manually
d) if necessary, somehow tie in !!:p which prints the new command to the command line but does not execute it, thus letting you manually edit it.
e) using ctrl-J submit edited command rather than a newline
or they might not ....
There is an option in bash to modify command from history without executing it. I'm not sure it it's possible to use script for this, doesn't seem to be likely. Although, you can make modifications using history modifiers.
Enable option histverify to prevent execution of modified command
Use chain of modifiers to change last command
Use "!!" to put your result to command line for final edit
Here is how it looks:
$ shopt -s histverify
$ ls *.sh
script1.sh script2.sh script3.sh script-return.sh
$ !!:s/*/script1/:p
ls script1.sh
$ !!:s/1/2/:p
ls script2.sh
$ !!
$ ls script2.sh
script2.sh
I'd like to point you to the Composure framework for Bash (I'm not affiliated with it): https://github.com/erichs/composure
It provides draft and revise functions that sound like they could help with what you're trying to do. Here's a (long) quote from the project's readme file:
Composure helps by letting you quickly draft simple shell functions,
breaking down your long pipe filters and complex commands into
readable and reusable chunks.
Draft first, ask questions later
Once you've crafted your gem of a command, don't throw it away! Use
draft () and give it a good name. This stores your last command as a
function you can reuse later. Think of it like a rough draft.
$ cat servers.txt
bashful: up
doc: down
up-arrow
$ cat servers.txt | grep down
doc: down
$ draft finddown
$ finddown | mail -s "down server(s)" admin#here.com
Revise, revise, revise!
Now that you've got a minimal shell function, you may want to make it
better through refactoring and revision. Use the revise () command
to revise your shell function in your favorite editor.
generalize functions with input parameters
add or remove functionality
add supporting metadata for documentation
$ revise finddown
finddown ()
{
about finds servers marked 'down' in text file
group admin
cat $1 | grep down
}
$ finddown servers.txt
doc: down
It does not seem possible with a keyboard shortcut, at least:
$ bind -P | grep -e command -e edit
complete-command can be found on "\e!".
edit-and-execute-command can be found on "\C-x\C-e".
emacs-editing-mode is not bound to any keys
possible-command-completions can be found on "\C-x!".
vi-editing-mode is not bound to any keys
This can be done in native bash using readline specifically READLINE_LINE and READLINE_POINT variables. I use this functionality all the time though not through vim, you would need to get the value of $selected from your vim command and if not empty it takes your original line + your input and replaces your original line with the combination without executing. output as a variable
_main() {
selected="$(__coms_select__ "$#")"
origonal_text=$READLINE_LINE READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
}
bind -m emacs-standard -x '"\C-e": _main '
bind -m vi-command -x '"\C-e": _main '
bind -m vi-insert -x '"\C-e": _main '
Edit
Just remembered these two utilities that will let you do this as well.
Vipe allows you to run your editor in the middle of a unix pipeline and edit the data that is being piped between programs.
vp, up, vipe, Neomux (upgrade of nvim terminal) you can do some pretty neat throwing buffers between the terminal and split window.
and Athame (full vim on the command line)
https://github.com/ardagnir/athame
careful with that one though plugins work on the cli and it can get funky if you got tons of plugins
this question seems hard to search answers by google.
I executed:
man cp > cp.txt
vim cp.txt
there are many "^H" in the file, it maybe backspace, how to get rid of them?
Just like our grand parents with their typewriters, man uses "backspace" (^H) to to go over the previous character and type it again to obtain "bold" letters.
While you can get rid of those ^H with a simple :%s/<C-v><C-h>//g you will get many doubled characters:
"original
N^HNA^HAM^HME^HE
"result
NNAAMMEE
So you would need to include the character just before or just after the ^H:
:%s/<C-v><C-h>.//g
But why go through all that trouble when you can obtain a clean text file directly with:
$ man cp | col -b > cp.txt
Those special control characters are probably ANSI escape sequences, used to add styling and color to the text. Vim doesn't parse those by default, but the AnsiEsc plugin uses Vim's conceal and syntax highlighting features to draw the text as intended.
Vim ships with a Man page reader. Add the following to your ~/.vimrc:
runtime ftplugin/man.vim
Now you can do :Man cp to open a man page on cp.
See :h :Man for more help.
On of the commands that I wrote the most on daily basis on my console is
# history | grep -i 'something'
That and ctr+r is probably what I use the most :).
I was thinking in have something like
# h something
Or even better, a live search like ctr+r but that shows all results at glance, not only one. I know i can cycle pressing ctrl+r again, but would be better if I could see what are all the elements that I"m cycling.
So this is 2 questions:
1) Do you know any program that provide a better interface for bash history in console?
2) What is the best way to accomplish my h something alias?
Want a h command. Easy, add an alias in your .bashrc or .bash_profile or .bash_aliases (depending on your config).
alias h="history | grep -i"
Perhaps you may want to try https://github.com/dvorka/hstr which is "suggest box style" filtering of Bash history - you get hh 'something' - for instance hh an:
It can be easily bound to Ctrl-r and/or Ctrl-s
One way to acomplish the h something is adding to .bash_profile:
alias h="history | grep -i "
I'm actually using zsh but i guess it will work too
In my .bash_profile is set the following in there:
set -o vi
then I have the functionality of vi from the command line
Hitting the ESCape key followed by k will return the last line entered
ESC-k
keep hitting k so that it scrolls up. The magic happens when you then hit the slash key (/). Then you can search your history
http://www.thegeekstuff.com/2009/10/do-you-like-to-perform-vi-style-editing-in-bash-command-line/
I've done something to break my Bash Shell Prompt in OS X (10.5.7) Terminal.
This is the PS1 that I had configured:
PS1='\[\e[1;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\$ '
As far as I can tell I have the color commands escaping correctly. However when I scroll up and down in my command history I often get line wrapping issues if the historic commands wrap onto multiple lines.
I simplified my prompts to the following:
PS1='\[\e[1m\]\h:\w\$ \[\e[0m\]'
PS2='> '
And I still see something like:
localhost:~/Library/Application Support/Firefox/Profiles/knpmxpup.Defau
lt/extensions/{1A2D0EC4-75F5-4c91-89C4-3656F6E44B68}$ expocd \{1A2D0EC4-7
5F5-4c91-89C4-3656F6E export PS1="\[
\e[1;32m\]\h\[\e[0m\]: cd Library/Appl
ication\ Support/
I've also tried \033 instead of \e. I just included PS2 up there for information, I haven't changed that from the install default. If I completely remove the color codes then everything works fine, any ideas?
I am now using this PS1 with good effect:
green=$(tput setaf 2)
blue=$(tput setaf 4)
bold=$(tput bold)
reset=$(tput sgr0)
PS1="\[$green$bold\]\h\[$reset\]:\[$blue$bold\]\w\[$reset\]\$ "
Scrolling through my command history appears to handle line wraps now. However in the meantime since this question was asked I have also updated my OS X to 10.6.3
This stackoverflow thread seems relevant. As someone noted in that thread, the Bash FAQ at mywiki.wooledge.org discusses how to properly quote color codes in Bash prompts (FAQ 53), and the proper invocation of terminal colors (FAQ 37).
Line wrapping issues in Bash are nothing new. Your PS1 should work as is but there is a bug in Bash 3.2.49. Consult the mailing list, there's yet another bug regarding this which was confirmed to be fixed in 4.0.
You can't do much more than tagging unprintable characters with \[ and \], the rest must be done by the prompting code.
It seems that you have correctly escaped and enclosed sequences.
A workaround I use anyway it it to add a '\n' at the end. I find it clearer and lessen any problem with wrapping issues. The exact end of my PS1 is :
'\n\[\033[0;30m\]$\[\033[0m\]
An excellent howto you probably know :
Bash prompt howto
I noticed that there are some issues with the prompt cursor positioning even if there are no special character in the PS1 or PROMPT environment variable.
If we output a file that does not have a end-of-line char at the end. It will confuse the prompt.
You can reproduce this by doing:
curl https://gist.githubusercontent.com/martinos/d4aa0a7d4d752b0d0d9f/raw/3198c39f84a080c44227a084a19fb3a0bb661ee5/wrapping_issue.txt
and pressing the up key multiple times and you will see that the prompt get confused.
You can see an example of this in action:
https://asciinema.org/a/9mtjhi9dib6md4ocsbw210cca
When this occurs, just press <CTRL-C> and the prompt will come back to normal.
Note that ZShell does not have this issue.
For future reference, this is what I use:
export PS1="\[\033[0;31m\][\u#Project:\w]$\[\033[0m\] "
This would display my shell prompt as:
[ec2-user#Project:~]$
Helps me distinguish between live and dev sites.
Here's mine: it's the best one I've found, but the site where I originally found it was missing an escape character, leading to the line wrapping issue. I tinkered with it and finally got it working. It shows your user, path, and branch info with good contrast, color-wise.
export PS1='\[\e[1;37m\]\[\e[1;32m\]\u\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(__git_ps1 " (%s)")\[\e[0;39m\] \[\e[1;37m\]|\[\e[0;39m\]\$'
Also, add
GIT_PS1_SHOWDIRTYSTATE=true
To show a marker when a branch is "dirty" (changes to be committed exist)
export HISTCONTROL=ignoredups
Is also useful to ignore duplicates when scrolling up through bash history.
bind "set completion-ignore-case on"
Helps too.
Lastly,
shopt -s checkwinsize
May be helpful on OSX if issues persist.
'shopt -s checkwinsize' also works for Cygwin wrap problems also
If you're using the title bar trick "\e]2;titlebar\a", make sure to escape that too: "\[\e]2;titlebar\a\]"
In my current situation, it is not unusual for me to have several UNIX computers I connect to, as several different users depending on the situation, and to traverse through various directories on the machines doing things. I use ksh through the entire thing.
I was fiddling with my prompt recently, and I was able to get it to change some colors depending on my current username and current server. However, what I would also want is for it to change colors based on my current directory. For example, if I were in directory "foo", the prompt should be yellow, but if I were in directory "bar", the prompt would be magenta. In both cases, subdirectories should also count, so a simple substring check should be enough.
The problem I ran into, however, is that when I run my .profile script, it properly colors the directory--but it no longer dynamically updates whenever I switch to another directory--and I'm not sure how before I did all the branching, I was able to get it to print my current working directory correctly even after I switched directories.
I did some googling, and find information for bash, but ksh seems to be largely ignored. As I cannot figure out how to do this on my own, I must bring it to the Stack Overflow community, to add it to future knowledge. Thus, with my long-winded explanation, the "quick version" of my question is as follows:
In ksh, how can I set up my prompt to display the current working directory and color the text based on where the current working directory is? Is it even possible?
Why not using zsh? It is based on ksh, and it is much more powerful. In zsh you can write chpwd function that is implicitly called every time you change directory. In this function you can check your current directory and set PS1 to whatever you want.
Alternatively (even in ksh) you can create an alias for cd command:
change_my_ps() {
PS1=...
}
better_cd() {
builtin cd "$#"
change_my_ps
}
alias cd=better_cd
Something like this. I'm not sure it is proper, I don't remember ksh syntax.
I was able to dig up a semi-solution here:
http://books.google.com/books?id=QYu_v2R6fIQC&pg=PA71&lpg=PA71&dq=korn+dynamic+prompt&source=bl&ots=yMEZiWrGyU&sig=8KBbs12Mtk3eGNSZQiLVmFYZVFY&hl=en&ei=2HX4Sej0K6LWMP2NxakP&sa=X&oi=book_result&ct=result&resnum=2#PPA72,M1
Though enacting it is still difficult.
To display the current directory in ksh, put this in your .profile file:
export PS1="\$PWD "
That will dynamically update when you change directory without mucking around with functions.
Since nobody actually answered the part about the color, here's how I'd do it. I can't color the text, so I've also included a screenshot of my terminal.
See Wikipedia's page on ANSI escape codes for the full list of color codes and print --man and printf --man for the details of printing escape sequences in ksh.
(Note that ksh93v, currently in beta, will contain a prompt language based (I think) on bash's, which will make this sort of thing somewhat easier, though I think you'll probably still need to use something like this function to do complex conditional prompts like this one.)
> cat ~/scripts/prompt
function prompt
{
set -eu
typeset c=
case $PWD in
*/foo) c=3;; # yellow
*/bar) c=5;; # magenta
esac
print "\E[3${c}m$PWD\E[m > "
}
PS1='`prompt`'
> . ~/scripts/prompt
/Users/adavies > cd foo
/Users/adavies/foo > cd ../bar
/Users/adavies/bar >
I use this:
function chdir
{
cd "$#"
CWDH=${PWD%/*}
PS1="($_time)$hname:${CWDH##*/}/${PWD##*/} ->"
export PS1
}
alias cd=chdir
chdir .
Ignore the time and hname, but the rest should work for you. Changing colors is going to be terminal dependent. You need to know the escape codes for each color for the terminal you will be using. If you know you only ever use an xterm, it will be easier.