How to get information about current buffer/file in emacs? - emacs

When working on a buffer (that maps to a certain file) , how to get info about it?
Like Path on disk, size, ...

M-x dired RET

Additionally, there's dired-x which has dired-jump - this allows you to go straight to the file you're visiting. dired-x.el appears to be shipped with my emacs-22.1, so it should suffice to say
(require 'dired-x)
in your ~/.emacs. That installs the binding C-x C-j for dired-jump.

C-x C-b opens Buffer List, which includes path and size info.

If you just want the path and don't want to open dired mode, following will display the full path in the mini buffer and copy it to clipboard. I find it very useful. Put this in your .emacs
(global-set-key (kbd "<f8>") 'copy-buffer-file-name)

Command describe-file (bound to C-h M-f), here:
help-fns+.el -- Help+
In Dired+, just hit C-h RET to invoke describe-file on the current line.

Related

How to open a directory of current buffer that is opening a file using just one command in Emacs?

Is there any simple command to do this? I'm tired to type C-x C-f ENTER.
If you (require 'dired-x) in your init file (or alternatively follow the autoloading instructions1), you can use C-xC-j to call dired-jump, which not only does what you want (in both file-visiting buffers and also in dired buffers), but also places point on the dired entry for the file or directory that you have just come from, which can be incredibly convenient.
1 C-hig (dired-x) Optional Installation Dired Jump RET
There isn't a single command to do that as far as I know, but since you are using emacs you can easily define one. Add these to your init file
(defun my-open-dired-here ()
(interactive)
(dired default-directory))
The above defines a command to open dired in current buffer's directory, this is what you get when you do C-x C-f RET in vanilla emacs.
You can bind the above command to a key of your choice I am binding it to F6
(global-set-key (kbd "<f6>") 'my-open-dired-here)
Now when you press F6 you will get dired opened in current buffer's directory.

Reload .emacs for all active buffers

A question already has been asked how to reload a .emacs file after changing it.
The proposed solutions were to use M-x load-file or M-x eval-region RET on the changed region.
Neither of these solutions affect other open buffers for me. Is there a way to reload the .emacs file for all open buffers?
I should also note that the M-x load-file does not have the desired effect for reasons outlined in the comments to that answer.
Your .emacs file is a global configuration that gets evaluated once only. It does not get applied to each buffer individually.
How you actually achieve what you want is really going to depend on what those .emacs changes are. Some elisp will only take effect the first time it is evaluated; or when a buffer changes major modes; or when a file is loaded; etc, etc...
If you want to reload some or all of the file buffers, ibuffer makes that pretty easy:
M-x ibuffer RET to start ibuffer (I recommend binding this to C-xC-b).
/f.RET to filter by filename regexp . so as to match any filename.
m (on [default]) to mark all filtered buffers.
V (uppercase) to revert all marked buffers.
or you could replace steps 2+3 with M-x ibuffer-mark-by-file-name-regexp RET . RET. You may wish to bind that command to *f:
;; Bind `ibuffer-mark-by-file-name-regexp' to *f
(eval-after-load "ibuffer"
'(define-key ibuffer-mode-map (kbd "* f") 'ibuffer-mark-by-file-name-regexp))
type *c-h to see all the other ibuffer-mark-* commands which are bound by default.
This may strike you as brute force, but
it will certainly reload your init file (consider alternatives to .emacs)
it will reload all open buffers (provided you are using desktop, which you should)
it is easy
C-x C-c
emacs --debug-init &

How can I reload .emacs after changing it?

How can I get Emacs to reload all my definitions that I have updated in .emacs without restarting Emacs?
You can use the command load-file (M-x load-file, and then press Return twice to accept the default filename, which is the current file being edited).
You can also just move the point to the end of any sexp and press C-x, C-e to execute just that sexp. Usually it's not necessary to reload the whole file if you're just changing a line or two.
There is the very convenient
M-x eval-buffer
It immediately evaluates all code in the buffer. It's the quickest method if your .emacs file is idempotent.
You can usually just re-evaluate the changed region. Mark the region of ~/.emacs that you've changed, and then use M-x eval-region RET. This is often safer than re-evaluating the entire file since it's easy to write a .emacs file that doesn't work quite right after being loaded twice.
If you've got your .emacs file open in the currently active buffer:
M-x eval-buffer
Solution
M-: (load user-init-file)
Notes
you type it in Eval: prompt (including the parentheses)
user-init-file is a variable holding the ~/.emacs value (pointing to the configuration file path) by default
(load) is shorter, older, and non-interactive version of (load-file); it is not an Emacs command (to be typed in M-x), but a mere Elisp function
Conclusion
M-: > M-x
M-x load-file
~/.emacs
Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote.
For that, Ctrl + Alt + X in the Elisp part works just fine.
The following should do it...
M-x load-file
I suggest that you don't do this, initially. Instead, start a new Emacs session and test whatever changes you made to see if they work correctly. The reason to do it this way is to avoid leaving you in a state where you have an inoperable .emacs file, which fails to load or fails to load cleanly. If you do all of your editing in the original session, and all of your testing in a new session, you'll always have something reliable to comment out offending code.
When you are finally happy with your changes, then go ahead and use one of the other answers to reload. My personal preference is to eval just the section you've added/changed, and to do that just highlight the region of added/changed code and call M-x eval-region. Doing that minimizes the code that's evaluated, minimizing any unintentional side-effects, as luapyad points out.
Keyboard shortcut:
(defun reload-init-file ()
(interactive)
(load-file user-init-file))
(global-set-key (kbd "C-c C-l") 'reload-init-file) ; Reload .emacs file
C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el
Define it in your init file and call by M-x reload-user-init-file
(defun reload-user-init-file()
(interactive)
(load-file user-init-file))
I'm currently on Ubuntu 15.04 (Vivid Vervet); I like to define a key for this.
[M-insert] translates to Alt + Ins on my keyboard.
Put this in your .emacs file:
(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))
Besides commands like M-x eval-buffer or M-x load-file, you can restart a fresh Emacs instance from the command line:
emacs -q --load "init.el"
Usage example: Company backends in GNU Emacs
Here is a quick and easy way to quick test your config. You can also use C-x C-e at the end of specific lisp to execute certain function individually.
C-x C-e runs the command eval-last-sexp (found in global-map), which
is an interactive compiled Lisp function.
It is bound to C-x C-e.
(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)
Evaluate sexp before point; print value in the echo area.
Interactively, with prefix argument, print output into current buffer.
Normally, this function truncates long output according to the value
of the variables ‘eval-expression-print-length’ and
‘eval-expression-print-level’. With a prefix argument of zero,
however, there is no such truncation. Such a prefix argument also
causes integers to be printed in several additional formats (octal,
hexadecimal, and character).
If ‘eval-expression-debug-on-error’ is non-nil, which is the default,
this command arranges for all errors to enter the debugger.
Although M-x eval-buffer will work, you may run into problems with toggles and other similar things. A better approach might be to "mark" or highlight what’s new in your .emacs file (or even scratch buffer if you're just messing around) and then M-x eval-region.
You can set a key binding for Emacs like this:
;; Reload Emacs configuration
(defun reload-init-file ()
(interactive)
(load-file "~/.emacs"))
(global-set-key (kbd "C-c r") 'reload-init-file)
If you happen to have a shell opened inside Emacs, you can also do:
. ~/.emacs
It may save a few key strokes.

How do I stop emacs dired mode from opening so many buffers?

When I use dired mode to browse around and find a file I want to open in Emacs, dired opens a new buffer for each directory I visit when looking for the file each time I select a directory with Enter, which means I can end up with a lot of buffers I don't want:
. * newer 0 Fundamental c:/work/stackoverflow/batch/mydir/newer
% mydir 302 Dired by name c:/work/stackoverflow/batch/mydir/
% batch 616 Dired by name c:/work/stackoverflow/batch/
% stackoverflow 1017 Dired by name c:/work/stackoverflow/
% work 2545 Dired by name c:/work/
* *scratch* 190 Lisp Interaction
% *Completions* 162 Completion List
* *Messages* 2163 Fundamental
Is there any way to make dired re-use a single buffer? I tried M-x customize-group for group dired but didn't see anything promising in there.
Alternatively, does anyone have a macro to close all open dired buffers?
Use a (dired-find-alternate-file) instead of Enter
Also, see this page:
http://www.emacswiki.org/emacs/DiredReuseDirectoryBuffer
When browsing in dired instead of hitting enter to see a directory use i then it adds that directory to the current buffer.
I've never managed to get toggle-dired-find-file-reuse-dir to work reliably - I still end up with a variety of dired buffers open, and I'm never quite sure how.
Recently I discovered dired-single (http://www.emacswiki.org/cgi-bin/wiki/dired-single.el) which seems to work better for me. If you want it guarantees a single dired buffer, and also has a nice command dired-single-magic-buffer which will take you to the open dired buffer if you have one, and opens one if you don't.
There are some other alternatives if it isn't the multiple dired buffers per se that annoy, so much as the way they pollute your buffer lists. For example, elscreen.el has a dired plugin that keeps the dired buffers in their own tab, and the excellent ibuffer mode allows you to group dired buffers together when you list buffers.
Hope that helps!
Simon
From Xah Lee, at http://ergoemacs.org/emacs/emacs_dired_tips.html
;; Make dired open in the same window when using RET or ^
(put 'dired-find-alternate-file 'disabled nil) ; disables warning
(define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file) ; was dired-advertised-find-file
(define-key dired-mode-map (kbd "^") (lambda () (interactive) (find-alternate-file ".."))) ; was dired-up-directory
In Emacs 28, you can just (setf dired-kill-when-opening-new-dired-buffer t). This works for either dired-find-file (RET) or dired-up-directory (^).
Dired+ lets you do this optionally, and it lets you toggle it on/off anytime.
See also http://www.emacswiki.org/emacs/DiredReuseDirectoryBuffer.

Reload configurations without restarting Emacs

How do I load the edited .emacs file without restarting Emacs?
M-x eval-buffer
I usually use M-x load-file. But be aware that some initialization is only done the first time through. Things like libraries that set their defaults when loaded, but don't get reloaded the second time through. Its always a good idea to start up emacs from scratch as a final check that everything works ok.
In the *scratch* buffer, type:
(load-file user-init-file)
Then press C-x C-e to evaluate the expression.
M-x load-file and then choose the .emacs file should also work
M-x load-file ~/.emacs
eval-buffer when the .emacs file is opened
eval-region when you want apply selected lines
C-x C-e evaluates the preceding expression
M-x load-file ENTER
~/.emacs
ENTER
(source)
Open the .emacs file, select its contents and hit C-x,C-e
you can use C-x C-e which will evaluate an s-expression. Make sure the cursor is at the last parenthesis of the elisp code.
I use and recommend restart-emacs package on melpa