Copying file in emacs using existing file name as a "base" - emacs

When in a Dired buffer, is it possible to copy an existing file to a new file using the existing files name as a "base" and then edit it to create a new file name? I know you cannot copy a file to a new file with the exact same name. What I have been doing is copying the old file name to a register, using Shift-C on the old file name, and then pasting and editing the new file name. I'm looking for a way to do this that allows me to skip the step of copying the old file name to a register.

Just use C in Dired to copy the file of the current line to another file. At the prompt, use M-n to retrieve the existing file's name as the default, and edit it.
Throughout Emacs, M-n when you see a prompt retrieves the default value for your input. For file-name input in Dired, M-n typically retrieves the name of the file on the current line.

Related

emacs as an external editor for other applications (Houdini, Joplin)

I am trying to use emacs as an editor with other applications which allow people to open text in an editor (Sublime in this case), save it, and see it updated in the application. For example, in Houdini, a 3D software, I can type code in an external editor (in this case, Sublime), modify, save... and see it update in the application (Houdini). When I use emacs, it doesn't work. As an example, here I am adding a line of text using Sublime in Houdini:
Once I save and close, the text is updated in Houdini, and I can continue working:
Try as I might, I can't get this to work in emacs. I am sure the file has the same name, and when I save, it confirms the right file path.
What am I missing? I have run into the same problem with an application called Joplin: Sublime works, emacs does not.
A wild guess: Emacs has different behaviour when saving files than Sublime, and Houdini gets confused by that.
When you save a file in Emacs, Emacs creates a backup of the previous contents of the file, suffixing the file name with a tilde. By default it does this by renaming the existing file to the backup name, and then writing the contents to the real filename, thereby creating a new file.
(You can observe this by running ls -i before and after: the backup file will have the inode number that the main file had previously. Note that Emacs doesn't make backups after the first save during the same editor session, so you might need to restart Emacs or kill the buffer with C-x k to see this.)
I suspect that Houdini keeps the file open while Emacs is editing it, and so when you save the file from Emacs, the file that Houdini has open is actually the backup file.
You can configure Emacs to make backups by copying instead of renaming by setting the variable backup-by-copying to t. Add this to your ~/.emacs file (creating it if it doesn't exist):
(setq backup-by-copying t)

How does emacs compile the file and edit it?

There is this feature in emacs. Whenever you compile your code (filename: hello.cpp) run the program and then you edit your code then emcas will store you previous unedited code in another file named hello.cpp~
. hello.cpp~ will always contain first code you compile and whenever you write you edit your code in emacs it will produce file name .#hello.cpp. It automatically remove .#hello.cpp when you save that code but hello.cpp~ will remain same. .#hello.cpp file contain info. such as username#DESKTOP-FN20BRU.13000:1597860074. So anybody can please explain me this working process of emacs?
Emacs does the following when you edit a file filename
#: when you modify the file (in memory), Emacs creates a # file (on disk) and keeps updating it with your changes, until you save the edited version. This draft, on disk, can be recovered in case Emacs or the computer crash before you had time to save your changes.
~: as soon as you save the edited version, Emacs renames first your old version on disk (the one when you opened Emacs) adding a trailing ~ to the filename giving filename~ (it does this only the first time you save within the current session), then it overwrites filename with the modified version, and finally removes the # draft on disk.
Then, when you make further changes, another # file is created, etc.

Emacs Dired - C-x C-f to create a new file gives me suggestions of existing files

I'm trying to create a file in dired mode in emacs. I am in the right directory and when I press C-x C-f as suggested elsewhere on SO and type 'img' (that's the name of the file I want to create), it tries to find existing files from other directories including the pattern 'img'. Then I'm stuck as if I press enter, it'll open the first suggested file containing the pattern 'img' from other directories, TAB will just go over the suggestions.
Please advise.
You are probably using ido-find-file with which you can interactively select a file by typing a substring of that file name.
If you want to temporarily disable this feature (i.e. for your current search only) just press C-f before typing the name of your new file (i.e. immediatly after C-x C-f).

emacs bookmark+ :How to create a file specific bookmark file?

bookmark+ package provides a (bmkp-this-file-bmenu-list) function. This, I suppose, loads a file specific bookmark file and filters only the bookmarks, which relate to the file.
Question: how to create this specific bookmark file for a specific file?
The result should be a filtered list of bookmarks, when using C-x p , command (which is bound to (bmkp-this-file-bmenu-list)
).
Edit: I use only one default bookmark file ~/.emacs.d/bookmarks. This file has some bookmarks for ~/.emacs file. Now, when I visit, say, ~/.emacs file, then run C-x p ,, I get the following error: bmkp-this-file-bmenu-list: No bookmarks for file ~/.emacs.
No, actually, command bmkp-this-file-bmenu-list does this (from the doc string):
Show the bookmark list just for bookmarks for the current file.
Set `bmkp-last-specific-file` to the current file name.
If the current buffer is not visiting a file, prompt for the file name.
It shows the *Bookmark List* display, listing only and all bookmarks that target the current file.
So if you use this command in a file buffer then you see displayed, in buffer *Bookmark List*, all of the bookmarks to the current file, and only those bookmarks.
This has nothing to do with using a different bookmark file.
Beyond what this command does, it's not clear to me what behavior you would like. What, for instance, do you mean by a "specific bookmark file for a specific file"?
You can create a different bookmark file using bookmark+ with M-x bookmark-load. You will be prompted for a filename. You can either merge bookmarks from different files or replace the current bookmark set entirely with this fileset by supplying a prefix argument.
Saving bookmarks bookmark-save will write all current bookmarks to the current value of the variable bmkp-current-bookmark-file , or if you supply a prefix arg you can choose a bookmark filename to save them to.
If you want a set of bookmarks in a distinct file, associated with particular files you could perhaps achieve this by running bookmark-load , and bookmark-save with prefixes. You could probably even automate this with hooks to match your editing contexts, although I expect that would be a little fiddly to achieve.
It sounds like your question might be rooted in some confusion about existing bookmark behaviour, and perhaps you don't even need to maintain separate bookmark files.

"Selecting deleted buffer" error in elisp

In Emacs, I'm trying to create a file on the fly (using with-temp-file), set some file local variables, write the local variables to the file and save it. After some modifications to the newly created file, I delete the file and kill the corresponding buffer. So far it's working.
Now after deleting the file, I do the same process (create file on the fly, open the buffer, etc). But this time when I try to do something on the newly created buffer using with-current-buffer I'm getting this weird error:
Selecting deleted buffer
buffer-live-p on the new buffer returns t.
What could have gone wrong?
I'm using a custom build of Emacs 24.3 on RHEL 6.