A command to run the command that was executed right before the most recent one? - linux

In bash, to run the most recent command you used, you can either press the up arrow key on your keyboard and press Enter or type !! and press Enter. But, is there a command that you can use to run the command that was executed right before the most recent one? For example:
$ pwd
/home/john
$ ls
bin Documents Music Pictures Templates
Desktop Downloads output Public Videos
Typing in !! and pressing Enter will run the ls command again. But, is there something similar to the !! command that will let you run the pwd command in the same manner? If there is one, give some more details as to how it works.

You can use !n, where n is line number in your history. To get the second to last command you can then use !-2.

You can use !-2 like is mentioned here. Along with the up arrow, you can cycle back through your history with ctrl-p as well, forward with ctrl-n. You can also use ctrl-r and do reverse search for a command you've already entered.

Did you remember the beginning characters of last executed commands,
you can execute it by specify the beginning characters of the command with !
from your question, to execute ls again, use the !l,
and !p or !pw to execute pwd command.but it only execute the last executed one.

Related

Vim run shell and see output

I'm trying to run an unsaved buffer in a shell command and have a history of the output
console.log("test")
Then, :w !node does what is expected, test is outputted, but once I click enter, the output disappears, and doesn't even show in :messages:
How can I find it?
If you open a Vim buffer and type in one or more shell commands each on "his" own line. Like:
ls /home
ls /root
Then you can type the command:
:%!bash
and not only will it run the commands on each line one after the other, it will also overwrite the buffer with the output of each command in chronological order, then you can do whatever you want with it :-)
I hope it was helpful :)
BTW: if you want to run a command in Vim and get the output in your buffer, then you can just double tap the exclamation mark in NORMAL MODE and the command line in the bottom will show
:.!
then you just type in your command and press enter. :)
easy peachy lemon squeeze :)
note: I learned that last one by mistake
What :w !cmd does it pipes your current buffer contents into cmd's stdin. When you don't need this you should simply execute :!cmd instead.
:messages serve to show an important log data printed by specially dedicated :echom[sg] command, not some random stuff from terminal windows. So the output from "node" process will not and should not ever get there.
You can put the process' stdout into the current buffer by using :r[ead] command, e.g.
:r !node

How to clear previous commands before showing the result of :!command on VIM?

For example, if you use the following command on VIM:
:!ls
It will show the contents of the folder (as expected) plus the result of your previous commands. I want it to show only the last result. How?
One way:
:!clear && ls

Hooking bash commands?

I'm tired of doing long cd commands to other directories, so I want to make a little tool for jumping to the most recent folders.
I've searched and haven't found any sort of API that would let me trigger a process when a cd command is run. Can somebody point me in the right direction?
Working off the bash_history seems inefficient, and isn't always enabled.
There is a classic bash script that makes easier directory navigation: http://linuxgazette.net/109/marinov.html
Take a look at pushd and popd.
If you're using a fairly recent version of bash you can just Ctrl+R and type a few letters to get the history. So, if you press Ctrl+R and type cd you'll get your last cd command. Press Ctrl+R again and you're get second last cd command and so on.
And yes, cd - takes you to your last working directory.
You can also find your last working directory in shell variable $OLDPWD

Alternative to Up Arrow + Enter to run previous command?

