I'm attempting to bind this series of commands
C-x RET f undecided-dos
to my keyboard f11 key. So far I've tried many things such as
\C-x RET \f undecided-dos
in my .emacs file but no success. Please show me the correct syntax.
If you can complete a command interactively, you can then query Emacs for what the function you performed is called. Try M-x repat-complex-command and press the up arrow once (or more times if you have completed other commands in the interim) or ask for key binding help:
C-h k C-x RET f
=> set-buffer-file-coding-system
Unfortunately, you can't bind this directly to a keystroke:
;;;; BROKEN
(global-set-key (kbd "<f11>") '(set-buffer-file-coding-system 'dos-undecided))
... because when you try to run that, you run into
Wrong type argument: commandp, (set-buffer-file-coding-system (quote dos-undecided))
You can work around that by specifying an interactive form around it:
(global-set-key (kbd "<f11>")
(lambda ()
(interactive "*")
(set-buffer-file-coding-system 'undecided-dos)))
The "*" argument to interactive says it is only allowed in buffers that you have permission to modify.
I'm learning Clojure. Every day, I open up Emacs and type in the following commands:
C-x 3 ; create a new window on the right
M-x cider-jack-in ; open up a REPL
C-x o ; switch to my left window
C-x C-f code/clojure-projects/something.clj ; open up a file and start coding
I would like to automate these tasks, so that they automatically happen every time Emacs starts.
To do this, I need to add something to the bottom of my ~/.emacs.d/init.el file, right?
I would also like to know the process by which I can figure out how to do these things in the future.
To have these commands all run at startup in clojure-mode only, add the following to your ~/.emacs.d/init.el file:
(defun my-clojure-startup ()
"Startup sequence for clojure-mode"
(interactive)
(split-window-horizontally)
(cider-jack-in)
(other-window)
(find-file "/your/full/filepath.ext"))
To bind this to a key, for example CRTL+c a :
(global-set-key (kbd "C-c a") 'my-clojure-startup)
Or to have it run whenever you start in clojure mode:
(add-hook 'clojure-mode-hook 'my-clojure-startup)
NOTE this is untested as I do not have clojure so cannot see full behaviour of each of the commands, but this should hopefully give you a boost at least.
You can use keyboard macro for that.
The idea is you record your actions and name the action. Emacs will generate the code for you and you just set your key mapping.
So first, you need to start recording your macro by C-x ( or <f3> and execute your commands until you finish then C-x ) or <f4> to end the macro recording session.
Now you will have an unnamed macro which you can test it by C-x e or <f5> which will execute all of your recorded commands.
Now you name your macro by M-x name-last-kbd-macro. After that, go to your init.el and then do M-x insert-kbd-macro which will paste (fset <macro-name> <your-macro-definition>) for you and now you can call your macro by using M-x <macro-name>even if you open new emacs session.
You can also set key binding if you like. For example:
(global-set-key (kbd "C-c a") 'my-macro)
For more info, you can have a look at EmacsWiki: Keyboard Macros
As a novice Emacs user (I'm about 3 months into what's probably a lifelong journey), I make changes to my .emacs file pretty regularly. It would be handy to have a global key binding to reload .emacs rather than go through the incredibly laborious process of M-x load-file (delete a long string if I'm deep into some directory) ~/.emacs <RET>. I've attempted a solution, but
;; reload .emacs when C-c <f12> is pressed
(defun reload-dotemacs ()
(load-file "~/.emacs"))
(global-set-key (kbd "C-c <f12>")
(lambda() (interactive) 'reload-dotemacs))
doesn't seem to work. Basically, when I enter the key combination, nothing happens, whereas trying M-x load-file ~/.emacs makes things happen (e.g. I see my yasnippet files reload).
For the record, C-c <f12> doesn't seem to be bound to anything else.
Fix for your code
(defun reload-dotemacs ()
(interactive)
(load-file "~/.emacs"))
(global-set-key (kbd "C-c <f12>") 'reload-dotemacs)
You do not need it 1
You do not need to remove the default string when you do M-x load-file RET - just type ~/.emacs.el RET and it will work.
You do not need it 2
Do not reload the init file, just evaluate the new code.
Type C-h m and C-h b in the .emacs.el buffer and you will see the useful keybindings (after searching for eval):
C-c C-b eval-current-buffer
C-c C-r eval-region
C-M-x eval-defun
C-j eval-print-last-sexp
C-x C-e eval-last-sexp
When using python-mode in Emacs, I first split the screen via C-x 3.
I'd like to be able do C-c ! to launch py-shell in the other window, not in the window currently active. How can I configure Emacs to do that without having to switch windows with C-x o before launching the shell?
I'm using Emacs 24.3.1, and I've got all my configuration files in ~/.emacs.d.
I just installed the python-mode package using package-install with the Marmalade repository, and I haven't yet edited any .el file related to python-mode.
As #BleedingFingers says you can simply use a macro and bind that to a key. It's up to you whether or not you want to re-use the C-c ! binding for the macro or bind it to a different key.
Here's how to proceed should you decide to go with the macro option, starting with Emacs showing only a single window:
Define macro
F3
C-x 3
C-x o
M-x py-shell RET
C-x o
F4
Assign name to macro
M-x name-last-kbd-macro RET py-shell-other-window RET
You can replace py-shell-other-window with whatever name you would like to use for the macro.
Add macro to your configuration
Open your configuration file, move point (cursor) to an empty line and do
M-x insert-kbd-macro RET
This will insert the macro definition into your configuration file.
Bind macro to key
Add the following code to your configuration file to bind the macro to a key in python-mode:
(require 'python-mode) ; Make sure python-mode-map is available
; for modification
(define-key python-mode-map (kbd "C-c !") nil) ; Unset default binding
; for C-c !
; (not necessary if you choose an
; unused binding)
(define-key python-mode-map (kbd "C-c !") 'py-shell-other-window) ; Bind macro to C-c !
Turn key binding on
Mark the lines added in the previous step and run M-x eval-region RET, or simply restart Emacs.
Celebrate :)
Advice lets you redefine code in other libraries on-the-fly.
(defadvice py-shell (around auto-split activate)
(split-window-right)
(other-window)
,ad-do-it
(other-window))
Variations apply.
How can I get Emacs to reload all my definitions that I have updated in .emacs without restarting Emacs?
You can use the command load-file (M-x load-file, and then press Return twice to accept the default filename, which is the current file being edited).
You can also just move the point to the end of any sexp and press C-x, C-e to execute just that sexp. Usually it's not necessary to reload the whole file if you're just changing a line or two.
There is the very convenient
M-x eval-buffer
It immediately evaluates all code in the buffer. It's the quickest method if your .emacs file is idempotent.
You can usually just re-evaluate the changed region. Mark the region of ~/.emacs that you've changed, and then use M-x eval-region RET. This is often safer than re-evaluating the entire file since it's easy to write a .emacs file that doesn't work quite right after being loaded twice.
If you've got your .emacs file open in the currently active buffer:
M-x eval-buffer
Solution
M-: (load user-init-file)
Notes
you type it in Eval: prompt (including the parentheses)
user-init-file is a variable holding the ~/.emacs value (pointing to the configuration file path) by default
(load) is shorter, older, and non-interactive version of (load-file); it is not an Emacs command (to be typed in M-x), but a mere Elisp function
Conclusion
M-: > M-x
M-x load-file
~/.emacs
Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote.
For that, Ctrl + Alt + X in the Elisp part works just fine.
The following should do it...
M-x load-file
I suggest that you don't do this, initially. Instead, start a new Emacs session and test whatever changes you made to see if they work correctly. The reason to do it this way is to avoid leaving you in a state where you have an inoperable .emacs file, which fails to load or fails to load cleanly. If you do all of your editing in the original session, and all of your testing in a new session, you'll always have something reliable to comment out offending code.
When you are finally happy with your changes, then go ahead and use one of the other answers to reload. My personal preference is to eval just the section you've added/changed, and to do that just highlight the region of added/changed code and call M-x eval-region. Doing that minimizes the code that's evaluated, minimizing any unintentional side-effects, as luapyad points out.
Keyboard shortcut:
(defun reload-init-file ()
(interactive)
(load-file user-init-file))
(global-set-key (kbd "C-c C-l") 'reload-init-file) ; Reload .emacs file
C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el
Define it in your init file and call by M-x reload-user-init-file
(defun reload-user-init-file()
(interactive)
(load-file user-init-file))
I'm currently on Ubuntu 15.04 (Vivid Vervet); I like to define a key for this.
[M-insert] translates to Alt + Ins on my keyboard.
Put this in your .emacs file:
(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))
Besides commands like M-x eval-buffer or M-x load-file, you can restart a fresh Emacs instance from the command line:
emacs -q --load "init.el"
Usage example: Company backends in GNU Emacs
Here is a quick and easy way to quick test your config. You can also use C-x C-e at the end of specific lisp to execute certain function individually.
C-x C-e runs the command eval-last-sexp (found in global-map), which
is an interactive compiled Lisp function.
It is bound to C-x C-e.
(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)
Evaluate sexp before point; print value in the echo area.
Interactively, with prefix argument, print output into current buffer.
Normally, this function truncates long output according to the value
of the variables ‘eval-expression-print-length’ and
‘eval-expression-print-level’. With a prefix argument of zero,
however, there is no such truncation. Such a prefix argument also
causes integers to be printed in several additional formats (octal,
hexadecimal, and character).
If ‘eval-expression-debug-on-error’ is non-nil, which is the default,
this command arranges for all errors to enter the debugger.
Although M-x eval-buffer will work, you may run into problems with toggles and other similar things. A better approach might be to "mark" or highlight what’s new in your .emacs file (or even scratch buffer if you're just messing around) and then M-x eval-region.
You can set a key binding for Emacs like this:
;; Reload Emacs configuration
(defun reload-init-file ()
(interactive)
(load-file "~/.emacs"))
(global-set-key (kbd "C-c r") 'reload-init-file)
If you happen to have a shell opened inside Emacs, you can also do:
. ~/.emacs
It may save a few key strokes.