Maybe there is a tool that does that? Maybe I’m doing this all wrong and there is a different way I should browse?
There’s already some great answers in the thread, so I won’t add to those.
Some shortcuts are shell-specific. For example,
!!,!:$, and^old^neware inbash, but not infish.Some things, like
Alt-.andCtrl-a, are implemented byreadline, which can be used elsewhere, as well, for example in the python interpeter.Depending on your context, it may be better to invest time learning methods implemented by
readline. While not necessarily universal, they are more versatile.There were heaps of functions I was using not realising they were from readline. For example,
Ctrl-rfor reverse interactive search,Ctrl-a/efor start/beginning if line,Ctrl-wfor deleting a word.You mean something like this?
ls /your/long/path/name
cd !$Holy fucking shit. I’ve dabbled in Linux for so long to not know. Thank you!
I think, if your using bash
alt + .
Does the same thing as typing
!$
ls /super/long/path/to/dir
cd alt + .
I believe if you hit it again, it cycles through all previous last words
To add,
!$ gets replaced with the last word from the previous command line
!! Repeats the most recent command
!-N Repeats the Nth previous command
!string Repeats the most recent command that began with ‘string’
!!is the goat when you needed to run the last command usingsudo💯
I’m loving this
Huh - TIL about !-N
Thanks 🙏
In some shells (e.g. fish), “running” a directory changes to it so you can save keystrokes and just delete the "ls ".
You can hit TAB in any decent shell to autocomplete paths. Personally I highly recommend fish
The way I solve this problem is to use nnn instead of ls. It’s a full file manager for the terminal but so light and quickly started that it’s as convenient as ls (a lot more so actually). You can navigate around your file system with vim-like key bindings and then have the option to exit either to the directory you started from or the one currently displayed.
What I do is: Up, Home, type
cd, Delete (hold/repeat as many times as needed), Enter.The Home key instantly brings your cursor to the start of the command line (before the
ls), even if the command is long enough to wrap.I’m not a terminal guru, and I don’t aspire to become one. I don’t want to memorize arcane symbols that each has a subtly different meaning, and I especially don’t want to execute them before I see explicitly what they’re going to do. So while the top comment of
cd !$is a perfectly good and correct answer, I offer this alternative in case you, like me, like to see what you’re doing as you do it. Home and End are pretty good.I had already found ctrl+a that brings me back to the front of the line. It’s bit more convenient for my hands than home.
I have a really strong muscle memory for Home so it’s super convenient to me, but ctrl+a sounds just as good.
Minor addition: I’ve had some shells where home will bring you back to the beginning of the current line, not the whole command.
Ctrl+Homealways brings you to the very start of the command if this applies to you
-
You could start by typing cd and use tab completion to explore your target folder(s). This would require at least a rudimentary knowledge of what you’re looking for so you can prefix your search
-
If you really want to explore the contents and pick your target without knowing anything in advance,
cd "$(find . -type d | fzf)"would give you a smart browser. You can of course replace ‘.’ with any base directory you like. Requires fzf. -
In general, zoxide can help with navigation.
-
If you want to stay a purist, I’d go for simply recalling your last ls command from the history and replacing it with cd. An example has been given in the thread already.
-



