Terminal: prefix previous command?

OJFord

Frequently in Terminal (though I fully expect any answer will work in bash and most others), I type some command, and then get some sort of permission error due to missing sudo.

Okay, so press 'up', hold/hammer 'left', type sudo and hit 'enter'.

It's annoying. More so the longer the command was.

Note that missing sudo is worst case; a typo nearer the beginning than end is similarly annoying.

What I would like, is a way to perform a sort of 'up and home' keystroke. Actually, perhaps those two strokes will do it - but my Macbook keyboard doesn't have Home, so I cannot try.

Bonus points for a way to insert sudo and hit 'enter' auto-magically too, say by CMD+Shift+S.


Can this be done, or am I dreaming?

Hastur

Simply use UP and CTRL-A.

Both !sudo and sudo !! function easy. With a difference:

  • sudo !! will execute the last command taken from the history with sudo in the beginning.

  • !sudo will execute the last command in the history that started with sudo.

  • Usually CTRL-A brings you at the beginning of the line in a linux terminal and CTRL-E to the end.
    If you want you can continue to use your strategy. You have only to discover the shortcut of your system. You can find a list of shortcuts in graphic and text environment e.g. here.

Only a warning: sudo !! and !sudo function really fast and you have no visual control of the line you are running as superuser. This can result harmful sometimes. Specially if you incur in a missprint.

Let's we do a dramatic example:

cd $HOME/Directory_To_Delete  # You go to delete what is inside a directory
rm -rf . ; cd ..              # You do it and go up of one
 apt-get update               # Note the space in the beginning of the line  
sudo !!                       # Gone!! Now you realize you never really wanted 

Too late you just erased your home directory and all the subfolders !!.

When you write a command with a space as a first character it's possible that it will not finish in the history. This depend from the value of the variable $HISTCONTROL. If the list of its values includes ignorespace (or ignoreboth) the lines which begin with a space character are not saved in the history list. So with sudo !! you will execute the last command before.

You can imagine similar situation with !sudo, it will execute the last command starting with sudo recorded in the history, but if the last that you remember was starting with a space... now you know you will use another.

Last note: less is dramatic your error more it can be devious because it can pass without that you notice... and you will risk to be called to pay all the interests of a bill that you cannot remember.


Update (Bash is great):
with recent versions of Bash it is possible to fix this dangerous behaviour with the following command that I suggest to put in ~/.bashrc file:

shopt -s histverify

From man bash you can read

If the histverify shell option is enabled and readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the readline editing buffer for further modification.

So you can read, and modify when needed, the command that you will execute.
Since it is not the default setting in many distribution we still have to remember it when we use other accounts.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Search for a previous command with the prefix I just typed

From Dev

how to assign terminal variable from previous command's output

From Dev

Search for a previous command with the same prefix when I press Up at a shell prompt

From Dev

OSX terminal weird prefix

From Dev

How to setup a terminal prefix

From Dev

How do I see all previous output from a completed terminal command?

From Dev

VSCode terminal previous commands

From Dev

Jumping to previous prompt in terminal

From Dev

VSCode terminal previous commands

From Dev

prefix command error

From Dev

Autocomplete prefix command in bash

From Dev

Avoid terminal characters from previous terminal commands

From Dev

Recover previous Python output to terminal

From Dev

Terminal command follows the lifetime of another terminal command

From Dev

Use previous "path" in a command

From Dev

Checking the execution of the previous command

From Dev

Bash prefix command name for subcommands

From Dev

Use key as both prefix and command

From Dev

Prefix to each output of a command on runtime

From Dev

Bash prefix command name for subcommands

From Dev

How to explain './' prefix before the name of an executable in terminal?

From Dev

Modify Bash prompt prefix in OS X terminal

From Dev

How to explain './' prefix before the name of an executable in terminal?

From Dev

Mac terminal prefix has been changed

From Dev

Creating a terminal command programmatically?

From Dev

Is there a terminal command that will click the mouse?

From Dev

Switch encoding of terminal with a command

From Dev

Command run in terminal, not with shortcuts

From Dev

run :!command in a real terminal

Related Related

HotTag

Archive