how to avoid the buffer from closing in emacs? - emacs

Assume now I have opened File A and then I type C-X-3. Now I have another window with File A.
However, if I want to replace File A with File B in the new window by typing C-X C-V, the old window will replace File A with Scratch. How to avoid old window doing this extra action? I mean in fact I do not want to close File A in the old window.
Thanks.

C-x C-v is usually bound to find-alternate-file, which closes the previous buffer. Seems, this is not what you want.
To open a new file, use C-x C-f (find-file), to select an existing buffer, use C-x C-b.
Generally, to find out what a keybinding does, use C-h k and then type the key combination you are curious about. This will give you the name of the command and a short description. You can then do C-h F (i.e. upper case F) and type in the name of the command: this will show you the relevant section of the manual, in your case various ways of opening a file.

Related

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).

How to copy/select a whole file or buffer in Emacs?

Is there any command to select the whole file contents in Emacs?
For example, Control+a selects whole contents of a file in Notepad, Notepad++, etc.
I can select whole contents using the mouse, but it's inconvenient for large files. I found the basic Emacs commands here and here, but could not find what I am looking for.
C-x h will select the entire buffer.
You can search for help within Emacs using the built-in help system.
C-h f will look for help for specific functions. In this case, you could have searched for whole-buffer to find mark-whole-buffer.
Other help commands of interest would be:
C-h m to show available commands for the current modes
C-h v for help related to variables
C-h k to find which functions keys are bound to
C-h w to see which key bindings are defined for a given function
C-h ? to see which other options are available.
Note that C-x h will only highlight all the text. To actually copy the selected text, you must use C-w for cut (kill-region) or M-w for copy (kill-ring-save).
I use CUA, so I didn't like mark-whole-buffer because it doesn't use a temporary region.
After much messing about, I finally achieved this using a keyboard macro saved to a function:
Define a macro which selects the whole buffer
Run kmacro-name-last-macro to name the macro
Use insert-kbd-macro to script it out
Now you can copy it into your config & map it to a key, like C-a
Copying and selecting are related, but distinct, actions. A file and a buffer are also related, but distinct.
To copy a buffer, consider using jac.el. It handles both the "copying" and dealing with modes.

How to make emacs allow opening one buffer in different frames?

I have two frames of the same Emacs instance (got them by C-x 5 2) and a file opened in one of these frames. Then I try to show this file in a buffer in another frame (by C-x b) and nothing happens.
Actually, I think it shows this file in a buffer in the first frame, but this is not what I want it to do. I want it to show this buffer in both frames.
When there is only one frame and some windows in it, opening one buffer in different windows is no problem. I want the same behaviour but with several frames.
Most likely you are using iswitch, as what you describe is its default behavior.
You can change it by doing M-x customize-variable RET iswitchb-default-method. probably you want to choose option samewindow
The ido equivalent variable , in case you are using it, is ido-default-buffer-method (ido defaults to raise-frame, that does not match with your observed behavior).
You may find if you are using one or the other using the way #Olaf describes.
Maybe, you have C-x b redefined. I have the same behaviour with iswitch-buffer. But when I enter M-x switch-to-buffer, it shows the buffer in the second window.
You can find out, which command is executed with C-h c C-x b.

When I create one file in Emacs, I can not open the directory

I use C-x C-f and C-X C-s to save a file to directory "test", then I can not open the test directory with "tab", it says "Dired (directory): ~/test.....".
I want to know what is the wrong and the correct method to create a file in emacs, thanks!
Emacs distinguishes between files and buffers. You do all your editing in buffers, which do not necessarily have to be associated with a file (see, e.g., the *scratch* buffer). However, you often want to edit a buffer that is associated with a file. In that case you visit (Emacs lingo for "open") said file with C-x C-f.
Let's say you have a directory "test" in your home directory, and in that directory a single file "foo.txt". If you want to edit the contents of that file, visit it by typing:
C-x C-f ~/test/foo.txt RET
That's easy enough: now you get a new buffer in Emacs which is conveniently named after the file you're visiting ("foo.txt"). If you want to store changes you've made in the buffer back to the associated file, you type C-x C-s, and Emacs will write the contents of the buffer to the file "~/test/foo.txt".
This mechanism also works for files that do not exist when you start editing!
If you type, say,
C-x C-f ~/test/bar.txt RET
You get a buffer associated with a (yet non-existent) file "bar.txt" in the directory "~/test". Again, you can edit that buffer to your liking and then save the buffer with C-x C-s. The first time you do that, a new file is created.
If you want to create a buffer named "baz" that is not associated with any file, type
C-x b baz RET
Since it is not associated with a file yet, typing C-x C-s in that buffer will prompt you to specify a file-name. After saving, the buffer will be associated with the file you specified.
Finally, if you're in a buffer that is associated with a file, but you want to save it to a different file, you can do so with C-x C-w, which will give you the same prompt as in the previous case. Again, the buffer will be associated after saving with the file you provide.
Tab completion does work in all of the commands that ask you for a file name. If you think it doesn't then it might be because the file you think should be completed does not exist (also beware of upper-/lower-case distinctions in file names), or because another file with the same prefix forbids further disambiguation. Tapping TAB twice should list the available completions in a temporary buffer, letting you continue specifying the path name until it is complete.
Dired mode is pretty much orthogonal to all of the above. It is a mode for "editing" directories, i.e., doing file system operations. You invoke it by typing C-x d, which you may have typed accidentally?! It is quite powerful, but also quite complex. See here for its documentation.

Using bookmark to open a file but always in a given position

When using bookmarks to open a file, is there a way to always open at a particular position of the file?
Emacs default behavior when bookmarking with C-x r m is to bookmark the file AND the position. So, if you use that command (bookmark-set) it will have the behavior you're asking for.
If you C-h k C-x r m to view the documentation:
C-x r m runs the command bookmark-set,
which is an interactive compiled Lisp
function in `bookmark.el'.
It is bound to C-x r m,
.
(bookmark-set &optional NAME
NO-OVERWRITE)
Set a bookmark named NAME at the
current location.
It can even set two positions (the region), if you use Bookmark+.