touchpad horizontal scrolling in emacs - emacs

app on Mac OS X on a MacBook. Most applications on the computer allow me to scroll both vertically and horizontally with a two-finger drag on the trackpad. I would like to use this ability to position the cursor in emacs.
Adding the following lines to .emacs allows me to move the cursor vertically:
(global-set-key [wheel-up] 'previous-line)
(global-set-key [wheel-down] 'next-line)
I don't know of an equivalent setting for wheel-left or wheel-right. Can anyone help?

Put this in your .emacs-file:
;; Turn on horizontal scrolling with mouse wheel
(global-set-key (kbd "<mouse-6>") 'scroll-right)
(global-set-key (kbd "<mouse-7>") 'scroll-left)
When you first use it, emacs will ask you if you want to activate the restricted command scroll-left. That's just because some users find it confusing at first.

this slightly simpler version works for me:
(global-set-key [wheel-right] 'scroll-left)
(global-set-key [wheel-left] 'scroll-right)
or for osx reversed scroll directions:
(global-set-key [wheel-right] 'scroll-left)
(global-set-key [wheel-left] 'scroll-right)

(global-set-key (kbd "<mouse-7>")
(lambda () (interactive) (message "WHEEEEEL")))
That worked for me. Try C-h c and then scroll the mouse the way you intend to to see what event is triggered. It will tell you in the echo area.

In version 26.2 I can set these variables from the mouse customize group:
'(mouse-wheel-flip-direction t) ;; correct left-right scroll direction for OS X
'(mouse-wheel-tilt-scroll t) ;; enable left-right scroll from trackpad

In emacs 25.1 check out the *Messages* buffer when you swipe left and right on the trackpad. By the looks of it the key bindings are wheel-right and wheel-left with double and triple variants.
For example:
(global-set-key (kbd "<triple-wheel-right>")
(lambda () (interactive) (message "wheeling right")))
(global-set-key (kbd "<triple-wheel-left>")
(lambda () (interactive) (message "wheeling left")))
For me these values are not reversed in macOS

Related

In Emacs split windows, how to browse the other window?

I used C-x 3 to split to left and right windows. The cursor is in left window and I can use C-n, C-p to move the cursor. How can I stay in the left window and move the code in right window up and down?
I have done this before but I lost all my emacs files. So I cannot figure it out right now. Thanks.
I've googled for the answer, Re: How to scroll other window backwards? C-M-v does forwards
Still, use scroll-other-window function and add parameters to it. Below are the details:
(defun scroll-other-window-up-line ()
"Scroll the other window one line up."
(interactive)
(scroll-other-window -1))
(defun scroll-other-window-down-line ()
"Scroll the other window one line down."
(interactive)
(scroll-other-window 1))
(global-set-key (kbd "M-p") 'scroll-other-window-up-line)
(global-set-key (kbd "M-n") 'scroll-other-window-down-line)
Hope it can help.
scroll-other-window is your friend, bound to C-M-v by default.

Defining Control-Shift-* Emacs keyboard shortcuts

I'm trying to define the following two keyboard shortcuts to move between windows in Emacs:
C-shift-n: Move to the next window
C-shift-b: Move to the previous window
I thought the following will do it but it doesn't.
(defun select-next-window ()
"Switch to the next window"
(interactive)
(select-window (next-window)))
(defun select-previous-window ()
"Switch to the previous window"
(interactive)
(select-window (previous-window)))
(global-set-key (kbd "C-<S-n>") 'select-next-window)
(global-set-key (kbd "C-<S-p>") 'select-previous-window)
The problem seems to be with the last two lines that define the actual keyboard shortcuts to the functions that switch the windows (if I use simpler keyboard shortcuts instead of Control-Shift-* it works).
So, how do I use kbd to define Control-Shift-n and Control-Shift-p?
Assuming you never use caps lock, here's a super simple solution:
(global-set-key (kbd "C-N") 'select-next-window)
or
(global-set-key (kbd "C-<S-N>") 'select-next-window)
The problem is that when you hit shift you're sending capital N.

Disabling mouse scrolling in Emacs

Everything I've found on the internet has told me that emacs disables mouse scrolling by default, but mine is very enthusiastic and scrolls to its hearts content when my thumbs get too close to the trackpad on my laptop. I have not given it permission to do so, and here is my .emacs to prove it:
;; Line by line scrolling
(setq scroll-step 1)
;; Turn off backup file creation
;;(setq make-backup-files nil)
;; Enable backup files
(setq make-backup-files t)
;; Save all backup files to this directory
(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))
;; Show column number
(column-number-mode 1)
;; Fix C indenting
(setq c-default-style "bsd"
c-basic-offset 8)
Every once and a while my thumbs will brush the track pad, and suddenly I'll be typing in the middle of another line way up or way below from where I had just been. How can I stop this from happening?
EDIT
Running M-x customize-option mouse-wheel-mode yields:
Mouse Wheel Mode: [Hide Value] [Toggle] off (nil)
[State]: CHANGED outside Customize; operating on it here may be unreliable.
Non-nil if Mouse-Wheel mode is enabled. [Hide Rest]
See the command mouse-wheel-mode' for a description of this minor mode.
Setting this variable directly does not take effect;
either customize it (see the info nodeEasy Customization')
or call the function `mouse-wheel-mode'.
Groups: [Mouse]
It's enabled by default (at least since Emacs 23).
To turn it off (locally):
(mouse-wheel-mode -1)
To turn it off (globally):
M-x customize-option mouse-wheel-mode ; set to nil and save
But maybe you just want to adjust the settings: Take a look at M-x customize-group mouse RET
I did the following:
;; Disable mouse wheel (and two finger swipe) scrolling because
;; it scrolls horribly and I would rather work without it.
(mouse-wheel-mode -1)
(global-set-key [wheel-up] 'ignore)
(global-set-key [wheel-down] 'ignore)
(global-set-key [double-wheel-up] 'ignore)
(global-set-key [double-wheel-down] 'ignore)
(global-set-key [triple-wheel-up] 'ignore)
(global-set-key [triple-wheel-down] 'ignore)
The last part disables the scroll wheel events, which gets rid of the annoying beeping.
Here's what I did to disable all mouse scrolling (including horizontal):
(mouse-wheel-mode -1)
(global-set-key [wheel-up] 'ignore)
(global-set-key [double-wheel-up] 'ignore)
(global-set-key [triple-wheel-up] 'ignore)
(global-set-key [wheel-down] 'ignore)
(global-set-key [double-wheel-down] 'ignore)
(global-set-key [triple-wheel-down] 'ignore)
(global-set-key [wheel-left] 'ignore)
(global-set-key [double-wheel-left] 'ignore)
(global-set-key [triple-wheel-left] 'ignore)
(global-set-key [wheel-right] 'ignore)
(global-set-key [double-wheel-right] 'ignore)
(global-set-key [triple-wheel-right] 'ignore)
I don't think what you're seeing is mouse wheel scrolling as much as tap-to-click on your trackpad. If your mouse pointer is pointed to a different part of the document than where you typing and you accidentally brush the trackpad and your trackpad/OS interprets that as a mouse click, then the your input point will jump to the place the mouse cursor is pointed at.
You can address by disabling tap-to-click or by using the feature availabe with some trackpad/OSes that disable the trackpad all together while you're typing. How you do those will depend on your trackpad/driver/OS combination.

How do I fix the cursor to the middle of the screen in Emacs, so that the page moves, not the cursor?

I'd like to fix the cursor to the centre line of the screen, so that when I press Ctrl-N or Ctrl-P, the page itself moves up or down, and the cursor stays still.
Has anyone got any tips on how to achieve this?
Thanks
Ed
Try centered-cursor mode:
http://www.emacswiki.org/emacs/centered-cursor-mode.el
If you're using MELPA, it's available by M-x package-install RET centered-cursor-mode.
M-x scroll-lock-mode, which could be used to put the Scroll Lock key to good use too:
(global-set-key (kbd "<Scroll_Lock>") 'scroll-lock-mode)
The EmacsWiki page on SmoothScrolling presents some possible solutions.
You can roll your own using the recenter built-in:
(global-set-key (kbd "C-n")
(lambda (n)
(interactive "p")
(next-line n)
(recenter)))
(global-set-key (kbd "C-p")
(lambda (n)
(interactive "p")
(previous-line n)
(recenter)))

How to scroll line by line in GNU Emacs?

To put it simply, I'm trying to get scrolling in emacs like in vim and most other editors; when I'm for example, two lines from the bottom/top, and I press down/up (Ctrl-p,n, ↑,↓) it goes only one line up or down, not half the screen.
See some of the suggestions on the Emacs Wiki:
Emacs Wiki: Smooth Scrolling
(setq scroll-step 1
scroll-conservatively 10000)
If you want to position the screen exactly, you can use Ctrl-L.
By default it positions the current line in the middle of the screen.
ESC 0 Ctrl-L positions the current line at the top.
I'm a bit late to the party, but if you don't mind installing a package then smooth-scrolling (github, also available in MELPA) may be what you're looking for - it certainly works for me.
Once you've installed it you can pop the following in your init.el:
(require 'smooth-scrolling)
(smooth-scrolling-mode 1)
(setq smooth-scroll-margin 5)
The last line is optional; it starts scrolling near the screen edge rather than at it, so you've always got a little context around the point. Adjust to taste.
My solution is not to change Emac's default scrolling, but rather to create a key sequence command from a macro. This way you have a convenient way to scroll one line at a time when you want. Not ideal, but super easy. It just happens that M-(↓) and M-(↑) are available, so that's what I used.
This is how I did it. First, you need to record a macro for one line scrolls, up and down.
Begin macro
C-x (
Scroll down one
C-u 1 C-v
Stop macro
C-x )
For scroll up one, use
C-u 1 M-v
Next you need to name the macro.
M-x name-last-kbd-macro
Give it a name when prompted like:
down-one-line
Then just use the following to bind a key sequence to that command name:
M-x global-set-key
And upon prompting, use something like:
M-(down arrow)
Then it will ask you which command you want to bind, and you should give it the name you invented earlier, e.g., down-one-line.
Here is where I got this information. You can also find instructions below and elsewhere about adding your macro to the .emacs file.
Here for macro definition explanation
Here for how to control scrolling
I've been using these in my .emacs file since 2000.
(global-set-key (quote [M-down]) (quote View-scroll-line-forward))
(global-set-key (quote [M-up]) (quote View-scroll-line-backward))
This way, I can keep the Emacs default behavior as well as scroll one line at a time, depending on what I'm doing.
This worked till at least GNU Emacs 22. I recently upgraded to Emacs 24 and discovered that View-scroll-line-forward and View-scroll-line-backward are no longer available. After some hunting, I discovered that scroll-up-line and scroll-down-line work. So if you're using Emacs 24, you can use this.
(global-set-key (quote [M-down]) (quote scroll-up-line))
(global-set-key (quote [M-up]) (quote scroll-down-line))
I mostly skipped Emacs 23, so if that is the version you're using, you can experiment with both the above.
Note: scroll-up-line actually scrolls one line down, because the buffer is being moved one line up.
I rebind my arrow keys to perform scrolling operations.
(global-set-key [up] (lambda () (interactive) (scroll-down 1)))
(global-set-key [down] (lambda () (interactive) (scroll-up 1)))
(global-set-key [left] (lambda () (interactive) (scroll-right tab-width t)))
(global-set-key [right] (lambda () (interactive) (scroll-left tab-width t)))
Simples do this:
(global-set-key [M-up] (lambda () (interactive) (scroll-up 1)))
(global-set-key [M-down] (lambda () (interactive) (scroll-down 1)))
then meta cursor up moves up and meta cursor down moves down.
QED. Not sure what all the above people were smoking!
I have the following in my .emacs file to enable a nice ctrl-up, ctrl-down scrolling behavior. I also use this for the mousewheel.
(defun scroll-down-in-place (n)
(interactive "p")
(previous-line n)
(scroll-down n))
(defun scroll-up-in-place (n)
(interactive "p")
(next-line n)
(scroll-up n))
(global-set-key [mouse-4] 'scroll-down-in-place)
(global-set-key [mouse-5] 'scroll-up-in-place)
(global-set-key [C-up] 'scroll-down-in-place)
(global-set-key [C-down] 'scroll-up-in-place)
If you are looking for a quick way to create a scroll-like effect, enter in C-n and C-l sequentially which moves the cursor down and then centers it.
To have the "vim" scrolling put this to your .emacs file:
(defun next-line-and-recenter () (interactive) (next-line) (recenter))
(defun previous-line-and-recenter () (interactive) (previous-line) (recenter))
(global-set-key (kbd "C-n") 'next-line-and-recenter)
(global-set-key (kbd "C-p") 'previous-line-and-recenter)
Since it can be annoying to use the M-up, M-down because it interferes with the org-mode which overloads these commands. To avoid this issue I personally use those commands which combine M-page-up M-page-down". Here I defined the scroll up and down to 1 line.
;;;scroll by `number-of-lines' without the cursor attached to the screen
(global-set-key [M-prior] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-down number-of-lines)
(forward-line (- number-of-lines)))))
(global-set-key [M-next] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-up number-of-lines)
(forward-line number-of-lines))))
;;;scroll by `number-of-lines' with the cursor attached to the screen
(global-set-key [S-M-prior] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-down number-of-lines))))
(global-set-key [S-M-next] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-up number-of-lines))))
M-x customize-variable scroll-conservatively
Set it to 1.
You don't really want to do this, though.
If you don't mind using the mouse and have a scroll wheel, you can customize the variable mouse-wheel-scroll-amount by either:
C-h v mouse-wheel-scroll-amount (click on customize, change value to "Specific # of lines" 1, ApplyAndSave.)
or add to .emacs the line:
'(mouse-wheel-scroll-amount '(1 ((shift) . 1) ((meta)) ((control) . text-scale)))
There are lots of possibilities listed at
https://www.emacswiki.org/emacs/Scrolling
If you start emacs in .xsession, in my case setting scroll-conservatively to 100+ will not work, nor scroll-step 1. But if u start emacs after X, it works.
After playing a bit with the available configuration (emacs 26.3), I got to the following set of values:
(setq scroll-step 1
scroll-preserve-screen-position t
scroll-margin 10
scroll-conservatively 10
maximum-scroll-margin 0.0
scroll-up-aggressively 0.0
scroll-down-aggressively 0.0)
I believe the values for scroll-margin and scroll-conservatively do not matter much because the maximum-scroll-margin clamps them down. They just need to be equal (maybe?).
Scroll happens line by line, even on the end of the file (worst case for me). The only missing feature was that with this the margin on top and bottom are lost.
Its a compromise and, for me, smooth scrolling is worth it.