Bash history search, partial + up-arrow

1

I have searched, but not found anything on this. I am looking for a functionality in bash, using a terminal.

Way back when, I had a user on a debian system, and a friend set me up with a convenient history search feature (I believe I used tcsh then), where I would type the beginning of a previous command, hit up-arrow, and it would do a search, based on the partial string.

E.g. if my history is:

./script.pl ./script.pl arg1 cat output cat output | grep yada

And I type ., and press up-arrow, it would show me: ./script.pl arg1. Press it again and it would show ./script.pl, etc.

Very much like it would perform a grep on .bash_history. Is there a way to get this...

0 0
2

I spend a lot of time at command line, and bash records the history of all the commands I've typed and puts it in .bash_history (there's a limit on how many it stores but for me it goes back a few days). I find it really useful to look back at the history to use the same commands again or edit them slightly. You can press the up arrow to go through your history but it can take a really long time to find what you're looking for. So instead, try Ctrl + r.

To do this: first press Ctrl + r, then start typing the command or any part of the command that you are looking for. You'll see an autocomplete of a past command at your prompt. If you keep typing, you'll get more specific options appear. You can also press Ctrl + r again as many times as you want to, this goes back in your history to the previous matching command each time.

Once you see a command you like, you can either run it by pressing return, or start editing it by pressing arrows or other movement keys. I find...

0 0
3

January 26, 2016

I recently switched back to bash shell from zsh and in doing so I lost zsh's history search. From your zsh prompt if you type in part of a command and then press the up arrow, you'll be shown the previous occurrence of that command. Repeated up arrows walk you through all previous occurrences. A very handy tool, and one I grew fond of.

Here's how to have this history search in bash.

First use the read command to learn what code is transmitted by the up or down arrow key press.

