Emacs: disable automatic file search in ido mode - emacs

I use ido mode. Here is how I set in .emacs
(require 'ido)
(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(ido-mode t)
When I open a file, I do C-x C-f my_file and if it doesn't exist in current directory, emacs will try to search for it in other recent used directories in about a second. However, most of the time I was just trying to create new files. I had to type the file name really fast and then C-j to confirm it. How can I stop ido from doing this?

The following will completely disable the feature:
(setq ido-auto-merge-work-directories-length -1)
I've never seen any value in it, so disabling it completely might make sense for a lot of people.

Here is another option using Ido:
Type C-x C-f as usual.
Find the directory you want to create the new file in using Ido search.
At any moment type C-f again, and Emacs will go back to the old find-file functionality.
You can then type the file name you want and Emacs will create a new buffer. So, if you type C-x C-f C-f file_name RET it will create a buffer called file_name temporarily in the current directory.

I found an easy solution:
(setq ido-auto-merge-delay-time 9)
The time here is in seconds. I could set a very large number to completely disable this feature.

Related

Why doesn't modifying the .emacs file change the behavior of GNU Emacs?

I'm running GNU Emacs 24.3.1 on Windows 7. As mentioned in this manual page, I can type
C-x d ~/ RET
to determine the location of my home directory. In my case, Emacs returns:
e:/EmacsDocs
which, according to Windows Explorer, indeed contains a file .emacs. When I open .emacs with Emacs, the file appears to be empty.
Now, I want to set Emacs so that it is in overwrite mode by default. According to this page, that can be done by adding the following to .emacs:
(setq-default overwrite-mode t)
But when I make this change to .emacs and close and reopen Emacs, Emacs does not overwrite when I select text and start typing. (Rather, it still operates in insertion mode, with new characters inserted before the cursor.)
I also tried, for example, adding this command to .emacs, which according to this page will disable cursor blinking:
(blink-cursor-mode 0)
but again, there is no change when I restart Emacs.
How can I tell if Emacs is actually reading the .emacs in the home directory (upon restarting Emacs)?
You are confusing what Emacs calls overwrite-mode with the requested behavior "overwrite when I select text and start typing".
In Emacs, the latter behavior is called delete-selection-mode, and overwrite-mode means that when you type text (without selecting anything), any existing text that follows the cursor is overwritten by what you type.
Most Emacs users do not turn on overwrite-mode by default, and they just hit the insert key (typically to the left of the home key) to toggle `overwrite-mode on/off when they need/want to.
One way to turn on delete-selection-mode by default is to put one of the following in your init file (.emacs):
(setq delete-selection-mode t)
or
(delete-selection-mode)
Another way is to customize the option delete-selection-mode using M-x customize-option RET delete-selection-mode RET and save the customized value.
Instead of editing the file outside of Emacs, just type
C-x C-f ~/.emacs
and add your configurations there. Save with
C-x C-s
and restart. This should work, since ~ in Emacs defaults to the home directory.

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 to make emacs stay in the current directory

When I start working on a project in emacs, I use M-x cd to get into the project root directory. But every time I use C-x C-f to open a file in one of the subdirectories (like app/model/Store.rb) emacs changes current directory to that of the file. Is there a way to make emacs stay at the root?
How about this? It replaces the regular find-file command with your own which always starts in some "root" directory (customize the find-file-root-dir variable):
(defvar find-file-root-dir "~/"
"Directory from which to start all find-file's")
(defun find-file-in-root ()
"Make find-file always start at some root directory."
(interactive)
(let ((default-directory find-file-root-dir))
(call-interactively 'find-file)))
(global-set-key (kbd "C-x C-f") 'find-file-in-root)
Assuming that you want the working directory of a file to be set to whatever the working directory was before you executed find-file, you could try the following:
(defmacro disallow-cd-in-function (fun)
"Prevent FUN (or any function that FUN calls) from changing directory."
`(defadvice ,fun (around dissallow-cd activate)
(let ((old-dir default-directory) ; Save old directory
(new-buf ad-do-it)) ; Capture new buffer
;; If FUN returns a buffer, operate in that buffer in addition
;; to current one.
(when (bufferp new-buf)
(set-buffer new-buf)
(setq default-directory old-dir))
;; Set default-directory in the current buffer
(setq default-directory old-dir))))
Armed with this macro, go search for operations that set the variable default-directory: M-x find-library files; M-x occur (setq default-directory. After some investigation, you discover that the desired function is called find-file-noselect-1. Also, it looks like set-visited-file-name is also a candidate. So:
(disallow-cd-in-function find-file-noselect-1)
(disallow-cd-in-function set-visited-file-name)
Note
Note that (disallow-cd-in-function find-file) would work just fine, but then if you switched to ido-mode, you'd be opening files with ido-find-file instead of find-file. Both of these functions ultimately use find-file-noselect-1, so hitting that with the macro is a more univeral solution.
Is there a way to make emacs stay at the root?
No, there isn't. C-x C-f always visits starting from the default directory of the buffer you are already vising. The default directory, by default, is the same directory as the file. You can change these (separately for every buffer) using M-x cd.
But that is not what you want. What you should do is C-x b to *scratch* (whose default directory is the same as where you launched Emacs from -- in your words "root"), and then visit a new file. And if you need to do this frequently, just open up a dired in there and work your way thru.
I appreciate I'm not answering your question directly, but I noticed you were more specific in your requirements in one of your comments: "I don't use compile or recompile, I just tend to close files I am not working on, since it takes fewer keystrokes to open a file again".
Have you got ido turned on for buffer switching? If you exclude the directory thing for a moment, switching files or buffers with ido is an identical number of keystrokes (C-x C-f vs C-x b, followed by a few characters in the file name). If you include the directory thing, switching files is more tricky for the precisely the reasons you mention. Sticking with buffers is much easier.
Going a step further, with the help of 'anything.el' it's quite easy to abstract away whether a given file is in a buffer or in a file using the file cache. For example, if you do the following:
(file-cache-add-directory-recursively "/my/ruby/project") ".*\\.rb$")
and run 'anything-for-files' (I have it bound to C-x f) all your open buffers are listed, along with all of the files you've just added to the file cache; isolating a given file usually only takes one or two more characters.
Any file in your project is thus 4 or 5 key presses away, and the directory they are in or whether or not they are in a buffer becomes irrelevant.
Hope that's helpful...
Sorry I haven't worked out the details, but you might be able to add a function to find-file-hook that resets the default directory to whatever you want.

Is there any way to automatically close filename completetion buffers in Emacs?

For example, when you open a file via C-x-C-f, you can TAB complete file names, and if there are more than one possible completions, it will pop open a completion buffer with a list of possible completions. The problem is, after you've opened the file, the window the buffer was in switches back to normal, but it doesn't close. Is there any way I can make those buffers close automatically after the file has been opened?
Sorry to enter really late on this but this is how I do:
;; Remove completion buffer when done
(add-hook 'minibuffer-exit-hook
'(lambda ()
(let ((buffer "*Completions*"))
(and (get-buffer buffer)
(kill-buffer buffer)))))
Tested on GNU Emacs 22.x and 23.x
Although it does not directly solve your problem have you considered ido-mode as a mechanism for opening files?
ido-mode will bind C-x C-f to ido-find-file this allows you to interactively opening files (selecting between name collisions from within the minibuffer C-s and various other nifty features) I find it a much easier method of finding files and it will get rid of the *Completions* buffer altogether.