Emacs dired - using a predefined variable

In emacs dired, I want to do something that I do quite often in Microsoft PowerShell.

In PowerShell, I have a set of folders that I always use, and I assign them the full path to the global variables in my profile script (similar to init.el in the emacs world), for example:

 $standardTemp = "C:\Long\Path\To\Folder" 

If I'm in a different folder and I want to copy something to the above folder, I:

 copy myFile $standardTemp 

Even more useful as a function, if I put a backslash after $standardTemp , it will expand it, so I can go into subfolders if I need to. This is a very amazing feature and saves a lot of time.

With the dired copy command, I can do something similar if I define variables, for example. setq in my init.el file?

+5
source share
4 answers

How about something like that?

 ;; Use ido (require 'ido) (ido-mode t) ;; Make a hash table to hold the paths (setq my-target-dirs (make-hash-table :test 'equal)) ;; Put some paths in the hash (sorry for Unix pathnames) (puthash "home" "/home/jhrr/" my-target-dirs) (puthash "target" "/home/jhrr/target/" my-target-dirs) ;; A function to return all the keys from a hash. (defun get-keys-from-hash (hash) (let ((keys ())) (maphash (lambda (kv) (push k keys)) hash) keys)) ;; And the function to prompt for a directory by keyword that is looked ;; up in the hash-table and used to build the target path from the ;; value of the lookup. (defun my-dired-expand-copy () (interactive) (let* ((my-hash my-target-dirs) (files (dired-get-marked-files)) (keys (get-keys-from-hash my-hash))) (mapc (lambda (file) (copy-file file (concat (gethash (ido-completing-read (concat "copy " file " to: ") keys) my-hash) (file-name-nondirectory file)))) files))) 

It is not exhaustively verified, as I just whipped it in 10 minutes, but it does the job and can process multiple files.

You will need to open the executable buffer in the directory where the files are located and mark each file that you want to copy with "m", then call my-dired-expand-copy , and it will prompt you to specify the destination (in form the keyword from the hash table table that we set) for the file, before finally copying the file to a directory that maps to the target keyword.

This doesn't quite apply to the case of the subdirectories you are talking about, but it should not be too complicated to get a bit more hack there.

UPDATE:

Now you should invite you to go down to subdirectories from the original target; perhaps not the most surprisingly wonderful UX overall, but it works:

 (defun my-dired-expand-copy-2 () (interactive) (let* ((my-hash my-target-dirs) (files (dired-get-marked-files)) (keys (get-keys-from-hash my-hash))) (mapc (lambda (file) (let ((target (gethash (ido-completing-read (concat "copy " file " to: ") keys) my-hash))) (if (y-or-np "Descend?") ;; Descend into subdirectories relative to target dir (let ((new-target (ido-read-directory-name "new dir: " target))) (copy-file file (concat new-target (file-name-nondirectory file))) (message (concat "File: " file " was copied to " new-target))) ;; Else copy to root of originally selected directory (copy-file file (concat target (file-name-nondirectory file))) (message (concat "File: " file " was copied to " target))))) files))) 
+3
source

When I need to use dired to access frequently used directories, I use the standard emacs bookmark options.

I will manually go to the directory and click

 Cx rm 

execute command

 bookmark-set 

You will be prompted for the name of the bookmark. Enter a shortcut that you can remember.

At this point, anytime you want to open this directory in dired mode, simply run the command

jump bookmark

using keys

 Cx rb 

Enter your shortcut into the directory and dired will open at that point.

To copy from one directory to another, make sure that the following set is in the initialization file

 (setq dired-dwim-target t) 

Then you can open a window for the source directory and another window for the target directory inside the same frame, and then by default it will automatically assign the source and target location to the corresponding directories.

Please note that this is just a subset of what emacs bookmarks can do for you!

  • Chris
+1
source

In addition to using bookmarks, use directory aliases (such as symbolic links) or directory-abbrev-alist . See the Emacs Handbook, node File Aliases .

0
source

If you want to insert the value of the environment variable into the minibuffer, you can do it as follows:

 Cu M-: (getenv "THE-VARIABLE") 

where THE-VARIABLE is the name of the variable. Using Cu inserts the sexp value into the current buffer (in this case, the minibuffer).

So, you could, for example, use C to copy the marked files to Dired, and then use Cu with getenv sexp for the existing variable that you have to insert your value into the minibuffer when prompted for the directory to copy.

(Depending on your Emacs setup, you may need to set enable-recursive-minibuffers not for nil in order to use M-: from the minibuffer.)

0
source

All Articles