Skip to content

Instantly share code, notes, and snippets.

@joshwnj
Created August 8, 2012 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshwnj/3292750 to your computer and use it in GitHub Desktop.
Save joshwnj/3292750 to your computer and use it in GitHub Desktop.
;; turn line numbers off by default
(global-linum-mode -1)
(defun goto-line-with-feedback (&optional line)
"Show line numbers temporarily, while prompting for the line number input"
(interactive "P")
(if line
(goto-line line)
(progn
(linum-mode 1)
(let ((line (read-number "Line: ")))
(goto-line line))
(linum-mode -1))))
(global-set-key (kbd "C-l") 'goto-line-with-feedback)
@joshwnj
Copy link
Author

joshwnj commented Aug 8, 2012

To do:

  • is there any worth in making this a global minor mode?
  • when you activate line numbers, and then exit (eg. C-g) the line numbers stay visible. Is there a way to catch the exit?

@pragdave
Copy link

You can use unwind-protect:

(defun goto-line-with-feedback (&optional line)
  "Show line numbers temporarily, while prompting for the line number input"
  (interactive "P")
  (if line
      (goto-line line)
    (unwind-protect
        (progn
          (linum-mode 1)
          (let ((line (read-number "Line: ")))
            (goto-line line)))
          (linum-mode -1))))

@avatar-lavventura
Copy link

@pragdave Why are you using linum-mode, instead of the built-in line numbers that Eli Z. implemented starting from Emacs 26 and forwards? linum-mode is officially deprecated. The replacement is display-line-numbers-mode / global-display-line-numbers-mode: https://www.gnu.org/software/emacs/manual/html_node/emacs/Display-Custom.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment