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

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

Related

how to avoid the buffer from closing in 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.

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.

how to create a list of favorite files in Emacs?

In GNU Emacs, I want to create a keystroke that will show me a list of my favorite files (not recent files) so that I can easily open them.
Is list-bookmarks the way to do this? Is there a better way to create and save such a list?
I think the bookmarks feature gives you what you want, and in fact a little bit more, since you can name bookmarks and bookmark positions in files.
C-x r m Add a bookmark for the current file at the current point
C-x r b Open a bookmark by name
C-x r l List all bookmarks

How do I set marks in Emacs à la Vim?

I'd like to be able to set multiple marks in Emacs like Vim does. In Vim you might press m b and that would set a mark at that line in the file. Later pressing ' b will move your cursor back to that line. You can make multiple marks with m{a-zA-Z}. Is there a way to have multiple marks like this in Emacs?
From Emacs documentation :
C-x r SPC r
Record the position of point and the current buffer in register r (point-to-register).
C-x r j r
Jump to the position and buffer saved in register r (jump-to-register).
But if you want your positions to persist automatically from one Emacs session to the next, you should use Bookmarks :
C-x r m RET
Set the bookmark for the visited file, at point.
C-x r m bookmark RET
Set the bookmark named bookmark at point (bookmark-set).
C-x r b bookmark RET
Jump to the bookmark named bookmark (bookmark-jump).
C-x r l
List all bookmarks (list-bookmarks).
M-x bookmark-save
Save all the current bookmark values in the default bookmark file.
You can use what Emacs calls registers. The documentation explains them better than I can.
Try the mark ring for quick marks:
C-spaceMake a mark at current position; also, add position to mark ring.
C-xC-xJump back to previous mark.
C-UC-spaceCycle through marks in the mark ring.
I used Vim for a decade before switching to Emacs a few years ago, and while the registers and bookmarks looked good at first, the mark ring is what I actually end up using 90% of the time. Usually I just use the C-space, C-x C-x, but cycling works, too.
Btw, realize that doing large non-arrow key movements like M-v will often add a mark to the mark ring. Just practice these key combos and you'll likely find them sufficient for most tasks.
Radix already did a good job explaining the registers and bookmarks, and those are useful for locations in files that will be referred to often or need annotation.
Vanilla Emacs makes you specify a name for each bookmark. What you want, it sounds like, is a quick way to create bookmarks without naming them -- just hit a key. You want autonamed bookmarks, available with Bookmark+. You can even have them be automatically highlighted, if you like (the fringe or the line).
Have a look at this: http://www.cs.utah.edu/dept/old/texinfo/emacs18/emacs_13.html