Emacs: Don't create #these# files when not saving modified buffer - emacs

How do I prevent Emacs from creating backup copies of buffers I don't wish to save when I kill buffers that have not been saved?

See Here
Short version: put
(setq make-backup-files nil)
in you .emacs file, or toggle this feature at any time with M-x auto-save-mode.
Here is the Emacs Manual Node.

If you don't want emacs to litter your drive with backup files try adding the following to your .emacs file:
(setq backup-directory-alist '(("." . "~/emacs-backups")))
This will store every backup in a single location.

I would advice you to use something like:
(defvar backup-dir "~/.emacs.d/backups/")
(setq backup-directory-alist (list (cons "." backup-dir)))
This way you can have all backup files in one directory.

Related

Change location of .ido.last history file in Emacs on Windows

Using Emacs with ido mode enabled on Windows, Emacs tries to save a history file .ido.last when exiting. The file is located in C:/.ido.last, but it fails with a permission denied message. This is strange since I actually have access to that folder. However:
Is there a command to change the directory where the .ido.last file gets saved?
Short answer: (setq ido-save-directory-list-file "/some/file/name").
Long answer:
I keep all the little files that remember Emacs's state in a single directory under the user-emacs-directory. I'm not sure what this is on Windows, but I think it's C:\Users\<username>\Application Data\.emacs.d\. On Unix, it's ~/.emacs.d/. The variable user-emacs-directory should be defined by Emacs, no need to set it.
(setq emacs-persistence-directory (concat user-emacs-directory "persistence/"))
(unless (file-exists-p emacs-persistence-directory)
(make-directory emacs-persistence-directory t))
(setq ido-save-directory-list-file (concat emacs-persistence-directory
"ido-last"))
You may want to look at the no-littering package, which sets better default locations for files like this.

Emacs auto-save: Why are files not stored in the correct folder?

I looked in various places and finally came up with the following setup for 'auto-save' mode in Emacs:
(defvar my-auto-save-folder (concat "~/.emacs.d/auto-save")); folder for auto-saves
(setq auto-save-list-file-prefix "~/.emacs.d/auto-save/.saves-"); set prefix for auto-saves
(setq auto-save-file-name-transforms `((".*", my-auto-save-folder t))); location for all auto-save files
(setq tramp-auto-save-directory my-auto-save-folder); auto-save tramp files in local directory
After having this setup for some weeks, I visited ~/.emacs.d and found that the folder ~/.emacs.d/auto-save is empty, while ~/.emacs.d contained two auto-save files of the form #!home!<myusername>!<myfolder>!<myfile>. Why are the auto-save files not stored in ~/.emacs.d/auto-save? [the folder auto-save has rights 775, .emacs.d 700]
Your error is in:
(defvar my-auto-save-folder (concat "~/.emacs.d/auto-save")); folder for auto-saves
(the call to concat with a single argument is pointless, incidentally).
If the optional element UNIQUIFY is non-nil, the auto-save file name is
constructed by taking the directory part of the replaced file-name,
concatenated with the buffer file name with all directory separators
changed to `!' to prevent clashes.
Emacs identifies directory names by a trailing /, which means that "the directory part" of the path you've used is "~/.emacs.d/".
You want:
(defvar my-auto-save-folder "~/.emacs.d/auto-save/"); folder for auto-saves
The positioning of the comma in the following is also strange (although apparently it still works):
`((".*", my-auto-save-folder t)))
That should really be:
`((".*" ,my-auto-save-folder t)))
This is what i have in my .emacs, which works well for me:
(add-to-list 'auto-save-file-name-transforms
(list "\\(.+/\\)*\\(.*?\\)" (expand-file-name "\\2" my-auto-save-folder))
t)

How to have Emacs save file at multiple locations?

Is there an easy way to have emacs save current buffer in two locations?
I could in the 'after-save-hook' programmatically copy the current file to a second location, but writing lisp code for that might take some time.
For those that are curious why I want this:
I want the changes I make to my JSP immediately be deployed in tomcat's webapps/myapp directory.
So everytime I save a JSP file I want it saved in both my current version controlled source location as well as in the directory where my Tomcat application is deployed.
I can't use symlinks because I use a windows machine and the destination location is a directory in Linux box that is exported through Samba.
Something like this should work:
(add-hook 'local-write-file-hooks 'my-save-hook)
(defun my-save-hook ()
"write the file in two places"
(let ((orig (buffer-file-name)))
(write-file (concat "/some/other/path" (file-name-nondirectory orig)) nil)
(write-file orig nil)))
For more on local-write-file-hooks see this answer.
Obviously customize the file name created in the first call to 'write-file.
Given the problem you are trying to solve is to deploy changes immediately, I would suggest writing a script (in your case a batch file) that invokes rsync with the appropriate options. You could either run this in the after-save-hook (which is probably overkill) or assign a hotkey to run it for you when you have made a set of changes that you want to test. Something like:
(global-set-key 'f11 (shell-command "c:/dev/deploy_to_test.bat"))
where the script would look like this:
rsync -avz --del c:/dev/mywebapp z:/srv/tomcat/mywebapp
This is probably better than saving the same file in multiple places, as it ensures the deployment directory always matches what you have in your source repository.
Probably a more general solution is a hook similar to this Gist: https://gist.github.com/howardabrams/67d60458858f407a13bd :
(defun ha/folder-action-save-hook ()
"A file save hook that will look for a script in the same directory, called .on-save. It will then execute that script asynchronously."
(let* ((filename (buffer-file-name))
(dir (file-name-directory filename))
(script (concat dir ".on-save"))
(cmd (concat script " " filename)))
(write-file filename nil)
(when (file-exists-p script)
(async-shell-command cmd))))
(add-hook 'local-write-file-hooks 'ha/folder-action-save-hook)
Essentially, on any file save, if the directory where the file is being saved has an executable script called .on-save, it will execute that script with the name of the file being saved.
This allows you to specify an rsync command (or one or more other commands) on a per-directory basis. Granted, this could be expanded to walk up a directory tree looking for this sort of pattern.

Dired copy asynchronously

Is there a way to modify/tell dired to copy files asynchronously? If you mark multiple files in dired and then use 'C' to copy them, emacs locks up until every file is copied. I instead want this copy to be started, and for me to continue editing as it goes on in the background. Is there a way to get this behaviour?
EDIT: Actually, C calls 'dired-do-copy' in dired-aux, not in dired itself. Sorry for any confusion.
I think emacs is mostly limited to a single thread - so this may not be directly possible through standard dired commands such as 'C' copy.
However, there is a dired command "dired-do-shell-command" which calls out to a shell to do the work in the background. If you select the files you want to copy and then use key '!' (this runs dired-do-shell-command) then type 'cp ? [destination]' (possibly can use 'copy' if you are on windows). I haven't tested this - so see help on "dired-do-shell-command" for full details.
See also the Emacs function dired-do-async-shell-command.
For an even more generic solution see https://github.com/jwiegley/emacs-async with which you also can evaluate arbitrary Emacs Lisp code through call to a separate Emacs process (which of course incurs a bit of extra latency). More specifically regard file operations see the file dired-async.el in this repo.
Also note that there is work on threading in Emacs under the working name Concurrent Emacs but it's not there yet. See http://www.emacswiki.org/emacs/ConcurrentEmacs for details.
I found this answer quite helpful: https://emacs.stackexchange.com/a/13802/10761. Reading that answer shows that you can make it so that dired will copy with the scp method instead of the ssh method (the latter initially encodes the file with gzip and that can be quite slow). The scp method will only copy with the scp program when the file is larger than tramp-copy-size-limit (which is 10240 by default). Using this scp method in conjunction with dired-async-mode is very nice, as it will not only copy quickly with scp, but it will also do it asynchronously and out of your way.
Also, I think this is useful: https://oremacs.com/2016/02/24/dired-rsync/. It provides this snippet of code to use rsync to copy files in dired:
;;;###autoload
(defun ora-dired-rsync (dest)
(interactive
(list
(expand-file-name
(read-file-name
"Rsync to:"
(dired-dwim-target-directory)))))
;; store all selected files into "files" list
(let ((files (dired-get-marked-files
nil current-prefix-arg))
;; the rsync command
(tmtxt/rsync-command
"rsync -arvz --progress "))
;; add all selected file names as arguments
;; to the rsync command
(dolist (file files)
(setq tmtxt/rsync-command
(concat tmtxt/rsync-command
(shell-quote-argument file)
" ")))
;; append the destination
(setq tmtxt/rsync-command
(concat tmtxt/rsync-command
(shell-quote-argument dest)))
;; run the async shell command
(async-shell-command tmtxt/rsync-command "*rsync*")
;; finally, switch to that window
(other-window 1)))
(define-key dired-mode-map "Y" 'ora-dired-rsync)

Open a file with su/sudo inside Emacs

Suppose I want to open a file in an existing Emacs session using su or sudo, without dropping down to a shell and doing sudoedit or sudo emacs. One way to do this is
C-x C-f /sudo::/path/to/file
but this requires an expensive round-trip through SSH. Is there a more direct way?
[EDIT] #JBB is right. I want to be able to invoke su/sudo to save as well as open. It would be OK (but not ideal) to re-authorize when saving. What I'm looking for is variations of find-file and save-buffer that can be "piped" through su/sudo.
The nice thing about Tramp is that you only pay for that round-trip to SSH when you open the first file. Sudo then caches your credentials, and Emacs saves a handle, so that subsequent sudo-opened files take much less time.
I haven't found the extra time it takes to save burdening, either. It's fast enough, IMO.
Tramp does not round-trip sudo via SSH, it uses a subshell. See the manual: https://www.gnu.org/software/tramp/#Inline-methods
Therefore, I recommend that you stick with TRAMP.
If you use helm, helm-find-files supports opening a file as root with C-c r.
Not really an answer to the original question, but here's a helper function to make doing the tramp/sudo route a bit easier:
(defun sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))
Your example doesn't start ssh at all, at least not with my version of TRAMP ("2.1.13-pre"). Both find-file and save-buffer work great.
At least for saving, a sudo-save package was written exactly for that kind of problem.
I recommend you to use advising commands. Put this function in your ~/.emacs
(defadvice ido-find-file (after find-file-sudo activate)
"Find file as root if necessary."
(unless (and buffer-file-name
(file-writable-p buffer-file-name))
(find-alternate-file (concat "/sudo:root#localhost:" buffer-file-name))))
(works only locally. Need to be updated to work correctly via tramp)
A little bit extended Burton's answer:
(defun sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))
(add-hook 'dired-mode-hook
(lambda ()
;; open current file as sudo
(local-set-key (kbd "C-x <M-S-return>") (lambda()
(interactive)
(message "!!! SUDO opening %s" (dired-file-name-at-point))
(sudo-find-file (dired-file-name-at-point))
))
)
)
Ugh. Perhaps you could open a shell in Emacs and exec sudo emacs.
The problem is that you presumably don't just want to open the file. You want to be able to save it later. Thus you need your root privs to persist, not just exist for opening the file.
Sounds like you want Emacs to become your window manager. It's bloated enough without that. :)
I find sudo edit function very useful for that. After opening a file, press s-e to have sudo access to edit/save the file.