Opening a window into a specified buffer - emacs

How can I open a new window (for example using C-x 3) into a new buffer, rather than a mirrored buffer that just echoes what I type.
So for example, let's say I'm messing around with python and I want to run the script in the shell. As it is currently I do this: C-x 3, M-x shell and then start it up and running. I'd rather just C-x 3 and it automatically opens into shell. I'm really new to Emacs so I don't know where to look for this.

It sounds to me like this, or something similar, is what you are looking for:
(defun pop-to-buff-at-right (buffer)
"Pop to BUFFER at the right of the current window."
(interactive "B")
(pop-to-buffer buffer '(display-buffer-in-side-window
(side . right)
(inhibit-same-window . t))))
You do not want to just split the window, which is specifically about showing the same buffer twice. You want to switch to another buffer, but you want it to be displayed to the right of the current window.

In emacs it is easy to define custom commands and bind it to keys. For instance, if you add this to your init file:
(defun open-shell-at-left ()
(interactive) ;; Tell emacs this function can be called interactively
(split-window-right) ;; Just what C-x 3 does
(shell)) ;; Just what M-x shell does
(global-set-key (kbd "C-c 3") 'open-shell-at-left)
You will have what you want when you type C-c 3. In general, you can find documentation about what a key binding does by typing C-h k and the keybinding. From that point, it is easy to chain existing commands into new ones.

Related

How to move an emacs buffer to a new window?

I have many buffers open in an Emacs window. I want to move one of the buffers to a new window. Is there a command which does that?
The command you are looking for is tear-off-window. Note that this command must be associated with a mouse click event.
For example, you can put the following code (from this reddit comment) in your init file (more about init file here):
(global-set-key [mode-line C-mouse-1] 'tear-off-window)
This will call tear-off-window when you Control-click on the buffer modeline.
If you want to use a keyboard binding, the modified version of tear-off-window is below. Put it into your init file to save it after restarting emacs.
(bind-key "C-x C-X" #'my/tear-off-window)
(defun my/tear-off-window ()
"Delete the selected window, and create a new frame displaying its buffer."
(interactive)
(let* ((window (selected-window))
(buf (window-buffer window))
(frame (make-frame)))
(select-frame frame)
(switch-to-buffer buf)
(delete-window window)))
In the code, the modified command is bind to "C-x C-X". You are free to change it to any other key sequence (more details here).
IIUC, you want to create a new WM window.
Emacs uses a slightly different terminology: what are usually called "windows" in a GUI environment, Emacs calls "frames". WIthin a single frame, Emacs subdivides its area into separate "windows" (IOW, even in a non-GUI environment, Emacs acts as a "tiling" window manager). So, you can create a new frame with C-x 5 2 (or equivalently M-x make-frame-command RET) and then in that frame, switch to the required buffer with C-x b <buffer-name> RET.
Note by the way, that you don't "move" the buffer to the new frame: the buffer exists independently of whether there is a (emacs) window in a frame that displays the buffer.

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 - Open buffer list without replacing another buffer

I have one frame, one window.
I use Cx 3, I now have two windows.
I use Cx Cb in order to see the list of buffers, however it opens it in another window but doesn't put the focus on it. It is even more annoying if I had opened a buffer on the 2nd window.
I would prefer to either open the buffer list in the window which currently has the focus, or temporarily change the focus to the buffer list.
First of all I want to start by saying that the ibuffer function does similary to what you want and does so in the current window, its definitely worth checking out.
Now onto your actual question. C-x C-b by default calls the function list-buffers. If you search for that command using C-h f it will show you the documentation, from there you can view the source code for the function by clicking on the underlined text that says buff-menu.el.
Now we can view the source of the list-buffers, the first function called is display-buffer. That sounds promising. We can now use C-h f once again to search for the display-buffer command. Reading though this documentation we see that the variable display-buffer-alist dictates how the display-buffer and in turn how list-buffers works. We can then see that by adding ("*Buffer List*" . display-buffer-same-window) to display-buffer-alist you will get the desired result.
All in all you simply need to put (add-to-list 'display-buffer-alist '("*Buffer List*" . display-buffer-same-window)) in your init file for your changes to take place.
Please just try one of these:
open the buffer list who currently has the focus:
(defun my:list-buffers (&optional arg)
(interactive "P")
(display-buffer (list-buffers-no-select arg) '(display-buffer-same-window)))
change the focus
(defun my:list-buffers2 (&optional arg)
(interactive "P")
(select-window (list-buffers arg)))

Changing window faster in Emacs (or repeating last shortcut with a single strike)

if I want to change windows in emacs I do C-x o and that's fine with me...but when I want to change window lots of times in a row C-x o is not so convenient...is there a way to change window with just one strike after first C-x o ?
and in general...is there a (single) strike that would repeat my last shortcut?
I use C-tab to switch windows:
(global-set-key [C-tab] 'other-window)
Holding down the Control key, you can jump windows repeatedly just by hitting the tab key.
EDIT: my original answer contained the following
I don't think there's a built-in way to repeat last command for basic commands like this ...
This is no longer true. Emacs now contains repeat.el, which allows for exactly the behaviour rabidmachine9 asked for.
The following code will create a repeating other-window, such that after pressing C-x o the first time, pressing o afterwards will continue moving to the next window.
(require 'repeat)
(defun make-repeatable-command (cmd)
"Returns a new command that is a repeatable version of CMD.
The new command is named CMD-repeat. CMD should be a quoted
command.
This allows you to bind the command to a compound keystroke and
repeat it with just the final key. For example:
(global-set-key (kbd \"C-c a\") (make-repeatable-command 'foo))
will create a new command called foo-repeat. Typing C-c a will
just invoke foo. Typing C-c a a a will invoke foo three times,
and so on.
See related discussion here:
http://batsov.com/articles/2012/03/08/emacs-tip-number-4-repeat-last-command/#comment-459843643
https://groups.google.com/forum/?hl=en&fromgroups=#!topic/gnu.emacs.help/RHKP2gjx7I8"
(fset (intern (concat (symbol-name cmd) "-repeat"))
`(lambda ,(help-function-arglist cmd) ;; arg list
,(format "A repeatable version of `%s'." (symbol-name cmd)) ;; doc string
,(interactive-form cmd) ;; interactive form
;; see also repeat-message-function
(setq last-repeatable-command ',cmd)
(repeat nil)))
(intern (concat (symbol-name cmd) "-repeat")))
(global-set-key (kbd "C-x o") (make-repeatable-command 'other-window))
The function make-repeatable-command than then be used to create other repeating commands, using the same template.
Check out windmove; it lets you just hold down a modifier key and press an arrow key to move to the window in that direction. I've been using it for years with the default modifier (shift) and strangely enough it doesn't interfere with my impulses to use shift-arrow text selection in other applications.
There's also an equivalent for frames, which I should really try...
You have, say, 10 windows in the frame, and you are doing M-x other-window a lots of times in a row, I take it you mean to jump from, say window #2 to window #8 and then on to window #1 and so on. By doing lots of other-window in a row, I would imagine you do nothing of importance until you reach the desired window.
See if universal-argument bound to C-u helps. In the 10 window frame, if you are in window #3 and want to go to window #9, you are hopping to the 6th next window. So you would do C-u 6 C-x o. Or, you could as well do C-u -4 C-x o and reach window #9 from window #3.
Bit late to the party, but there is also window-numbering (known as 'window-number' in MELPA).
This includes a window number in the modeline -1-, -2- etc, and provides M-1, M-2 etc key bindings to directly select them. Very quick.

How can I easily reload Emacs lisp code as I am editing it?

As an Emacs beginner, I am working on writing a minor mode. My current (naive) method of programming elisp consists of making a change, closing out Emacs, restarting Emacs, and observing the change. How can I streamline this process? Is there a command to refresh everything?
You might try using M-C-x (eval-defun), which will re-evaluate the top-level form around point. Unlike M-x eval-buffer or C-x C-e (exal-last-sexp), this will reset variables declared with defvar and defcustom to their initial values, which might be what's tripping you up.
Also try out C-u C-M-x which evaluates the definition at point and sets a breakpoint there, so you get dropped into the debugger when you hit that function.
M-x ielm is also very useful as a more feature-rich Lisp REPL when developing Emacs code.
M-x eval-buffer should do it.
What Sean said. In addition, I have (eval-defun) bound to a key, along with a test. The development loop then becomes: 1) edit function, 2) press eval-and-test key, 3) observe results, 4) repeat. This is extremely fast.
During development I write a test, bind it to jmc-test, then use the above key to run it on my just-edited function. I edit more, then press key again, testing it again. When the function works, I zap jmc-test, edit another function, and write another jmc-test function. They're nearly always one line of code, so easy to just bang out.
(defun jmc-eval-and-test ()
(interactive)
(eval-defun nil)
(jmc-test))
(define-key emacs-lisp-mode-map (kbd "<kp-enter>") 'jmc-eval-and-test)
(when t
(defun myfunc (beer yum)
(+ beer yum))
(defun jmc-test () (message "out: %s" (myfunc 1 2))))
When editing "myfunc", if I hit keypad enter, it prints "out: 3".
It all depends on what you're writing and how you've written it. Toggling the mode should get you the new behavior. If you're using [define-minor-mode][1], you can add code in the body of the macro that keys off the mode variable:
(define-minor-mode my-minor-mode
"doc string"
nil
""
nil
(if my-minor-mode
(progn
;; do something when minor mode is on
)
;; do something when minor mode is off
)
But, another way to check it quickly would be to spawn a new Emacs from your existing one:
M-x shell-command emacs&
I just define a function called ldf (short for load-file) in my .emacs file,
like this:
(defun ldf (arg) (interactive "P") (load-file (buffer-file-name)))
As you can see, this little function looks up the filename of the current buffer and then loads the file. Whenever I need to reload the current buffer elisp file, just type "M-x ldf"