172
votes
Accepted
How do I reload a file in a buffer?
M-x revert-buffer will do exactly what you want. It will still ask for confirmation.
Another option (my favorite) is the below function:
;; Source: http://www.emacswiki.org/emacs-en/download/misc-...
114
votes
Accepted
Edit file with super-user rights
Use C-x C-f and type out /su::/etc/hostname or /sudo::/etc/hostname as applicable.
This uses the TRAMP package, which is distributed with Emacs. This package provides access to remote files, and more ...
73
votes
How do I reload a file in a buffer?
There is also auto-revert-mode which does it automatically and gives you feedback.
From the doc string:
auto-revert-mode is an interactive autoloaded compiled Lisp function
in `autorevert.el'.
(...
65
votes
How do I reload a file in a buffer?
Another option, which I use, is find-alternate-file bound to C-x C-v. This opens a file reusing your current buffer.
By default, it points to the file you're currently on, so you can just type C-x C-...
48
votes
Accepted
What's the difference between a buffer, a file, a window, and a frame?
Emacs predates contemporary window-based operating systems, so its
terminology also predates the current terms. Unfortunately, it means that new users and experienced users sometimes have trouble ...

Dan♦
- 31k
28
votes
Accepted
Save current file with a slightly different name
There's no need to do all that.
Simply:
Still in the original file, hit C-x C-w. You will be prompted for a new filename.
Hit M-n, and the current filename will be inserted for you.
Edit what you ...
27
votes
Ivy disable completion for a command in minibuffer
I think what you're looking for is C-M-j (ivy-immediate-done) in the mini buffer. This function is described as follows in the Ivy manual:
Exits with the current input instead of the current ...
21
votes
Accepted
How can I find the path to an executable with Emacs Lisp?
@Sigma's answer is a good start, but it doesn't filter by executability, nor does it allow for extra suffixes. On windows, for example, running a can invoke a.exe, if that's what's in your path.
So ...
21
votes
Accepted
Programmatically insert files into Dired buffer
C-h f dired tells you the answer. Just pass to dired, as the DIRNAME argument, a list that has as its car the Dired buffer name you want (any string) and as cdr the list of file names you want listed ...
21
votes
Accepted
Run command when opening a file of a specific filetype
In Emacs terminology, these are two different steps:
Associate files with the .ts extension with the major mode typescript-mode.
Run the function tss-setup-current-buffer when Typescript mode starts.
...
20
votes
Recursively list all files and sub-directories
directory-files-recursively
(I think this was added in Emacs 25)
here's the link to the manual: https://www.gnu.org/software/emacs/manual/html_node/elisp/Contents-of-Directories.html
Return all ...
19
votes
Put the current file name in the minibuffer
What existing command gets the current file buffer's file name like this?
Unfortunately there is no ready made command for this, but we can make one as follows:
(defun name-of-the-file ()
"Gets ...
18
votes
Accepted
Recursively open files matching regexp in marked directories
Try this:
C-u C-x d
This let's you enter arguments for how Dired should list files. By default it says -al at the prompt. Add R to that, ending up with -alR. Type RET.
Select/type a directory and ...
17
votes
Accepted
How to tell Ido to get out of the way when I can already just paste the file name?
While an ido command is active (ido-find-file in this case), you can drop back to the non-ido version of the same command (find-file in this case) using the ido-default binding C-x C-f for ido-...
17
votes
Open file in new window after searching it with helm
I'm using Spacemacs (on Windows 10 if that matters) with the default keybindings. This may differ from your configuration, but I found the following ways to open files via Helm-Find-Files:
Open Helm-...
17
votes
Accepted
Open large files?
I suggest you use the vlf package.
It handles opening larges files in chunks and is very performant, I use it regularly to open files ~2gb.
In your config, require vlf-setup to have vlf ask you if ...
16
votes
Opening a new file whose parent directory doesn't exist yet
You don't need to do that, if that's your question.
C-x C-f /some/new/directory/newfile.txt
Emacs prints a message to let you know that the directory /some/new/directory/ does not yet exist: Use M-x ...
16
votes
Accepted
What is the easiest way to open the folder containing the current file by the default explorer of the OS?
Using browse-url-of-file should work when given a directory.
You could implement a command that opens the directory of the current file like this:
(defun browse-file-directory ()
"Open the current ...
14
votes
Accepted
How to check whether a buffer is visiting a file?
I'd argue that your usage is idiomatic elisp, since the buffer's name is a perfectly appropriate boolean value in its own right. Quoting from the manual:
There is an important aspect to the truth ...
14
votes
Accepted
How should I get the filename of the original buffer in an indirect cloned buffer?
I keep the Emacs manual and Elisp manual on speed dial for questions like these.
From the Elisp manual on indirect buffers:
Function: buffer-base-buffer &optional buffer
This function returns the ...
14
votes
Spacemacs: kill buffer and kill window in one command?
spacemacs binding for kill-buffer-and-window:
Cx,4,0
12
votes
Edit file with super-user rights
Use find-file with tramp. At the prompt /sudo::/etc/ssh/ssh_config will use sudo and a subshell to open that file with root privileges. When sudo is first used it will prompt for a password, but until ...
12
votes
How do I reload a file in a buffer?
Emacs calls this reverting.
You can revert the current file with M-x revert-buffer. This prompts for confirmation whether the file has been modified or not, except for files that match patterns ...
12
votes
Specify major-mode for files with multiple extensions
The auto-mode-alist variable does what you need. Never bind anything to the major-mode variable.
The documentation explains how you can set major-modes per file extension and, most importantly, it ...
12
votes
Accepted
How can I create a dired buffer listing all open files?
The dired command accepts a list of file-names as argument. Just create one and pass to it:
(dired
(cons
"Open Files"
(sort (remove nil (mapcar #'buffer-file-name (buffer-list)))
#'string&...
12
votes
Accepted
How to prevent Emacs from breaking hard links?
@Hakon solved his own problem with:
(setq backup-by-copying t)
If you prefer to retain the default behaviour, and use back-up-by-copying only for hard-linked files, you can use this instead (and ...
12
votes
Elisp: How to exclude sensitive information from an init file? (ie login credentials)
Emacs comes with auth-source.el. I would not try to roll my own version of it.
auth-source makes it easy to read from ~/.authinfo.gpg. Good programs will already support authinfo. A quick search ...
12
votes
How to prevent creating files starting with .# (lock files)?
How I can prevent generating the lock file .#rest.api.rc?
Set the user option create-lockfiles to nil, but note that this can stop Emacs from being able to detect simultaneous edits to the same file.
...
11
votes
Can I view within Emacs the properties of a file which I already opened?
There doesn't seem to be a command to get this info interactively, although you can use dired as @jrm suggests.
You can get this information with the following elisp code:
(file-attributes (buffer-...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
files × 258buffers × 34
dired × 29
org-mode × 15
find-file × 15
directories × 12
tramp × 11
saving × 11
elisp × 9
microsoft-windows × 8
init-file × 7
hooks × 7
helm × 6
window × 6
permissions × 6
completion × 5
ivy × 5
osx × 4
search × 4
major-mode × 4
functions × 4
projectile × 4
start-up × 4
recentf × 4
spacemacs × 3