Emacs, bind "convert buffer to dos format" to f11 key - emacs

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.

Related

How to set a keybinding to create and jump to the next line in emacs?

I have the following code that attempts to create a new line and then jump to it. The idea is that move-end-of-line will jump to the end of the current line, and ["C-m"] would act as return/enter. Yet executing this command gives the error: "wrong number of arguments". How do I fix this?
(global-set-key (kbd "C-.") 'new-line)
(defun new-line ()
(interactive)
(move-end-of-line)
["C-m"]
)
I think you need to read the Emacs & elisp manuals: these questions are pretty easy to answer. Here's one way to do it.
(defun insert-line-after-line (&optional n)
(interactive "p")
(end-of-line 1) ;end of current line
(open-line n) ;open n new lines
(forward-line 1)) ;go to start of first of them
But seriously: Emacs has very extensive self-documentation, it is easy to find out how to do these things.
An option is to record a macro and use that.
M-x kmacro-start-macro
C-e
C-m
M-x kmacro-end-macro
If you don't care about the macro persisting, just run it:
C-x e
But if you want it to persist you would save it:
M-x name-last-kbd-macro new-line
M-x insert-kbd-macro new-line
and paste the output into your initialisation file (with your shortcut definition):
(global-set-key (kbd "C-.") 'new-line)
(fset 'new-line
[end return])
["C-m"] is like the way you specify a key for doing a key binding, but this is not the same as how you programmatically tell Emacs to insert a character into a document. You could use (insert-char ?\^M) (see ref here), but that would result in a literal ^M character (try it; another way to do the same thing interactively is Ctrl-Q followed by Ctrl-M). (insert "\n") seems to be what you're looking for.
Also, the reason you're getting the message "wrong number of arguments" is because (move-end-of-line) requires an argument when called out of interactive context. (move-end-of-line 1) works.
That said, possibly an easier way to achieve the result you're looking for is with the following setting:
(setq next-line-add-newlines t)
This will allow you to just do C-n at the end of the file and not worry about the distinction between moving and inserting.

How do I add the following key bindings to my Emacs init.el?

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

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

Launch py-shell command in another emacs window

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.

emacs map several keystrokes and command to one key

I'm trying to map c-u m-x indent-pp-sexp to a single key, like F5, so that working with Emacs doesnt erode my fingerprints.
I use (global-set-key (kbd "C-u M-x indent-pp-sexp") "<f5>") but i'm getting the following error:
global-set-key: Key sequence C-u M-x i n d e n t - p p - s e x p starts with non-prefix key C-u
EDIT
With this lambda function (global-set-key (kbd "<f5>") (lambda (interactive) (universal-argument) (indent-pp-sexp t)))
Getting error:
recursive-edit: Wrong type argument: commandp, (lambda (interactive) (universal-argument) (indent-pp-sexp t))
Weird, because univeral-argument takes no parameters, and indent-pp-sexp takes boolean
You have the arguments the wrong way around, and you bind keys to functions, not to other key sequences. Perhaps you are really looking for a named macro; or you can write some actual Lisp and bind that to F5:
(global-set-key (kbd "<f5>")
(function (lambda () (interactive) (indent-pp-sexp t) )) )
The presence of an argument in the call form appears to be sufficient to select the prefix argument functionality.
You're missing the argument list to the lambda. Additionally I think passing t to indent-pp-sexp negates the need to call universal-argument.
(global-set-key (kbd "<f5>") #'(lambda ()
(interactive)
(indent-pp-sexp t)))
I'm a noob like you, but I already happened to figure basic things like making macros. I don't really know what's wrong with your code, but here's walkthrough of how I do things at home. What you need to do first, is press F3. Then type your keystrokes, and when finished, press F4. Congratulations, you have defined an anonymous macro. You can replay it as many times you wish by pressing F4 again. When you have played enough, enter M-x name-last-keybord-macro, and name it eg. foobar. Go to your ~/.emacs.d/macros/ directory (make it if you don't have one) and visit a file that you will name foobar.el. In its buffer, M-x insert-kbd-macro. When asked about name, say foobar. You will see that emacs has entered the contents of your just recorded macro into the file. Save it. Open your .emacs file, and add lines:
(load (expand-file-name "~/.emacs.d/macros/foobar.el"))
(global-set-key (kbd "M-<f5>") 'foobar)
And things start working for me after restart, with M-F5 as the binding for foobar.el macro.