1

After going through the renaming guides from the doc folder in vim I found a way to replace text with the input :%s/frompattern/topattern/g. Reaching the renaming files with the file browser tutorial/guide I managed to learn the MR and R method stated in the netrw-R help file.

Then I tried to use the text replacing method on multiple files like so:

:set ma
:%s/frompattern/topattern/g

All the files changed after backspacing the : requested from the notification

"Press Enter or Type Command To Continue".

All files changed unfortunately those changes have only been made inside vim , no files have been altered.

Is there a way to apply the changes after renaming them that way or is that input strictly for text inside a file/document ?

Trying to exit vim through :x assuming it would apply changes gives me:

E45: 'readonly' option is set (add ! to override)
Press Enter or Type Command To Continue

Trying to exit with :x! gives me:

"/home/user/VimLearning/"
E502: "/home/user/VimLearning/" is a directory
Press ENTER or type command to continue 

I can only exit with :qa!

This post explains the same technique as the NetRw-R help file but better in my opinion.

But the question remains as to renaming multiple files via :%s/frompattern/topattern/g and applying those changes. Would it be possible ?

Thanks for all the answers , I am really enjoying learning Vim and the QMV suggestion that was posted here. In the NetRw-R help file there is this info:

The g:netrw_rename_cmd variable is used to implement remote renaming. By default its value is:

ssh HOSTNAME mv

One may rename a block of files and directories by selecting them with V (linewise-visual) when using thin style.

The latter is what I am interested in. Renaming a block of files and directories. There are no examples. Would anyone be kind enough to point me towards one ? Or has that feature been removed or it is only for the renaming of remote files and folders as the text above it would mention ? Would that work the same as QMV ? Thank you guys , this community has been very helpful.

3
  • 2
    I would use external tools like vidir, qmv and the such for that. Or vifm file manager.
    – Maxim Kim
    Sep 23, 2021 at 6:35
  • Welcome to Vi&Vim SE....and to Vim! :)
    – B Layer
    Sep 23, 2021 at 10:52
  • As I pointed out in the answer, netrw can't do mass renames as qmv or emacs dired do.
    – Maxim Kim
    Oct 13, 2021 at 13:42

3 Answers 3

1

Netrw doesn't support file renames based on buffer changes (the way emacs dired does it).

There are external tools like vidir and qmv that runs vim (to be precise EDITOR) with directory contents where you can change filenames to be renamed "en masse".

It looks like this:

enter image description here

PS, you can also run it from within vim using built-in terminal: :term vidir

3
  • Note that qmv is part of the package known as renameutils. These are some excellent tools. Use qmv and qcp for bulk operations. Also check out icp and imv. When used in conjunction with set -o vi an alias like alias mv='imv --pass-through' is a nice convenience with a Vim flavor. :)
    – B Layer
    Sep 23, 2021 at 11:21
  • The Batchy plugin from Martijn is pretty great imo
    – D. Ben Knoble
    Sep 23, 2021 at 12:18
  • The Fern plugin has the possibility to rename files based on the modification of a path buffer. Jul 10, 2022 at 18:17
0

The Fern plugin has the possibility to rename files based on modification made on a Vim buffer.

  1. Open the folder that contains the files that you want to rename: :Fern path_to_folder
  2. Select the files you want to rename using the Fern action mark (a mark or m)
  3. Select the action rename (a rename)
  4. A buffer with the path selected is created
  5. Modify the created buffer according to your need
  6. Save the buffer (:w)
  7. The files are renamed accordingly

Remark: If you want to mark all file of the folder you can use the mark-children (from the fern-mapping-mark-children.vim plugin) the folder root (the first item) (a mark-children)

enter image description here

0

I really like my batchy.vim tool, but of course I would say that as I wrote it πŸ™ƒ

The main feature is that it's really simple and "obvious" to use it, which is important because I don't do batch renames very often. Every time I wanted to do batch renames I always had to figure out how to use $tool_I_choose_last_time; which included a few of batchy.vim's predecessors I had in my vimrc.


The README has more expansive docs, but to use it in brief, run :Batchy, which populates the current buffer with a list of files in the current directory, or run it from a shell alias:

alias batchy='vim +":Batchy echo" +"setl noswapfile buftype=nofile bufhidden=hide nowrap"'

This gives you a list of files in three columns:

ftplugin         ←  ftplugin         β”‚  d  0.0K  Thu 2021-06-17 00:44:20
plugin           ←  plugin           β”‚  d  0.0K  Thu 2021-06-17 00:50:21
LICENSE          ←  LICENSE          β”‚  f  1.1K  Thu 2021-06-17 00:21:12
README.markdown  ←  README.markdown  β”‚  f  2.3K  Thu 2021-06-17 00:45:46

Everything after the β”‚ is a comment and lists the file type, size, and modification date. You can put anything you want there when editing. Note this is U+2502 ("box drawings light vertical") and not the regular | (U+007C, vertical line).

Now, let's say you modify the buffer to prefix every file with xx-:

xx-ftplugin         ←  ftplugin         β”‚  d  0.0K  Thu 2021-06-17 00:44:20
xx-plugin           ←  plugin           β”‚  d  0.0K  Thu 2021-06-17 00:50:21
xx-LICENSE          ←  LICENSE          β”‚  f  1.1K  Thu 2021-06-17 00:21:12
xx-README.markdown  ←  README.markdown  β”‚  f  2.3K  Thu 2021-06-17 00:45:46

Run :Batchy again and it will replace the buffer with a shell script:

mv -n 'ftplugin'         'xx-ftplugin'         # d  0.0K  Thu 2021-06-17 00:44:20
mv -n 'plugin'           'xx-plugin'           # d  0.0K  Thu 2021-06-17 00:51:16
mv -n 'LICENSE'          'xx-LICENSE'          # f  1.1K  Thu 2021-06-17 00:21:12
mv -n 'README.markdown'  'xx-README.markdown'  # f  2.5K  Thu 2021-06-17 00:51:06

Nothing gets run automatically. Write it to a file or use :%!sh to run it.


The nice thing about reversing the "dst src" order is that it's easier to edit things with standard Vim commands:

:%s/^/prefix-/    add a prefix
:%s/^prefix-//    remove a prefix

:%s/ /-suffix/    add a suffix
:%s/-suffix //    remove a suffix

:%s/word/NEW/     replace word; because only the first match is
                  replaced this leaves the source filename alone.

If it would keep the src dst order all of the above would require a lot more complex patterns.

Keeping the original filename in there is convenient as it's a bit clearer, and it also allows you to reorder things if you want; I was never quite comfterable with tools that depend on line numbers/order.

Simply delete a line if you want to leave that file alone.

Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.