Hacker News new | ask | show | jobs
by jfreax 2128 days ago
Nice collection of quite useful tools. Some of these can be easily replicated by using a more modern shell (bash, zsh) like mispipe, others are just shortcuts (e.g. ifne, chronic).

But what immediately stood out to me is `vidir`. I really like the idea of editing file names with an editor. Using loops and regex in a shell for mass renaming can be a mess. It should be way easier with `vim`. This tool made me install moreutils.

12 comments

On most POSIX systems you can use fc(1) to edit a command in your $EDITOR. In vi(1) and friends you can then use "%!ls" to replace the contents of the buffer with directory listing and edit the commands you want.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/f...

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/v...

> you can then use "%!ls" to replace the contents of the buffer with directory listing and edit the commands you want.

I often used :r !ls for that, thanks for the tip + it's shorter

Note that in general this is not a one-to-one replacement. `%!cmd` sends the current buffer contents to `cmd`’s stdin and replaces the buffer with `cmd`’s output. In the case of ‘ls’ this works since it doesn’t take anything on stdin
In Emacs, you can do this by invoking shell-command-on-region, by default bound to C-u M-| - iirc if no region is active it replaces the buffer, but I rarely use it that way so I might be wrong about that. At worst, C-x h C-u M-| gets you there.
huh! I've been working on posix systems for 20 years and I didn't know that...
You just improved my life. Thank you.
Check out renameutils[1], which has the advantage over vidir of doing sanity checks before renaming.

Also, this can be done in emacs using wdired.

[1] - http://www.nongnu.org/renameutils/

The ranger file manager also has this built in.
Was about to point this out, but it's good to know if you ssh in a box which doesn't have Python installed.

Edit: Or use a more portable terminal FM, nnn pops to my mind

MMV is reasonably powerful, but straightforward, can produce & consume rename listings, and is included in a lot of distros.: https://git.deb.at/w/pkg/mmv.git
See writable dired-mode in emacs. Here's a short demo: https://youtu.be/8l4YVttibiI
I use Emacs shell-mode for most of my terminals, which allows navigating and editing the buffer just like any other file. I'll often make several copies of a command, then use a macro to alter each one; after checking that they look right, pressing Enter will send them all to the shell.
I always tried to use the shell with emacs (eshell, multi-term) but it didn't render correctly curses and all the fancy stuffs like emojis. Out of curiosity, how do you manage that?
Check out emacs-libvterm. I’ve replaced all terminal emulators with it. All the benefits of an Emacs buffer and no issues with curses or lag.
When I used Emacs, Eshell was perhaps my favorite feature. I loved how modern it felt in many ways; why shouldn’t you be able to just `cd` into a remote location (TRAMP+SSH under the hood), or invoke Elisp commands like Magit straight from the shell. It could however be quite slow, and choked on some common escape codes (e.g. the progress bars emitted by the modern Ubuntu `apt` command.)

Regarding curses, I found that there was two solutions. The first is to automatically spawn curses apps in a proper terminal emulator, you just have to setup the `visual-commands` variable properly. The second alternative is to replace curses apps with Emacs apps, e.g. htop to helm-top. Personally, I ended up going the second route after a while, as I realized that there are actually very few curses apps that are important to me, and that Emacs apps are better integrated if you use Emacs for everything else.

If you rely on a lot of curses apps, a “real” terminal like emacs-libvterm may however suit you better. Renders curses apps and emojis as well as any other terminal I’ve used. It’s also much faster than ansi-term and friends at rendering.

I use a separate terminal (st) for curses-like things. They work in Emacs ansi-term, but since that doesn't offer the same navigation/editing interface as normal Emacs buffers I don't find it compelling enough to use.

Eshell is interesting, but I can't use the bashisms I'm used to, and I can't copy commands back-and-forth between the prompt and a standalone script.

shell-mode lives in-between these two extremes: it runs a normal shell, but Emacs manages the buffer. I don't know about ansi-term or eshell (or alternatives mentioned by others), but shell-mode handles emoji fine, as well as progress bars and colour codes; I think it defaults to TERM=dumb, so many programs won't output colour, etc. unless you override it to something like TERM=xterm-256.

Usually with ansi-term instead, if I'm honest. Also look into emojify-mode if your system doesn't handle emoji fonts well or at all.
Belatedly to add - look into the eshell-visual-* lists, to configure commands, subcommands (like "git log"), and I think some other traits, such that matching commands get their own ansi-term buffer.
It's one of the reasons I like using the nnn file manager. It has a batch rename feature with exactly that functionality.
+1 for the powerful yet minimalistic nnn where concise help is only a question mark away and the default mnemonics make sense. Wonderful piece of software.
Honestly one of the most well maintained projects I’ve ever used. The author has a coherent “philosophy” and vision of how the program should work and does a good job of preventing feature/scope creep. Don’t see that too often
There are Vim plugins that let you rename files and directories within Vim, such as https://github.com/qpkorr/vim-renamer.

Also if you happen to use fern[0] you can mark off multiple files and directories, hit a hotkey and now you can edit their paths in a Vim buffer.

[0]: https://github.com/lambdalisue/fern.vim

I've known about `moreutils` for years, but the only two I ended up actively using myself are `vidir` and `vipe`.

Outside of `moreutils`, one of the utilities I always install on all my machines is `atool`, which is just so much nicer and more intuitive than trying to remember the command-line options needed to handle the various tar.*, rar, zip, 7zip, lzip, etc. archive formats from the command line.

you might also be interested in vimv and vimcp

https://github.com/thameera/vimv

https://github.com/danieldugas/vimcp

I used vidir yesterday for something that would have been more annoying with other tools - if you swap the numbers at the beginning of the line, then you'll safely swap the file contents. Useful if your TV episodes are named the wrong thing...
Emacs has dired for that reason. Imagine ls that you can edit.
> vidir

I just use ranger for that.