3

Normally return key is just newline, I believe a lot of people bind it to newline-and-indent, but return is relatively common used key, the C-u RET and C-- RET basically do nothing but same as return, I don't want to waste prefix features. How can I make it do these?

  1. If I type return, it will do normal things(e.g., it will create a new line and indent if I type return at the end of a line; it will split the line from here, create new line, jump to it and then indent if I type return in the middle of the line.)
  2. If I type C-u return(whether in the middle or at the end of the line), it will create a new line(not split), jump to it but not indent.(like C-e C-o C-n)
  3. If I type C-- return(whether in the middle or at the end of the line), it will create a new line above the current line and jump to it.(like C-a C-o)

I already made these happen using global-set-key, but wasted S-return and M-S-return, if anyone can advance the default return key using prefix argument feature, that would be better.

Update: I got the following defun, but it doesn't work, what's wrong with it?

(defun advanced-return (&optional arg)
  (interactive "p")
  (if (equal arg '-)
      (progn
        (beginning-of-line)
        (open-line))
    (if (equal arg '(4))
        (progn
          (end-of-line)
          (open-line)
          (next-line))
      (newline-and-indent))))

If you can make it work, is it possible not to defun a new function, but just put them inside

(global-set-key (kbd "<return>")
...
)
10
  • 1
    If you don't know much elisp yet, this looks like an achievable project. To get started, look up interactive prefixes, cond, eolp, and bolp.
    – Dan
    Commented Nov 29, 2014 at 18:19
  • 1
    Just define a new command (instead of putting a lambda form in global-set-key) that does what you want, and bind it to (kbd "RET").
    – Drew
    Commented Nov 29, 2014 at 18:52
  • 1
    @CodyChan with C-u, the prefix argument is '(4). With C--, it is -1. Just check for its value.
    – Malabarba
    Commented Nov 29, 2014 at 19:45
  • 1
    Be sure to read C-h i g (elisp) Prefix Command Arguments RET
    – phils
    Commented Nov 29, 2014 at 22:11
  • 1
    Everything you need to know about testing and using different prefix arguments is contained C-h v current-prefix-arg and C-h f prefix-numeric-value. And you can check C-h f interactive too. If that's not enough, then read (elisp)Prefix Command Arguments. And above all, try some code. Lisp is interactive - it is simple and immediate to try things out.
    – Drew
    Commented Nov 30, 2014 at 1:56

1 Answer 1

5
  • You need (interactive "P") (not "p") to get a raw prefix argument (i.e. the ability to distinguish between C-u and C-u 4). See C-h f interactive.

  • The raw prefix argument for C-- is not (-1), but -. See C-h i g (elisp) Prefix Command Arguments.

  • open-line takes a required argument.

(defun advanced-return (&optional arg)
  (interactive "P")
  (if (equal arg '-) ;; C--
      (progn
        (beginning-of-line)
        (open-line 1))
    (if (equal arg '(4)) ;; C-u
        (progn
          (end-of-line)
          (open-line 1)
          (next-line))
      (newline-and-indent))))
  • You might also write this as a cond expression, rather than a series of nested ifs. That would be structurally cleaner (although I always think the (lack of) indentation is a problem for readability with cond, so YMMV).
(defun advanced-return (&optional arg)
  (interactive "P")
  (cond ((equal arg '-) ;; C--
         (beginning-of-line)
         (open-line 1))
        ((equal arg '(4)) ;; C-u
         (end-of-line)
         (open-line 1)
         (next-line))
        (t ;; Otherwise
         (newline-and-indent))))
3
  • It there any chance that I can put them into (global-set-key (kbd "<return>")...(not defun a new advanced-return????
    – CodyChan
    Commented Nov 30, 2014 at 1:17
  • 1
    Yes, with a lambda expression, but you don't want that. It messes with the documentation -- C-h k RET would show you the entire function definition -- unformatted, and maybe as byte-code -- instead of a name.
    – phils
    Commented Nov 30, 2014 at 2:02
  • I confirm this works in GNU Emacs 24.3.1 Commented Jan 16, 2015 at 17:07

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.