9 ms·
I love/hate comments like yours. You have technical knowledge applicable to the problem, and you share it. I love that. You miss an important feature of the
by naikrovek 7y ago
I love/hate comments like yours.
You have technical knowledge applicable to the problem, and you share it. I love that.
You miss an important feature of the solution shown in (I presume) your rush to demonstrate your bash knowledge: the ability to edit a filename shown, in place. Brace expansion doesn't do that, doesn't show you the new filename before you commit the name change and isn't even close to interactive. This part is less great.
> Something like this isn't really necessary.
It may very well be necessary for someone else.
- kazinator 7y ago> doesn't show you the new filename $ echo mv foo{x,y}bar > the ability to edit a filename shown, in place Easily done right in the command line. First, type this: $ mv oldname _ The _ denotes the cursor. Now erase the last word using Ctrl-W: $ mv _ Now pastte it twie with Ctrl-Y Ctrl-Y: $ mv oldname oldname _ Edit in place, submit.
- matt-attack 7y agoUsing echo is the wrong way. What you do is ‘CTRL-x *’. That will expand any glob right there on the command line.
- Kiro 7y agoOr I just use this script which is much more intuitive and convenient.
- zeepzeep 7y agoBut only solves one use case. Why not learn how to do it for every other unix tool?
- Kiro 7y agoI don't see how this script prevents me from learning it. With that said, we're obviously in different ballparks when it comes to the terminal. I don't need to do anything fancy. cd, ls, mv and git are pretty much the only commands I ever use. For me, this script fits nicely into my workflow.
- kazinator 7y agoIn my other post, I got the readline method down to one control code. You type "mv name" and hit the assigned control character, which produces a second copy of the name that you can edit in place. It works with any command: mv, cp, ln, git mv ... The editing is in the same command line, resulting in a proper argument that is in the command line. It gets saved into your history, and can be pasted into a script.
- kelnos 7y agoBut it's... not? What if I want to use the same trick with 'cp'? Do I go and copy-paste my script, or maybe spend time to make it generic enough to handle both commands? That's not a good use of my time when the shell will do it for me, if only I'd spend a minute to learn.
- mehrdadn 7y agoNot just that, but the time it takes me to get the braces correct is usually more than it takes me to just select the damn thing with a mouse, paste it, and modify it...
- markstos 7y agoAt least with the `fish` shell, you can use "Alt-E" to edit any command line. It's a visual, interactive, re-usable solution that shows the result before you commit it.
- deleted 7y ago[deleted]
- Lio 7y agoIs that different to bash or zsh? Both support editing using either vi or emacs shortcuts via readline.
- Godel_unicode 7y agoNot at all, you're just not thinking the solution all the way through. If you want to see the results, an echo gets you there. The important point though, is that one of the solutions moves you closer to a common language with other posix users, and one moves you farther away. If you use the tools the way they're intended to be used (brace expansion, for example), you'll recognize it when other people use it. You'll understand similar brace expansions in other commands you see others craft. You won't need to remember if that shell you sudo'd into uses your override or not. The reasons go on forever. It really isn't necessary. That's not to say someone can't think of a reason for it, but rather that there are better ways to do what it does.
- titzer 7y agoFor the OP's sake, I hope they completely ignore your advice. Customize your environment to what makes you the most efficient. Yeah, sure. Learn to use all the things that are there and understand what others do. But customize your environment to what makes you the most efficient. This is an interactive command. It's not like they are going to foist it onto everyone else (which is the true sin, IMHO).
- bb88 7y agoChanging the default behavior of a standard posix command is a footgun. It would have been better to rename it.
- titzer 7y ago> It would have been better to rename it. Maybe. It's worthwhile pointing out that "mv x" with no second argument is an error: mv: missing destination file operand after 'x' Try 'mv --help' for more information. I also don't have the habit of letting people type in my shell, so there's that. TBH I really don't understand the level of pedantry (and frankly, sheer outrage) in this thread at all. It sucks to be on the receiving end of such disapproval over something so trivial. Let this person do what they want! It's a neat little hack. It also inspired me to see what more I could do with 'read'--something I have ignored for 20 years.
- graywh 7y agoZsh can expand expansions before you execute a command
- kesor 7y agoIn my `set -o vi` bash I can type `v` to edit the command in vim before executing it.
- deleted 7y ago[deleted]
- bnegreve 7y agoFor the record, you can do this with C-x C-e without vi keybindings
- pknopf 7y agoThe more I read these comments/suggestions, the more I realize I'm bash'ing like a child. I seriously need to embrace my shell a little more.
- rmetzler 7y agoSame for me. I knew the bash has emacs bindings and I know just enough to go to beginning, end, delete a word, etc. but I never knew you can start emacs with a shortcut like this. How do you even exit emacs properly?
- bnegreve 7y agoC-x C-s to save, C-x C-c to quit. But you can also control which editor is going to be used to edit your command line by setting the EDITOR environment variable. It also works for git, and other commands that need to spawn editors.
- m463 7y agoI never knew that existed. My first thought was "emacs editing characters already work on the command line." Then I thought, maybe there are commands you want to be really really sure of before submitting them? Then I realized I very frequently use very long commands with loops and pipes that might benefit, like: for i in *.py; do mv $i AA_$i; done or a little of both: find . -type f |while read f; do if cmp -s "$f" ../new/"$f"; then rm -f "$f"; fi ;done (thanks!)
- arkaic 7y agoAlso, typing braces are annoying. To think about it from a UX perspective, this solution on gist, judging by the video, is perfect.
- qplex 7y agoLet me just say that typing {}[] is super painful on many international variants of QWERTY. Typically you need to press a modifier for both (Alt Gr). https://en.wikipedia.org/wiki/QWERTY#International_variants https://en.wikipedia.org/wiki/QWERTY#International_variants
- izolate 7y agoWhich is why if you're a programmer, you're doing yourself a disservice by not using a keyboard with a US layout.
- llarsson 7y agoFull agreement from me. My ergonomics and efficiency both improved immensely when I switched from my native layout to US International. With US International, unless I want to hit the key combo to change layouts, I can still write a single or just a few umlauts (say, for a person's name). I find it easier to keep my focus when I don't have to make the right wrist twister that is otherwise required for { if using a Swedish layout.
- read_if_gay_ 7y agoAlso try setting caps as compose key and using the standard ANSI layout. Solves the problem of always having to double press characters such as quotes.
- RMPR 7y agoCouldn't agree more, was using the French layout and while learning vim I always asked myself what are those weird keybindings, switching to US layout, they suddenly started to make sense because the keys are more reachable without the usual gymnastic.
- deleted 7y ago[deleted]
- adito 7y agoAgree with you. Said in another word, "Their statement is correct but misses the point"[1] [1]: https://news.ycombinator.com/item?id=22854475 https://news.ycombinator.com/item?id=22854475
- b0rsuk 7y agoIf interactive editing is the big win, you can install 'mc' and it will give you many more interactive editing capabilities. Renaming via bash expansion has the additional advantage that it's script-friendly.
- dgl 7y agoI really like that zsh can be set to expand globs on tab, both useful for editing when a glob is “close enough” and for checking the result quickly. I think a lot of the value of a script like this is you don’t have to switch contexts or think any different before you use it.
- deleted 7y ago[deleted]
- mrb 7y agoInteractively editing the filename is possible with this even simpler solution: mv foo.txt ↑ Alt-. This works in bash and any shell using readline. Explanation: https://news.ycombinator.com/item?id=22863402 https://news.ycombinator.com/item?id=22863402
- bJGVygG7MQVF8c 7y agoGreat tip thanks! Similarly, readline's vi mode makes interactive editing in place easy
- xorcist 7y agoInteractive editing is generally something to avoid. It can't be part of a bash script, or a puppet manifest, or checked into version control. In the cases where you need to do interactive edits of file names, something like emacs or mc is probably less error prone than bash.
- mekazu 7y agoCould use mv -iv for interactive and verbose if you want a rollback or commit alternative. The real question is: why move files at all?
- ComodoHacker 7y agoFor the same reason you move stuff in your room. You don't always put it into right places. Or someone else has put it somewhere.
- deleted 7y ago[deleted]
- ivanhoe 7y ago> You miss an important feature of the solution shown in (I presume) your rush to demonstrate your bash knowledge: the ability to edit a filename shown, in place. OK, then just use !#$ (the last word of the current command) and press CTRL + ALT + e to expand it into the actual value.
- bJGVygG7MQVF8c 7y ago> (I presume) your rush to demonstrate your bash knowledge Heaven forbid someone share knowledge. Let's interpret as uncharitably as possible and passive-aggressively love/hate. > Brace expansion doesn't do that, doesn't show you the new filename before you commit the name change Allow me to rush to demonstrate my zsh knowledge for any passersby who might benefit: % mv foo-bar-{bar,baz}.txt|[TAB] % mv foo-bar-bar.txt foo-bar-baz.txt
- kelnos 7y ago> in (I presume) your rush to demonstrate your bash knowledge It's unfortunate that you reach for the uncharitable explanation first. To return the favor, I assume you rushed to post your reply because you think being contrarian and chastising the top-rated comment will get you more karma points? See, that wouldn't be cool... of course I don't actually believe that. While I don't agree with you, I assume you're arguing in good faith! The simple fact is that I just don't see the need for it; after many years of heavy command-line use, I wouldn't use a tool like this. Either the simple case is sufficient, or I need something much more advanced, like prefix/suffix/regex replacement across multiple files (in which case shell variable expansion will often work, or I'll reach for `sed`). Beyond that, I try to avoid getting used to tools that only exist on my machine, and not on the tens of machines I ssh to weekly. (I already sometimes find myself, for example, attempting to use ripgrep on remote hosts that don't have it, and it's annoying.) If you look downthread a bit, there are several comments that expand on doing it the "built-in" way that covers the original use case even more. For example: Type "mv foo.txt" -> hit ctrl+w (cut word) -> hit ctrl+y (paste word) twice -> use arrow keys to edit. Someone else mentioned fish will expand braces if you hit the <tab> key. There are so many cool built-in things that the shell can already do for you; writing up a hacky solution that only works for one use case (the methods mentioned above work for mv, cp, ls, etc.) is limiting your learning and wasting your time. > It may very well be necessary for someone else. I really wish people would understand that when people make statements like I did, there's an implicit "for me" or "in my experience" tacked onto the end of that. Yes, obviously, people have different needs, and yes, obviously, I am talking about my own personal experience and needs. But I really do think that shell brace expansion covers the 90% case, and I was under the -- perhaps mistaken -- impression that the OP may not have known about it, or realize how it can be useful in this particular context. I knew about this sort of shell expansion for many years before I saw someone using it to do a file rename, and it was a big "wow, how have I never thought of that?" moment for me at the time; I expect that sort of thing happens to a lot of people.