this post was submitted on 16 Nov 2024
37 points (100.0% liked)

Linux

48213 readers
715 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

There is a feature in termux (android) history command which when you use !371 to execute the command 371 in the command history it prints that command in the prompt instead of executing it, then you just press enter to execute it. I found it very useful because many times I want to execute a command that is in the history but with some modification, I'm using Konsole in my desktop PC and I couldn't find an option to make such a thing. The only one I found is executing history -p !371, but that just print the command to stdout and not to the prompt itself.

EDIT: the answer is !371:p then up and the command 371 shows up in the prompt. Thanks Schizo!

top 25 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 14 hours ago* (last edited 14 hours ago)

As the other commenters have mentioned, this is part of the shell configuration and outside the scope of the terminal emulator.

You can configure this yourself by adding shopt -s histverify to your bashrc.

[–] [email protected] 4 points 22 hours ago

There's always Ctrl+R

[–] [email protected] 5 points 1 day ago (1 children)

https://www.gnu.org/software/bash/manual/bash.html#Miscellaneous-Commands

Add to your .bashrc following lines:

bind '"\C- ":shell-expand-line'
bind '"\C-x":edit-and-execute-command'
  • Control+Space: Now you can expand variables, aliases, !492 history commands, the tilde without executing the line. Now you can make changes to the command.
  • Control+x: Opens the current command in an external editor (such as Vi, or whatever is setup for VISUAL or EDITOR variable). Now you can edit the command and if you save the temporary file and exit editor, the modified command will be executed. If you do not save, the unmodified command before launching the editor will be executed.
[–] [email protected] 2 points 1 day ago
[–] [email protected] 4 points 1 day ago* (last edited 1 day ago) (1 children)

not sure if it's what you're looking for but on my Konsole (I think it's actually just a bash feature), Ctrl+R lets you search through your command history and easily pull up past commands. it's called reverse-i-search if you wanna check it out.

[–] [email protected] 2 points 1 day ago (1 children)

I was just told, but for the quick test I did I doesn't seem to let me modified the command before executing it.

[–] [email protected] 2 points 1 day ago* (last edited 1 day ago) (1 children)

yeah I think you're supposed to be able to but it seems borked for me too. my terminal kinda freaks out when I try to move the cursor to the right to edit the command.

edit the borking was for sure because i had a broken custom $PS1. i reset it to something normal and i'm able to edit the commands pulled up with Ctrl + R just fine now by hitting the right arrow key.

[–] [email protected] 1 points 1 day ago

That's another great option, thanks.

[–] [email protected] 3 points 1 day ago* (last edited 1 day ago) (1 children)

i have a need for something like this too; but i've been making due with crtl+r and that's been good enough to prevent me from taking action on it.

[–] [email protected] 2 points 1 day ago (1 children)

I didn't know that, it's not what I was asking but it's a cool command, thanks.

[–] [email protected] 2 points 1 day ago (1 children)

careful with it; i need the thing that you asked for too and crtl+r has been effective enough to prevent me to creating it.

[–] [email protected] 1 points 1 day ago (3 children)

but can you modify the command? I tried but I couldn't.

[–] [email protected] 2 points 23 hours ago

For CTRL+R, once you see the command you want, hit ESC, and the command is there ready to edit.

[–] [email protected] 2 points 1 day ago

idk if you're having the same issue but unborking my $PS1 fixed this for me. left right arrow keys should let you edit the command.

[–] [email protected] 1 points 1 day ago (1 children)

yes. use the arrow keys to modify it before hitting enter

[–] [email protected] 2 points 1 day ago

the answer is !371:p and then up!

[–] [email protected] 2 points 1 day ago

Alternatively, use fc 371 to open the command in an editor and take your time figuring out how you might want to change it.

[–] [email protected] 1 points 1 day ago (2 children)

Uh, are you sure your shell you're using is bash and not zsh or something else?

Bash is indeed just !12345.

[–] [email protected] 2 points 1 day ago* (last edited 1 day ago)

Same behavior for me as OPs

[–] [email protected] 1 points 1 day ago (1 children)

Yeah, it's bash. But !12345 executes the command 12345, but I don't want to execute it, just print it to the prompt.

[–] [email protected] 5 points 1 day ago* (last edited 1 day ago) (2 children)

Does !12345:p do what you want?

Edit: that also makes hitting the up arrow result in whatever command that was, so if you wanted to edit the line or whatever, you could !12345:p, up, then edit and execute.

[–] [email protected] 2 points 1 day ago

ohh now I got it....yes thats it! Thanks!!

[–] [email protected] 1 points 1 day ago

It doesn't do anything, just prints a blank line.

[–] [email protected] 0 points 1 day ago (1 children)

Things like that aren't a feature of the terminal emulator, but of the shell. Try to find out which shell you're using on Android, maybe try using that one on desktop too.

[–] [email protected] 2 points 1 day ago

basename $0 returns bash in both.