$ read ^[[A # up arrow ^[[B # down arrow

Control-c will return you to your prompt from the read builtin command.

Parsing the up and down arrow strings reveals that they both start with an escape character ^[ and then the key value itself: [A or [B.

The bash function to search history is history-search-backward or history-search-forward. So binding ^[[A to history-search-backward and ^[[B to history-search-forward emulates the arrow key behavior from...

0 0
4

If you have this in your

/etc/inputrc

or

~/.inputrc

, you will no longer have to hit the key twice to produce a list of all possible completions. A single will suffice. This setting is highly recommended.

# vi ~/.bash_profile
#!/bin/bash
source ~/.bashrc # get aliases

# vi ~/.bashrc
### alias
alias ls='ls --color=auto'
alias ll='ls -l'
alias h='history'

### ls with color
export CLICOLOR=1 # Use colors (if possible)
export LSCOLORS="ExGxFxdxCxDxDxBxBxExEx"

### display history command with date and time
export HISTTIMEFORMAT="%m/%d/%y %T "

### Prompt
PS1='\e[0;32m\u@\h \w #\e[m '

#######
# Note: on Ubuntu, xterm-256color may be in different place, try this:
# find /lib/terminfo /usr/share/terminfo -name "*256*"
# Note: tmux respects screen-256color
#######
#if [ -e /usr/share/terminfo/x/xterm-256color ]; then
# export TERM='xterm-256color'
...

0 0
5
# By default up/down are bound to previous-history # and next-history respectively. The following does the # same but gives the extra functionality where if you # type any text (or more accurately, if there is any text # between the start of the line and the cursor), # the subset of the history starting with that text # is searched (like 4dos for e.g.). # Note to get rid of a line just Ctrl-C "\e[B": history-search-forward "\e[A": history-search-backward $if Bash # F10 toggles mc on and off # Note Ctrl-o toggles panes on and off in mc "\e[21~": "mc\C-M" #do history expansion when space entered Space: magic-space $endif # Include system wide settings which are ignored # by default if one has their own .inputrc $include...
0 0
6

For quite some time I didn’t understand why the console in sbt, the scala built tool, is using up arrow for activating reverse-i-search (normally used in terminal with keyboard shortcut CTRL+R). This was one of those small bugs that are not annoying enough for you start searching for a solution straight away. After some time I got fed up and did some googling. Turns out it is not a bug within sbt, but a common side-effect caused by ~/.inputrc configuration.

Some time ago I had configured a sweet bash productivity feature that completes partial commands from history with the up arrow. I wrote about this and other bash productivity tips a few weeks ago.

Luckily this feature can be kept in bash, but disabled everywhere else. The inputrc fix provided was originally posted by Paul Phillips on sbt google group. Just wrap the conflicting part of inputrc with a simple conditional.

Best of both worlds. Up arrow still works its magic in bash and the standard way in sbt...

0 0
7

If you've used bash for a while you probably know that the commands you enter are saved in the file ~/.bash_history when you log out. Next time you log in, bash automatically loads these history commands from the saved file and you can then use the up and down arrow keys to browse your command history and find the command you want rather than re-entering it.

However, there are more effective ways to use bash's history: you can use Ctrl+R (Control key held down at the same time as the R key). This will display the following in your shell:

(reverse-i-search)`':

If you know type some substring found in the command you're searching for, for example "ls", bash will search for matching commands. For example, it might show:

(reverse-i-search)`ls': lsof -nP -p 3930

What it actually shows is going to be dependent on the commands you've previously entered.

When you do this, bash looks for the last command that you entered that contains the substring "ls", in...

0 0
8
...
0 0
9

When you are working at the command line, you often need to repeat or slightly modify previously entered commands. Most experienced users know they can browse the Bash history quite simply by pressing the up and down arrows at the command line, then pressing the left and right arrows to move the text cursor.

Fewer users know that you can also search the Bash history by pressing Ctrl+R and then typing; the shell automatically retrieves the first matching result. Continuing to press Ctrl+R jumps from match to match until you find the command you are seeking.

Bash keeps track of all the commands typed at the command line in the ~/.bash_history file, and you can view the file with the history command (Listing 1).

$ history 1 locate terminal | grep svg 2 xdg-mime query default inode/directory 3 joe /etc/gnome/defaults.list 4 ...

Although the Bash history and its search function deliver very useful results, the standard tools leave room for improvement. If you type...

0 0
10

If you actually need the output of the .bash_history file, replace history with
cat ~/.bash_history in all of the commands below.

If you actually want the commands without numbers in front, use this command instead of history:

history | cut -d' ' -f 4-

otherwise, there will be no difference (except if you're using a different shell).

Last 15 commands

You can use

history | tail -n 15

to get only the last 15 lines of your history with the last executed printed last (at the bottom).

Searching for a command

Alternatively, use

history | grep "apt-get" | tail -n 15

to get the last 15 commands which contained apt-get with the last executed printed last (at the bottom). You can replace apt-get with any command (or command argument) you want to search for (it can be a regular expression).

Scrolling through history

You can use

history | tac | less

to scroll through all the commands executed starting with...

0 0
11

The answers in the link that you provided from the Super-Users site shouldn't necessarily be viewed as 'workarounds' to the history command's default behavior. The bash shell has some sane, out of the box, default behavior.

I would highly recommend reading How can I avoid losing any history lines? for an explanation of what these modifications to history are doing. Additionally, there are some reasonable concerns to be aware of in regards to why this is not the default behavior of the history command.

performance - Since you are saving every command from every window with history -a, the .bash_history file can grow quite large and require greater resources to load the bash shell. This can result in longer start up times(for your terminal sessions, not overall system startup, per se.).

organization - (from the above article) "the history commands of simultaneous interactive shells (for a given user) will be intertwined. Therefore the history is not a...

0 0
12

Bash (Bourne Again SHell) is a Unix shell or command-line interpreter supported by a variety of operating systems. You can use the Bash interface to directly access the ACI switches or you can develop Bash shell scripts to automate tasks. Bash provides a variety of command line and scripting features.

For more information about the Bash shell , see http:/ / www.gnu.org/ software/ bash/ bash.html.

Network Naming Conventions

Bash uses a forward slash (/) as a separator for interfaces, network addresses, and other settings.

For example, Ethernet interface number 46 on switch module 1 is represented as Eth1/46.

Command Completion

Bash provides tab completion for standard Linux commands and ACI switch-specific commands. When you press the Tab key at the end of a command or option abbreviation, Bash displays the command in full or the next available keyword or argument choice. Bash will also autocomplete...

0 0
13
...
0 0
14

Your Source for Oracle Database News

*************************************************
* Included Tips Page starts here
*************************************************

Upcoming News

Copying Oracle Datafiles Between Linux and Windows Servers

Oracle Adding Dates

When Oracle Storage Parameters are Ignored

Estimate Size of Last RMAN Backup

Oracle Best Fastest Way to Delete Data from Large Table Tips

Detecting Misspelled Row Column Data in Oracle

Name Validation Using Oracle Regular Expression Syntax

Find Numeric Data in Oracle Character Column

ORA 00020 maximum number of processes exceeded

Building a Dynamic SQL Statement

Integrating HP Data Protector with Oracle

ALTER SYSTEM SWITCH LOGFILE vs ALTER SYSTEM ARCHIVELOG CURRENT

Find All References to an Oracle Table

Fastest Way to Rebuild Oracle...

0 0