Sometimes I have to run a command many times in succession, for example to see if a service has started, and it becomes tedious to move my hands away from my normal typing position to press the Up Arrow and Enter keys repeatedly. Is there a way to run the previous command without the Up Arrow and Enter keys, perhaps with an elaborate shell script?
I've tried the following, but it is unsatisfactory because it cannot execute aliases, and it is a little slow.
history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt
sleep .01
chmod 777 prev_command.txt
eval prev_command.txt
rm prev_command.txt
Ideally I'd have an alias to this script so I can type in something like "prev" in the command line and hit Enter to run the previous command again.
In bash, you can press ctrlp to go to the previous command -- that's a lot better than having to move to the arrow keys.
See also: https://github.com/fliptheweb/bash-shortcuts-cheat-sheet/
Use
!!
to run your previous command.
sudo !!
also works , for the record.
Instead of running the same command many times in succession, why not watch it instead? watch will run a specified command repeatedly and display the output in stdout so you can see it change over time.
watchcommand
I often use the "history expansion" feature in bash (usually activated with cntlR) -- it interactively searches through your history for the previous closest match.
See the bash manual section Searching for Commands in the History, and also Using History Interactively.
Are you an emacs or vi user? You can use
set -o vi
set -o emacs
to set emacs or vi keybindings. You can then use the emacs or vi key bindings in bash. I don't know if this should work for other shells. I believe the vi mode starts in insert mode, so you need to hit esc to enter command mode. In emacs mode (the default), you can use ctrl+p and then ctrl+j to move to the previous line and do a carriage return.
Otherwise, you can use !! as someone else suggested.
In bash:
$ help fc
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
Display or execute commands from the history list.
fc is used to list or edit and re-execute commands from the history list.
FIRST and LAST can be numbers specifying the range, or FIRST can be a
string, which means the most recent command beginning with that
string.
Options:
-e ENAME select which editor to use. Default is FCEDIT, then EDITOR,
then vi
-l list lines instead of editing
-n omit line numbers when listing
-r reverse the order of the lines (newest listed first)
With the `fc -s [pat=rep ...] [command]' format, COMMAND is
re-executed after the substitution OLD=NEW is performed.
A useful alias to use with this is r='fc -s', so that typing `r cc'
runs the last command beginning with `cc' and typing `r' re-executes
the last command.
Exit Status:
Returns success or status of executed command; non-zero if an error occurs.
Note the suggestion for alias r; I use this frequently.
Depending on what terminal you're using, I know a lot used to have F3 as an option for repeating, but that's still outside the normal range for typing as well unless you have a special keyboard with more accessible function keys.
My keyboard makes the function keys easily accessible, but I don't do much command line work in unix any more, so I wouldn't be able to tell you for sure whether or not this is still possible.

Bash Shell - What is equivalent of DOS shell F8?

When working an interactive bash session, one aspect from the Windows shell I miss is the F8 key where you start typing a command, hit F8 and the shell finds the most recent command entered in history that matches what you have typed so far. e.g.
me#Ubntu07:~>cd /home/jb<F8 Key Here>
brings up my prior command:
me#Ubntu07:~>cd /home/jboss/server/default/log
Is there any way to do this in bash ?
Hit Ctrl-R before you start typing.
(There may well be another version which finds commands based on what's already been typed - I wouldn't know, as Ctrl-R has always been good enough for me :)
Pressing Ctrl-R again shows the next match etc.
My Gentoo is configured in a way that I can press PgUp and PgDn to scroll through those commands in the command history that start with what’s currently in my command line.
# cd<PgUp>
results in:
# cd hydrogen
That’s pretty much the same function. It is defined in my /etc/inputrc with the following lines:
# mappings for "page up" and "page down" to step to the beginning/end
# of the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
I have these lines in my .inputrc file:
"\e[A": history-search-backward
"\e[B": history-search-forward
This binds history search to the up and down arrow keys. So you can start typing a command, kextload say, and then each tap of the up arrow will complete the line with the previous command that started with kextload.
All of my config files are public on github.
http://github.com/jonshea/config-files/tree/master
In your case !jb would print and then run that command.
e.g.,
$ nano logconfig.properties
$ !n
nano logconfig.properties
$
Of course if you want to be on the safe side, use ctrl-r first to bring up the interactive command history.
Ctrl + R does a history search. It's a bit different in that first you hit Ctrl + R and then type what you're looking for.
If you're just talking about a command, you can use the !<cmd> to do the last one. For example, say you entered python runscript.py a while ago; you can type:
!py
or something along those lines to run that command again.
To repeat an argument to a command, you could do something like this:
echo !py:1
which would echo runscript.py back to the terminal, in this example. The number after the colon refers to the argument you'd like to use from the given command.
There's a lot of other great information about the bash history here.
If you use vi input mode (set -o vi in bash or via set editing-mode vi in .inputrc), you can use normal vi commands to search the history (/). This gives you full regular expressions, too, which can be helpful for finding a complex command.

Resources