1

I'm trying to get Emacs to imitate Vim's EOL behavior. Namely at the end of line ("\n"), the cursor comes to a halt instead of progressing to the start of the next line.

The following works fine and dandy but the cursor gets stuck before empty lines:

(defun make-eol-intangible ()
  (interactive)
  (add-hook 'post-command-hook 
    (lambda ()
      (when (and (point) (looking-at "\n"))
        (backward-char)) t t)))

I could put an additional condition that tests for (point) looking-back however Emacs docs mention that looking-back should be avoided, and is possibly deprecated. On that note it's probably less optimal to use looking-at either.

I would like some suggestions.


Amended function (doesn't work as expected):

(defun make-eol-intangible ()
  (interactive)
  (add-hook 'post-command-hook 
    (lambda ()
      (when (and (eq (char-before (point)) ?\n) 
                 (eq (char-after (point)) ?\.))
    (backward-char)))))

(make-eol-intangible)
10
  • 2
    When should the cursor "come to a halt"? When forward-char is invoked? Why are you checking (point) in a condition? It will always return non-nil. looking-at uses regexps, but you don't need a regexp to detect a newline. following-char, char-after, preceding-char, and char-before can all be used to detect a newline character after or before point, respectively. – Basil Jan 2 at 13:24
  • What @Basil said. Please try to clarify the behavior you want. Don't assume that readers will know what the Vim behavior is. Specify the desired behavior. That will help others help you better. – Drew Jan 2 at 17:35
  • What was not so clear about this description? : "I'm trying to get Emacs to imitate Vim's EOL behavior. Namely at the end of line ("\n"), the cursor comes to a halt instead of progressing to the start of the next line." – OS2 Jan 2 at 17:46
  • @OS2 In Emacs, when point is at the end of the line, it does not progress to the start of the next line on its own. A key/command has to be invoked in order for point to move to the next line. So which key/command are you trying to customise in order for point not to move to the next line? In other words, please describe the sequence of keys/commands you would like to invoke and what you would like to see as a result of that key/command sequence. Otherwise what you are asking is not very clear. – Basil Jan 2 at 19:46
  • 2
    Avoid using post-command-hook for this. Instead write your own forward char and backward char functions, and bind them to appropriate keys. – Lindydancer Jan 19 at 22:40
0

Something along these lines.

(defun make-eol-intangible ()
  (interactive)
  (add-hook 'post-command-hook 
    (lambda ()
      (when (and (eq (char-before (point)) ?\n)
                 (not (eq (line-end-position) (line-beginning-position))))
        (left-char)))))

Found a better solution:

(defun my-maybe-right ()
  "Move right if char at point is not a newline."
  (interactive)
  (unless (= (following-char) ?\n) (forward-char)))

(global-set-key [right] #'my-maybe-right)
2
  • (global-set-key [right] #'my-maybe-right) results in argument error unless I use (define-key (current-global-map) [right] #'my-maybe-right). – OS2 Jan 21 at 9:27
  • How do you get the error? What's the error? How are you evaluating this? If you look at the definition of global-set-key (e.g. with M-x find-function RET global-set-key RET) you'll see it's doing the exact same thing. – Basil Jan 21 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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