Key binding to reload .emacs after changing it? - emacs

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

Related

emacs cider clear REPL buffer

I simply want to clear the repl buffer so that a single prompt eg (user>) is left on the first line.
I have a keybinding:
(put 'erase-buffer 'disabled nil)
(global-set-key (kbd "C-x C-<backspace>") 'erase-buffer)
But this gives the message :
text is read only
There is the option C-c C-o but this only clears the last return value.
When using python, and run-python the following command C-x M-o which i believe is comint-clear-buffer
cider-repl.el provides a function cider-repl-clear-buffer which by default is bound to:
M-x c-r--bu RET
as C-c M-b is not used by cider-repl as far as I am aware:
(add-hook 'cider-repl-mode-hook
'(lambda () (define-key cider-repl-mode-map (kbd "C-c M-b")
'cider-repl-clear-buffer)))
cider-repl.el also provides cider-repl-handle-shortcut which is bound to ,.
Which will prompt you to many commands, such as clear (which you want), ns (to change namespace), refresh, reload and many others
I find pressing , followd by enter (to choose clear, faster/more convenient than the other answer.)
Note: you need to type , into the repl while the line is empty, it works for both evil and normal emacs keybinds

How to customize Emacs key bindings for going to specific line / end of buffer

I want to customize Emacs so that pressing
ESC : n RET
takes me to line number n
and
ESC : $ RET
takes me to the last line. (That's how the vi editor works.)
How can I achieve this inside my Emacs configuration file? Currently I have this in my .emacs:
(global-set-key (kbd "M-9") 'prev-window)
(global-set-key (kbd "M-0") 'other-window)
I don't want to use any of the off-the-shelf solutions (eg. evil) because they are bloated and mess with my existing shortcuts.
Put this in a file and load it (load means execute):
(defun vi-goto-line (arg)
(interactive "sLine:")
(message arg)
(if (string= "$" arg)
(end-of-buffer)
(goto-line (string-to-int arg))
)
)
(global-set-key (kbd "M-:") 'vi-goto-line)
To load it you can use M-xload-file and then enter interactively the path to the file.
Keep in mind that the key combo M-: (which is the same as ESC:) already has a meaning in Emacs, so this now gets cloaked.
Of course you also can load the file from your .emacs by putting (load-file "/path/to/my/file") into the .emacs or put these lines directly into your .emacs file (or any other configuration file which gets loaded)

Why doesn't my keymap for C-c C-c work in C++?

I'm new to emacs and using emacs 24 and trying to bind C-c C-c to a function to comment out a single line. I have the following in my init.el file but it doesn't seem to work in c++.
(defun toggle-comment-on-line ()
"comment or uncomment current line"
(interactive)
(comment-or-uncomment-region (line-beginning-position) (line-end-position))
(next-line))
(global-set-key (kbd "C-c C-c") 'toggle-comment-on-line)
When I'm playing around in the scratch page it works fine and when I check with C-h k C-c C-cit displays the right function but when I'm in C++ the same command displays the text:
C-c C-c runs the command comment-region, which is an interactive
compiled Lisp function in `newcomment.el'.
It is bound to C-c C-c, <menu-bar> <C++> <Comment Out Region>.
(comment-region BEG END &optional ARG)
Comment or uncomment each line in the region.
With just C-u prefix arg, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.
The strings used as comment starts are built from `comment-start'
and `comment-padding'; the strings used as comment ends are built
from `comment-end' and `comment-padding'.
By default, the `comment-start' markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments). This can be
changed with `comment-style'.
I assume something else is overriding C++ keybindings but I don't know what or how to fix it? Does anyone have any ideas?
Yes, c++ mode has its own keymap, which overrides the global map. Use the following instead:
(define-key c++-mode-map (kbd "C-c C-c") 'toggle-comment-on-line)
I've improved your code a bit, and below there's also the
code to bind the key without an error
(it happens because you're trying to define a key in
c++-mode-map before it was defined)
(defun toggle-comment-on-line ()
"comment or uncomment current line"
(interactive)
(let ((beg (if (region-active-p)
(region-beginning)
(line-beginning-position)))
(end (if (region-active-p)
(region-end)
(line-end-position))))
(comment-or-uncomment-region beg end)
(next-line)))
(add-hook 'c++-mode-hook
(lambda()
(define-key c++-mode-map (kbd "C-c C-c") 'moo-complete)))
As a side note, I strongly recommend against binding C-c C-c,
as this is a very popular mode specific binding that's different in every mode,
but means generally confirm:
in org-mode it evaluates a babel block of code
in message-mode it sends the email
in python-mode it sends the buffer to the process
in wdired it confirms your edits to the file names
So you'll really have a headache if you bind it, unless you're
using Emacs just for c++-mode.
I've been using Emacs for 3 years now and I have comment-dwim
on C-.. I'm quite happy with it so far.
If you're willing to use a different key binding, you can use the following code:
;; Nothing to see here.
after which you can do C-a C-SPC C-n M-;.

Reloading AucTex Labels and Defining Keybindings

When I add a label in emacs to a .tex file, I used to reload the file to get it to show up in RefTeX. i.e. C-c ) wouldn't have the new label unless I reloaded the file.
After some searching I found that C-u C-c ) will refresh RefTeX before trying to do the reference. This works as I would like, but I would like to use C-c r for this command instead of typing C-u C-c ) every time. How do I do this?
Thanks,
Jim
I don't use reftex but as far as I can understand you want just to define a binding:
(define-key reftex-mode-map (kbd "C-c r") 'reftex-reference)
Building on Oleg's answer:
Maybe it would be better to feed it the C-u argument already if that's what you are after:
(defun call-reftex-reference-directly ()
(interactive)
(let ((current-prefix-arg 4)) ;; emulate C-u
(call-interactively 'reftex-reference) ;; invoke reftex-reference
)
)
(define-key reftex-mode-map (kbd "C-c r") 'call-reftex-reference-directly)
Maybe try if this works for you?
You can type r in the label selection buffer to refresh it without reloading the file. The refresh is instantaneous (unless you have very large/very many linked files).

How to change the default compilation command/shortcut in GNU Emacs-AUCTeX?

In GNU Emacs with AUCTeX enabled, C-C C-C is the default shortcut for running latex over the active buffer. How can I change this to also run dvips after the dvi output is generated by latex? Can I define a new shortcut, say C-C C-D, and assign it to the foregoing operation?
M-x describe-key <RET> C-c C-c
C-h k C-c C-c
will each give you the function name that is called for compile. Then you can rebind as follows in your .emacs:
(global-set-key (kbd "C-c C-d") '<function name>)
This isn't fully general, as I'm not fully familiar with the innards of AUCTeX. Typically there is some type of mode hook to run (keeps you from rebinding a global).
Here's an example adapted from http://emacswiki.org/emacs/AUCTeX
(add-hook 'LaTeX-mode-hook
'(lambda ()
(local-set-key "<key>" '<function name>)))
As to your question about running dvips, you would define your own function and do a keybinding. in a similar manner as above.