Launch py-shell command in another emacs window - emacs

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.

Related

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

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.

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

How to open a directory of current buffer that is opening a file using just one command in Emacs?

Is there any simple command to do this? I'm tired to type C-x C-f ENTER.
If you (require 'dired-x) in your init file (or alternatively follow the autoloading instructions1), you can use C-xC-j to call dired-jump, which not only does what you want (in both file-visiting buffers and also in dired buffers), but also places point on the dired entry for the file or directory that you have just come from, which can be incredibly convenient.
1 C-hig (dired-x) Optional Installation Dired Jump RET
There isn't a single command to do that as far as I know, but since you are using emacs you can easily define one. Add these to your init file
(defun my-open-dired-here ()
(interactive)
(dired default-directory))
The above defines a command to open dired in current buffer's directory, this is what you get when you do C-x C-f RET in vanilla emacs.
You can bind the above command to a key of your choice I am binding it to F6
(global-set-key (kbd "<f6>") 'my-open-dired-here)
Now when you press F6 you will get dired opened in current buffer's directory.

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.