How to switch to a different buffer from a terminal buffer - emacs

I've been using emacs for few weeks and it's been great so far - coming from vim was easier than I expected (actually - emacs' keyboard shortcuts feel more... natural).
I have added few customizations like moving between buffers using M-Left/Right/Up/Down because C-x o felt a little bit too slow when I had four files opened at once.
So far - so good :-)
One thing bugs me though:
I open some splits using C-x 3 and C-x 2
I open the terminal in one of them using M-x term ENT
How do I switch to different split using keyboard?
Usual shortcuts obviously don't work - terminal is intercepting every emacs command, and I have to click on different buffer to activate it.

In term-mode, any regular C-x whatever keybinding becomes C-c whatever instead.

I'm not sure I understand your question. If you run M-x terminal, most of the key events are sent to the underlying terminal, so the standard C-x o binding and your M-Left are not available in the terminal.
Try using M-x shell to get a shell in one of the windows, and the navigation bindings you set up should still work.

In term-mode, type C-c b RET to switch to some other buffer.
That does what C-x b RET normally does.

This should do the trick to get C-x b working. You may have to add bindings for any custom move commands.
(add-hook 'term-mode-hook
(lambda ()
;; C-x is the prefix command, rather than C-c
(term-set-escape-char ?\C-x)
(define-key term-raw-map "\M-y" 'yank-pop)
(define-key term-raw-map "\M-w" 'kill-ring-save)))
BTW, there is a big difference between shell-mode and term-mode. The former integrates better with emacs (e.g. cd command). The latter is a full terminal emulation and can handle curses programs. They both have their place.

For a more generic answer dealing with emacs' windows, you can look at windmove, which started shipping with Emacs circa Emacs 22, I believe:
;;; Commentary:
;;
;; This package defines a set of routines, windmove-{left,up,right,
;; down}, for selection of windows in a frame geometrically. For
;; example, `windmove-right' selects the window immediately to the
;; right of the currently-selected one. This functionality is similar
;; to the window-selection controls of the BRIEF editor of yore.
;;
;; One subtle point is what happens when the window to the right has
;; been split vertically; for example, consider a call to
;; `windmove-right' in this setup:
;;
;; -------------
;; | | A |
;; | | |
;; | |-----
;; | * | | (* is point in the currently
;; | | B | selected window)
;; | | |
;; -------------
;;
;; There are (at least) three reasonable things to do:
;; (1) Always move to the window to the right of the top edge of the
;; selected window; in this case, this policy selects A.
;; (2) Always move to the window to the right of the bottom edge of
;; the selected window; in this case, this policy selects B.
;; (3) Move to the window to the right of point in the selected
;; window. This may select either A or B, depending on the
;; position of point; in the illustrated example, it would select
;; B.
;;
;; Similar issues arise for all the movement functions. Windmove
;; resolves this problem by allowing the user to specify behavior
;; through a prefix argument. The cases are thus:
;; * if no argument is given to the movement functions, or the
;; argument given is zero, movement is relative to point;
;; * if a positive argument is given, movement is relative to the top
;; or left edge of the selected window, depending on whether the
;; movement is to be horizontal or vertical;
;; * if a negative argument is given, movement is relative to the
;; bottom or right edge of the selected window, depending on whether
;; the movement is to be horizontal or vertical.

Related

Start Emacs with R session and side by side windows?

I am very bad at customizing emacs. I desire that if i start with a file ending in ".r", emacs starts maximized, with two windows side by side (vertical division of the frame), in one my source code file, and in the other the ESS R interpreter. If I can understand the example, maybe I can generalize it to other extensions and modes. I still do not get the syntax of hooks in elisp.
The usual way to use Emacs is to have it always running instead of
opening and closing it all the time.
I suggest that you define a function that would make an existing Emacs
look the way you want:
(defun my-R-window-configuration ()
"Prepare the current emacs frame for R work."
(interactive)
;; maximimize the current frame:
(set-frame-parameter nil 'fullscreen 'maximized)
;; keep just the current window, presumably containing the R code
(delete-other-windows)
;; create ESS R interaction buffer and go there
(ess-switch-to-end-of-ESS)
;; go back to the code
(other-window 1))
Now you can do M-x my-R-window-configuration RET in an R buffer to get what you want.

Emacs cursor jumps before period on Proof General

I only encounter this problem when running Proof General. I'm assuming this is some random minor mode that is started by Proof General, but can't figure out which one! I include a list of minor modes bellow, in case you can recognise the name.
If I place a period in Emacs, the cursor will jump before it, like so:
Writing something|
Writing something.|
Writing something|.
Where | represents cursor and the last two lined happen immediately one after the other.
the same happens if I click at the end of a line with a period. The cursor will appear after the period and immediately jump before the period.
Some sentence. (click here)
Some sentence. |
Some sentence|.
Where the last two lined happen immediately one after the other.
Here is the list of minor modes, in case you can spot the name:
Aquamacs-Autoface Auto-Composition
Auto-Compression Auto-Encryption Blink-Cursor Column-Number Cua
Delete-Selection Electric-Indent File-Name-Shadow Font-Lock
Global-Font-Lock Holes Line-Number Menu-Bar Mouse-Wheel Osx-Key
Recentf Savehist Show-Paren Smart-Frame-Positioning Tabbar
Tabbar-Mwheel Tool-Bar Tooltip Transient-Mark
key binding
--- -------
^C Prefix Command
ESC Prefix Command
. proof-electric-terminator
<C-M-down> pg-move-region-down
<C-M-mouse-3> proof-mouse-goto-point
<C-M-up> pg-move-region-up
<C-S-mouse-1> pg-identifier-under-mouse-query
<C-return> proof-script-complete
(that binding is currently shadowed by another mode)
<M-down> proof-forward-command
<M-up> proof-backward-command
<remap> Prefix Command
... then a bunch of Proof specific minor modes...
Proof General is incompatible with show-paren-mode. You can turn it off with M-x show-paren-mode or by putting (add-hook 'proof-ready-for-assistant-hook (lambda () (show-paren-mode 0))) in the appropriate place in your .emacs.
See http://proofgeneral.inf.ed.ac.uk/trac/ticket/496.

Emacs switch to the next window regardless of frame

I'd like for the C-x o command (next window) to include windows in other frames as well as windows in the current frame.
Does anyone know how to pull this off? Is there another command that I should be using? Is there some snippet of elisp magic that can do this with ease?
C-x o is other-window. To go to an other frame use C-x 5 o which is other-frame.
Not sure if this is what you mean, but if you want to just cycle through buffers in the buffer list, regardless of frame:
Ctrl x→
Ctrl x←
These are bound to (next-buffer) and (previous-buffer), respectively.
This can be a first approximation.
http://www.gnu.org/software/emacs/manual/html_node/elisp/Cyclic-Window-Ordering.html
http://www.gnu.org/software/emacs/manual/html_node/elisp/Frames.html
other-window has a parameter to control how it deals with frames.
(global-set-key (kbd "C-x o") (lambda ()
(interactive)
(other-window 1 t)
(let ((nframe (window-frame (selected-window))))
(select-frame-set-input-focus nframe)
(make-frame-visible nframe))))
You must press C-x 5 o C-h to see all functions about working with frames.
Some of these function is other-frame.
I use the version 2.0 of ace-jump-mode. It takes about two minutes to understand how it works and since version 2.0 it allows to "jump" to another frame. You can jump to any character from any buffer/frame/window that you can actually see on a screen in three or four keypresses. It's very hard to beat.
It's a gigantic time saver anyway so I'd recommend checking it out because it's really convenient.
http://www.emacswiki.org/emacs/AceJump
And the "Emacs Rocks! Episode 10: Jumping around" two minutes screencast showing it in action:
http://www.youtube.com/watch?v=UZkpmegySnc
From C-h f next-window:
(next-window &optional WINDOW MINIBUF ALL-FRAMES) ...
ALL-FRAMES nil or omitted means consider all windows on WINDOW's
frame, plus the minibuffer window if specified by the MINIBUF
argument. If the minibuffer counts, consider all windows on all
frames that share that minibuffer too. The following non-nil values
of ALL-FRAMES have special meanings:
t means consider all windows on all existing frames.
`visible' means consider all windows on all visible frames.
0 (the number zero) means consider all windows on all visible and iconified frames.
A frame means consider all windows on that frame only.
Anything else means consider all windows on WINDOW's frame and no
others.
Somewhat ironically, other-window supports this as well, as it uses next-window. Unfortunately, I don't know of a way to pass non-numeric arguments interactively, but a simple function should do the trick:
(defun my-other-window (count)
(interactive "p")
(other-window count t))
You say "Is there a way to cycle through windows regardless of what frame they're in? That's really what I'm looking for?"
Yes, there is, with Icicles.
What you request is what command icicle-select-window does when you use a prefix arg. If you want that behavior always, you can define your own command that does it without a prefix arg:
(defun my-select-window ()
"Select window by name. Windows of all visible frames are candidates."
(interactive)
(let ((current-prefix-arg 1)) (icicle-select-window)))
You are prompted for the window name. But if you just want to cycle, without narrowing the candidates by typing part of the name, then just use C-down to get the window you want.
(A window name is the name of its displayed buffer, but suffixed as
needed by [NUMBER], to make the name unique. For example, if you have
two windows showing buffer *Help*, one of the windows will be called
*Help*[2] for use with this command.)

Use emacs to copy kill-ring to window/buffer based on position

This may be too involved.
Considering:
In emacs in r-mode or lisp mode (etc) information can be sent directly (copied, pasted, evaluated) from one buffer to the the R or Lisp interpreter.
I typically configure an emacs session to have 3 windows - a large horizontal window on top and two windows beneath it. (How) could I configure, which keys/ commands might I use to send the kill-ring to the last cursor position of the top window / buffer?
The buffer / window will not always necessarily have the same contents/file. (How) could I name it upon initialization?
Similar to C-X, C-B or C-X, B how might I specify which of the three window positions to go to (based on position)?
See window-at. For example,
(defun yank-into-top-window (&optional arg)
(interactive "*P")
(with-selected-window (window-at 0 0)
(yank arg)))
I think you're going to have to write lisp code to do this effectively. Basically, you'd want a minor mode that sets up the two subwindows -- which isn't hard, it happens in compile mode from M-x compile -- and then make special keybindings for the keys you want to use.

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.