12

If I open a directory in vim using vim . I will get a file-browser-type interface of the current directory. From there I am expected to browse to a file, and then start editing it.

I want to use this interface to rename files. Normal vim rename works like %s/term1/term2/, but when I try it out here, it returns: E21: Cannot make changes, 'modifiable' is off.

Is there a way to turn modifiable on in this scenario, or does this simply mean that it cannot be done like this with vim?

Questionmark
  • 451
  • 1
  • 4
  • 18

4 Answers4

11

Yes, it can't be done like that.

That "file-browser-type interface" is provided by a built-in plugin called netrw. It is read-only so yeah, you can't modify it.

You are supposed to hit R to rename the file under the cursor or the marked files.

See :help netrw, :help netrw-browse-maps and more specifically :help netrw-R.

If you want to batch-rename files using Vim you should try qmv from the renameutils package or vidir from the moreutils package (thanks to Dmitry for the heads up).

romainl
  • 22,554
  • 2
  • 49
  • 59
2

Check out the renamer.vim - Use the power of vim to rename groups of files plugin (now purely maintained on GitHub). Like netrw, it presents the directory contents in a scratch buffer, and then lets you edit that buffer, and finally apply the edits to the underlying files.

Ingo Karkat
  • 22,638
  • 2
  • 45
  • 58
  • https://github.com/qpkorr/vim-renamer is the new location. The vim.org link is outdated. – Andrew Keeton Dec 31 '19 at 16:06
  • 1
    @AndrewKeeton: Thanks, added that link. The vim.org refers to it, too, but it's convenient to directly add it here, too. Though I really like the central vim.org repository, many plugins now directly use GitHub exclusively. – Ingo Karkat Dec 31 '19 at 17:02
1

Netrw will allow you to apply a pattern to rename files since about v143. The procedure: mark files (use mr to mark files based on a pattern), then hit R. Upon the first renaming query, respond with the strings/-frompat-/-topat-/ where the -frompat- and -topat- are vim substitution patterns. The s/ is required for this. Example:

s/\(.*\)\.c/\1.cpp/
user21497
  • 226
  • 2
  • 5
1

I try to use "system()" to accomplish this task using the shell (if that is the case, of course) creating a function to accomplish this task on your ".vimrc".

func! YourFunc() range

    let l:result = system("your shell command")

    [...]

endfunc

See an use of "system()" in https://github.com/eduardolucioac/groovim/blob/master/.vimrc

[]'s

Eduardo Lucio
  • 1,194
  • 2
  • 24
  • 48