Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut to open a specific file in Emacs

Tags:

emacs

I want to be able to use a keyboard shortcut to edit my .emacs file instead of typing Ctrl-XCtrl-F.emacsEnter every time (here's an analogous question regarding Vim). There's probably an obvious way of doing this, but I can't find the Emacs Lisp command to open a file. I'd think it would be something beginning with "open" or "file" but typing M-x and those terms doesn't seem to bring up anything relevant. I tried

(global-set-key (kbd "<f6>") (find-file "~/.emacs"))

but that doesn't work.

like image 539
Patrick Brinich-Langlois Avatar asked Sep 24 '12 02:09

Patrick Brinich-Langlois


People also ask

What command is used to open a file from within Emacs?

Use Ctrl-x f to open a file from within Emacs. Create a new file in the same way as opening a file by specifying the new filename. The new file will not be saved unless specified. Save a file that is currently open by entering the Ctrl-x Ctrl-s command.

What does Ctrl u do in Emacs?

Editing Shortcuts Emacs, as an editor, also provides users with a variety of shortcuts to work with. For repeating a command in the specified number of times, this can be done by hitting the keys Ctrl + u followed by entering the specified number the command will be repeated and then entering the command itself.

How do I set keyboard shortcuts in Emacs?

emacs file. To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET . To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET . See Key Bindings in The GNU Emacs Manual .

What is the C command in Emacs?

Summary of essential emacs commands In the list below, C- means "control key", M- means "meta key" (escape). For meta commands, press the meta key, then the other key. Thus M-f stands for the keyboard sequence "press meta key", " press f".


2 Answers

According to the documentation

(global-set-key KEY COMMAND)

Give KEY a global binding as COMMAND. COMMAND is the command definition to use; usually it is a symbol naming an interactively-callable function.

So you have to use an interactively-callable function:

(global-set-key (kbd "<f6>") (lambda() (interactive)(find-file "~/.emacs")))

Personally I prefer to use emacs registers to store files which I use often. I would store '~/.emacs' in a register:

(set-register ?e (cons 'file "~/.emacs"))

and open it with C-x r j e

like image 133
Oleg Pavliv Avatar answered Sep 28 '22 19:09

Oleg Pavliv


Bookmarking is an excellent solution for this purpose that is packaged with emacs. That way if it's ok for you to see the list of files that you want to open, you can easily browse them. There is also BookmarkPlus which offers variety of options.

M-x list-bookmarks

like image 27
aartist Avatar answered Sep 28 '22 17:09

aartist