Hot answers tagged

114

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-cmds.el (defun revert-buffer-no-confirm () "Revert buffer without confirmation." (interactive) (revert-buffer :ignore-auto :noconfirm))


57

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 generally to files that Emacs can't open directly.


55

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'. (auto-revert-mode &optional ARG) Toggle reverting buffer when the file changes (Auto Revert mode). With a prefix argument ARG, enable Auto Revert mode if ARG ...


50

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-v RET to reload your file. It won't prompt unless your buffer has unsaved data. Some non-text modes like image-mode (used for rendering pictures, pdfs, svgs......


31

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 making themselves understood to each other, since they're using the terms in different ways. In brief: "Frames" are to Emacs what "windows" are to everything ...


23

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 want and hit RET to save the file to this new name. You will still have the original file. If you want to go back to it, just visit it again with C-x C-f.


20

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 in the buffer. Generally, you want to use absolute file names. For example: (dired (list "My Dired Buffer Name*" ; The Dired buffer name "/...


17

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-fallback-command. To answer you question, The first C-x C-f will call ido-find-file. The second C-x C-f will fall back to find-file. Then you can do C-a C-y C-k RET ...


17

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 the name of the file the current buffer is based on." (interactive) (insert (buffer-file-name (window-buffer (minibuffer-selected-window))))) The magic is ...


15

@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 use executable-find; here's the definition (taken from Emacs' sources), if you're curious: (defun executable-find (command) "Search for COMMAND in `exec-path'...


15

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 make-directory RET RET to create the directory and its parents. Insert text into the new buffer for new file newfile.txt. C-x C-s to save the file. Emacs ...


15

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. To choose which major mode to use for certain file names, add an entry to the variable auto-mode-alist. Put the following line in your init file: (add-to-...


15

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-Find-Files - SPCff Type in filename (via tab completion etc.). Once file has been selected, there are 3 options I use for opening the file: Option 1. Tab to ...


14

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 then press RET. This will recursively open all directories from the one you select, and below. From here, type % m to mark all files matching a certain file ...


14

TL;DR: With find-file-noselect you have no control about what actually happens, and you may end up with arbitrary minor modes enabling in the buffer, depending on what the user enabled in their init.el. Also, cleanup is hard. Use with-temp-buffer and insert-file-contents instead. If you need specific major or minor modes in the buffer, enable them ...


14

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 file's directory however the OS would." (interactive) (if default-directory (browse-url-of-file (expand-file-name default-directory)) (error "No `...


13

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 test in an if expression. So far, we have spoken of `true' and `false' as values of predicates as if they were new kinds of Emacs Lisp objects. In fact, `false' ...


13

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 you want to use vlf every time you try to open a large file. This means it works with anything that uses find-file such as helm-find-files.


12

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

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 base buffer of buffer, which defaults to the current buffer. If buffer is not indirect, the value is nil. Otherwise, the value is another buffer, which ...


12

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 files under directory whose names match regexp. This function searches the specified directory and its sub-directories, recursively, for files whose basenames (i....


11

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 suggests that ERC can use authinfo. For your random off the shelf MELPA programs you can easily use authinfo to retrieve your password from ~/.authinfo.gpg like ...


10

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 listed in the variable revert-without-query (see the manual for details). Another occasional annoyance of revert-buffer is that it resets the file mode to the ...


10

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 explains how to treat multiple extensions. Alist of filename patterns vs corresponding major mode functions. Each element looks like (REGEXP . FUNCTION) or (...


10

From section 24.3 in the Elisp manual: To copy the contents of a file into a buffer, use the function insert-file-contents. (Don't use the command insert-file in a Lisp program, as that sets the mark.) Searching the Elisp documentation for find-file-noselect it is obvious that it does much more than just reading a file into a buffer. Perhaps people ...


10

@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 leave backup-by-copying set to nil): (setq backup-by-copying-when-linked t) In addition, as pointed out by @Harald you can set back-up-by-copying as a file-local ...


10

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-file-name)) This will return a list like this: (nil 1 "lh" "users" (20614 64019 50040 152000) (20000 23 0 0) (20614 64555 902289 872000) 122295 "-rw-rw-...


9

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 that session closes you can edit any file with those permissions by prefixing with the existing /sudo:: command. See http://www.gnu.org/software/tramp/ for ...


9

My way wasn't shown in the other thread: M-! open . RET. open is a general OS X command which will cause the system to open a file or directory in the default application. . is the current directory. I don't do this very often, so I find it faster to type the above incantation than to write a function and map it somewhere. M-x open-in-finder would take ...


9

What you are looking for is find-file-hook: (add-hook 'find-file-hook 'my-project-hook) (defun my-project-hook () (when (string= (file-name-extension buffer-file-name) "ts") (typescript-mode) (tss-setup-current-buffer)))


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