How to force Emacs to save even if it thinks (no changes need to be saved) - emacs

This happens to me all the time:
I have a file open in emacs,
I save it ('save-buffer),
the file changes on disk (or get's deleted, moved, etc.)
but I want it back, so I try to save again in emacs ('save-buffer) and instead of saving it says "(no changes need to be saved)" and does nothing.
Is there a different function, or a setting, that I can use to force emacs to save?

Wrap a function around save-buffer that marks the buffer modified first:
(defun save-buffer-always ()
"Save the buffer even if it is not modified."
(interactive)
(set-buffer-modified-p t)
(save-buffer))

You can save as, with C-x C-w. That should save unconditionally. You can also just type a space then backspace over it. Emacs is smart enough to realize that if you undo everything you've done so far the buffer has no changes, but if you make changes and then manually reverse them it will consider the buffer to have been changed.

You can mark the current buffer as modified using the Emacs-Lisp function not-modified with a prefix arg, bound to:
C-u M-~
The answer above won't work if you don't call the new function directly.
If you want to seamlessly change emacs saving behavior. The best solution is to create an advice:
(defadvice save-buffer (before save-buffer-always activate)
"always save buffer"
(set-buffer-modified-p t))

As a slight alternative to scottfrazer's answer:
(defun my-save-buffer-always-sometimes (prefix)
"Save the buffer even if it is not modified."
(interactive "P")
(when prefix
(set-buffer-modified-p t))
(save-buffer))
This was you can force it when you want to with a prefix (C-u C-x C-s) but not unnecessarily change a file otherwise. The last modified timestamp is very useful (e.g. source code control) that it seems a shame to change it arbitrarily. YMMV, of course.

A similar problem brought me online to look for a solution. Then it hits me that all I have to do is type a space (or any character) and delete it, which marks the buffer as changed. Then I can type C-x C-s as normal. Maybe not sophisticated or advanced, but it works.

Like Tagore Smith said, you can force Emacs to save the buffer with C-x C-w.
If you're using Evil mode, you can also achieve this behavior by typing :w! in normal state. Unlike C-x C-w, :w! will not prompt you for the filename to save to.

Related

How can I get back the "natural" order in the Buffer List?

With C-x C-b the buffer list is displayed. First in it's natural order with most recently used buffers on top, and buried buffers at the bottom.
There, I can now sort the Buffer by name, size, mode and file. But once I click on such an option I cannot go back to the original ordering.
Also killing the buffer and recreating it does not change that order. (Using 25.2)
So how can I get that ordering back without restarting emacs?
There is another mode that's nowadays built-in to Emacs that can be used to display the buffer list: ibuffer-mode.
If you are not using it already, you can experiment with M-x ibuffer and find out its capabilities with C-h m. Note that in particular, it can sort the buffer list in various ways, one of which is by recency with s v, which is what the OP asked for; but note also that it has many other ways to sort that make it very flexible.
Once you are convinced that that's the way to go, you can redefine the C-x C-b keybinding in your init file with:
(global-set-key (kbd "C-x C-b") 'ibuffer)
I did that a long time ago and have never looked back. IMO, it should be the default: that may come to pass, but AFAIK that is still not the case.
Sorting order goes away as expected when I kill buffer named *Buffer List* and reopen using C-x C-b (Using emacs 27.1 & 28.1). From the EmacsWiki
Once sorted, there is no nice way to restore the default MRU sort. You
have to ‘kill-buffer’ the buffer menu buffer, and then re-open it.
(Sorting sets the variable ‘tabulated-list-sort-key’ in
tabulated-list.el. Its default is ‘nil’. No way to restore the nil
value is provided.)
May be worth adding a custom function which can disable the sort without closing the buffer.
(defun disable-buffer-sort()
(interactive)
(save-excursion
(set-buffer (get-buffer "*Buffer List*"))
(setq tabulated-list-sort-key 'nil)
(revert-buffer)
))
(disable-buffer-sort)
As #NickD mentioned in other answer ibuffer-mode is also a good alternative with more features.

Emacs ispell goto previous word?

As I'm using M-x ispell to check LaTeX code
I use SPC to skip a lot of entries that should not be corrected.
But then I sometimes skip an actual misspelled word.
What's the key to go back with ispell to previous word?
I checked the source code of ispell.el, and unfortunately, there seems to be no key-binding for this (the keys are actually hardcoded in the function ispell-command-loop). As a quick hack, if you don't mind your buffer-local mark-ring to be cluttered, you could do something like this:
(defadvice ispell-command-loop (after leave-breadcrumbs activate)
"Leave a trail in the mark-ring when waiting for user input"
(push-mark (point) t))
Then you can always go back to previous errors with C-u C-SPC. Alternatively, you can create your own mark-ring for this function.

Open multiple instance of a file-

How can I open multiple different instances of file in Emacs? That is to say, the instances are totally independent of each other but write to the same file.
I actually want to have a reference to the original file in one buffers side-by-side with the buffer in which I will be editing while referring to the original content. I don't find opening a temporary buffer, yanking the entire original content into it and have it side-by-side, as an elegant solution.
Other solutions are welcome as well.
I have tried using clone-indirect-buffer and C-x C-v but it doesn't server the purpose.
You can create a new buffer (C-xb*new*) and insert the content of the file to it with C-xifilename.
As I read through the find-file code, I see this warning coded into it:
"The file %s is already visited normally.
You have asked to visit it literally,
meaning no coding system decoding, format conversion, or local variables.
But Emacs can only visit a file in one way at a time.
Do you want to revisit the file literally now? "
Something that would imply that Emacs' code specifically tries to protect against the situation when the same file is visited multiple times, but the buffers are out of sync with each other. However... you could open two copies of Emacs, in which case they would not know about each other visiting the same file and so would allow this situation to happen.
I can understand that the above isn't a very nice option, but it looks like adding that kind of functionality will require some time understanding the reasons behind it being specifically prevented in the first place.
I've tried this:
M-:(switch-to-buffer (find-file-noselect-1 (create-file-buffer (buffer-file-name)) (buffer-file-name) t nil (buffer-file-name) 1))
And it seems like it would work, but I'm not sure of consequences - maybe different major modes may rely on the original Emacs treatment of files and their editing history, so use with care. The last number 1 is the number to be displayed after the file name, as in Foo.bar<1> So, you'd need to change that, if you need more copies.
As mentioned, you might be able to use clone-buffer, although you'll have to let-bind buffer-file-name around the call since clone-buffer otherwise will refuse to clone it. Another option is to do:
M-x set-visited-file-name RET toto RET
C-x C-f thefile RET
C-x b RET
M-x set-visited-file-name RET thefile RET
the last set-visited-file-name should ask you if you really want to do that, but you can answer that you do and Emacs will accept your choice. Arguably, clone-buffer should not reject to do it, so you might like to submit a bug-report asking to make it behave similarly to what set-visited-file-name does.
You may have some luck with clone-buffer, depending on your mode. It has certain limitations that you can read about in the docs.
Otherwise, here's something quick and dirty:
(defun dodgy-clone-buffer ()
"Clone the current buffer. The clone will write to the original file."
(interactive)
(switch-to-buffer-other-window
(eval `(with-current-buffer
;; Create a new buffer or clear an existing one.
(get-buffer-create ,(format "*clone: %s*" (buffer-name)))
(delete-region (point-min) (point-max))
(insert ,(buffer-string))
(setq buffer-file-name ,(buffer-file-name))
(funcall ',major-mode)
(current-buffer)))))

"Touch" current file in emacs

Im emacs, I want to run "touch" on the file referenced by the current buffer (specifically want to change the modification time). I use guard to run some tests after files are changed, but sometimes I want to invoke it manually. I don't care about running the actual shell utility touch so long as mtime is set.
Of course, this assumes that you have a command named touch on your path.
(defun touch ()
"updates mtime on the file for the current buffer"
(interactive)
(shell-command (concat "touch " (shell-quote-argument (buffer-file-name))))
(clear-visited-file-modtime))
In dired-mode there is a touch command bound by default to T. That command isn't so easy to use though because it prompts the user for a timestamp. I suppose that's very general, but it isn't a very convenient way to do what is typically intended by "touch."
Maybe more keystrokes than a custom function, but you can do this out the box with C-u M-~ then C-x C-s.
M-~ is bound by default to not-modified, which clears the buffer's modification flag, unless you call it with an argument (C-u prefix), in which case it does the opposite. Then just save the buffer.
Here is a pure emacs way to do it:
(defun touch-file ()
"Force modification of current file, unless already modified."
(interactive)
(if (and (verify-visited-file-modtime (current-buffer))
(not (buffer-modified-p)))
(progn
(set-buffer-modified-p t)
(save-buffer 0))))
I do it like this: insert a space, end then I delete the space again and save. This changes the mtime.
8 years later, the useful "f" library in melpa provides f-touch.
See: https://github.com/rejeep/f.el

Is there any way to automatically close filename completetion buffers in Emacs?

For example, when you open a file via C-x-C-f, you can TAB complete file names, and if there are more than one possible completions, it will pop open a completion buffer with a list of possible completions. The problem is, after you've opened the file, the window the buffer was in switches back to normal, but it doesn't close. Is there any way I can make those buffers close automatically after the file has been opened?
Sorry to enter really late on this but this is how I do:
;; Remove completion buffer when done
(add-hook 'minibuffer-exit-hook
'(lambda ()
(let ((buffer "*Completions*"))
(and (get-buffer buffer)
(kill-buffer buffer)))))
Tested on GNU Emacs 22.x and 23.x
Although it does not directly solve your problem have you considered ido-mode as a mechanism for opening files?
ido-mode will bind C-x C-f to ido-find-file this allows you to interactively opening files (selecting between name collisions from within the minibuffer C-s and various other nifty features) I find it a much easier method of finding files and it will get rid of the *Completions* buffer altogether.