Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind File -> Open File with GUI dialog to C-o as global-set-key

I want to bind GUI dialog File -> Open File to Ctrl + o

I can (global-set-key (kbd "C-o") 'find-file) but I want it exactly with gui.

How can I do it?

like image 661
cnd Avatar asked Oct 21 '14 09:10

cnd


1 Answers

File -> Open File is just a GUI binding to find-file.

By binding it to "C-o", you can then open a file using "C-o". However, this will only bring up the standard find-file interface, which uses the echo area.

In order to also get a GUI dialogue box, you need to get emacs to think that find-file has been clicked, rather than invoked by keyboard. The solution to that can be found in Emacs M-x commands for invoking "GUI-style" menus.

Putting the two together (i.e. put them in your .emacs file and evaluate them):

(global-set-key (kbd "C-o") 'find-file)

(defadvice find-file-read-args (around find-file-read-args-always-use-dialog-box act)
  "Simulate invoking menu item as if by the mouse; see `use-dialog-box'."
  (let ((last-nonmenu-event nil))
     ad-do-it))

Note, C-o is already bound to open-line - which will 'insert a newline and leave point before it`.

like image 75
Squidly Avatar answered Sep 29 '22 04:09

Squidly