Hot answers tagged

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-...
user avatar
  • 24.2k
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 ...
user avatar
  • 2,137
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'. (...
user avatar
  • 831
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-...
user avatar
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 ...
user avatar
  • 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 ...
user avatar
  • 22.3k
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 ...
user avatar
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 ...
user avatar
  • 3,674
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 ...
user avatar
  • 69.1k
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. ...
user avatar
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 ...
user avatar
  • 10.8k
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 ...
user avatar
  • 5,305
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 ...
user avatar
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-...
user avatar
  • 6,652
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-...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 69.1k
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
14 votes

Spacemacs: kill buffer and kill window in one command?

spacemacs binding for kill-buffer-and-window: Cx,4,0
user avatar
  • 261
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 ...
user avatar
  • 3,954
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 ...
user avatar
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 ...
user avatar
  • 22.3k
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&...
user avatar
  • 22.3k
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 ...
user avatar
  • 20.1k
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 ...
user avatar
  • 2,622
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. ...
user avatar
  • 11.3k
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-...
user avatar
  • 20.1k

Only top scored, non community-wiki answers of a minimum length are eligible