4

I use evil-mode, but it doesn't support zs and ze shortcuts from Vim (scrolling horizontally to cursor position).
How can i map these keys to achieve the same behavior in Emacs?

Some explanation of what zs/ze do:
Let's assume i have a file with lines containing 500 chars and the line wrapping is turned off. My text editor window is 81 columns long, so i don't see all the chars in the lines, but i want to see them only from 100'th char (column) as far to the right as i can (the chars before 100'th do not interest me).
I move my cursor to 100'th char, which now is around center of the screen. When i press zs now, the 100'th column becomes the first visible column on the screen so i can see characters from 100 to 180 in my text editor window.
When i press ze now the current column (100'th) becomes the last visible column, so i can see the characters from 20 to 100'th.

Quote from Vim manual:

*zs*  Scroll the text horizontally to position the cursor
      at the start (left side) of the screen.  This only
      works when 'wrap' is off.  {not in Vi}


*ze*  Scroll the text horizontally to position the cursor
      at the end (right side) of the screen.  This only
      works when 'wrap' is off.  {not in Vi}

Solution

What worked for me, thanks to /u/gilles:

(defun hscroll-cursor-left ()
  (interactive "@")
  (set-window-hscroll (selected-window) (current-column)))

(defun hscroll-cursor-right ()
  (interactive "@")
  (set-window-hscroll (selected-window) (- (current-column) (window-width) -1)))

(define-key evil-normal-state-map "zs" 'hscroll-cursor-left)
(define-key evil-normal-state-map "ze" 'hscroll-cursor-right)
(setq auto-hscroll-mode 't)
(setq hscroll-margin 0
      hscroll-step 1)
3
  • 3
    Please clarify what you mean by "scrolling horizontally to cursor position." What do you want these commands to do?
    – Dan
    Commented Feb 10, 2015 at 15:56
  • @Kossak The link to the zs and ze info you pasted is not working. Can you put some examples here? Commented Feb 10, 2015 at 19:05
  • @kaushalmodi it works for me, but i pasted the part of the Vim manual concerning zs and ze and provided my own explanation
    – Kossak
    Commented Feb 10, 2015 at 21:01

2 Answers 2

3

It's easy enough to set the horizontal position of the current window.

(defun hscroll-cursor-left ()
  "Scroll horizontally to bring the cursor to the leftmost visible column.
Similar to Vim's `zs'."
  (interactive "@")
  (set-window-hscroll (selected-window) (current-column)))

(defun hscroll-cursor-right ()
  "Scroll horizontally to bring the cursor to the rightmost visible column.
Similar to Vim's `ze'."
  (interactive "@")
  (set-window-hscroll (selected-window) (- (current-column) (window-width) -1)))

(define-key evil-mode-map "zs" 'hscroll-cursor-left)
(define-key evil-mode-map "ze" 'hscroll-cursor-right)

However this will not help you under the default Emacs settings, because Emacs tends to arrange to scroll the window horizontally automatically to bring the cursor near the center. You can completely turn off automatic horizontal scrolling by setting auto-hscroll-mode to t, but this allows the cursor to get lost beyond the left or right edge of the visible part of the window.

You can get a behavior that's closer to Vim by keeping automatic horizontal scrolling but dropping the margin to a minimum.

(setq hscroll-margin 0
      hscroll-step 1)

I haven't checked what happens in “nontrivial” cases like variable-width characters, automatically wrapped lines, etc.

0

I've tried figuring out what those do by opening a file with long lines in Vim, disabling word wrap with :set nowrap and trying both commands. Apparently zs positions the column point is on so that point is on the left side of the viewport, ze does the same, but positions it to make point appear on the right side of the viewport.

However, Emacs handling of the viewport is... peculiar. The Emacs Lisp manual elaborates upon the commands window-hscroll and set-window-hscroll, but the latter only adjusts the viewport if point is far away from the window boundary and goes as far as readjusting it if you move point towards it again. Maybe someone else has more luck with figuring out the reasons for this pretty weird behaviour and can continue my research. I assume the authors of evil ran into the same hitch and decided it wasn't worth it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.