The Emacs Way: Moving and Renaming Files

In the previous article in the Emacs ways series we did a little jive on copying files from one source location to a destination location. As you can imagine, this keeps a copy in the source location and creates a copy in the destination location—exactly what you would expect.

Now, what if you don’t want a copy in the source location, but want to move that file completely to a different location? Or, what if you wanted to keep the source file in place but merely change the name?

This is where the UNIXy way of doing things can be a little confusing, and I think the Emacs way is superior, but I will let you be the judge of that.

If you are a writer interested in introducing open source tools into your workflow, you will love my DRM-free eBooks:

Now let’s get into some Emacs fun.

UNIXy Way

If you are coming over the world of GUI file managers, you likely think about renaming and moving files as two separate operations. However, on your UNIXy command line, renaming and moving are both covered by mv command.

mv old_name.txt new_name.txt
mv file.txt /path/to/destination/
mv -v *.org ~/documents/

Example output:

$ mv -v old_name.txt new_name.txt
renamed 'old_name.txt' -> 'new_name.txt'

$ mv -v report.txt ~/documents/
renamed 'report.txt' -> '/home/user/documents/report.txt'

$ mv -v *.org ~/documents/
renamed 'notes.org' -> '/home/user/documents/notes.org'
renamed 'todo.org' -> '/home/user/documents/todo.org'

Emacs Way

In your Emacs text editor, however, moving and renaming are still covered under one action, but keep the identification of “renaming” rather than “moving.” Let’s take a look at how this works.

In dired, position your cursor point on the file you want to rename and press R.

Or, interactively, you can do M-x rename-file.

For regexp-based renaming there is also % R.

Example dired interaction (renaming):

  /home/user/projects:
  -rw-r--r--  1 user user 1234 Jan  8 14:22 draft.txt

Rename draft.txt to: final.txt

Example regexp rename (% R):

 Rename from (regexp): \.txt$
 Rename to: .org

   /home/user/notes:
 * -rw-r--r--  1 user user 1234 Jan  8 14:22 ideas.txt
 * -rw-r--r--  1 user user 5678 Jan  7 09:15 plans.txt

Rename ideas.txt to ideas.org? (y or n) y
Rename plans.txt to plans.org? (y or n) y

The regexp rename is particularly powerful for batch operations, matching the flexibility of shell globbing but with interactive confirmation for each file.

Be the first to comment

Leave a Reply

Your email address will not be published.


*