Elisp - Activate and Deactivate Linum-Mode when Goto-Line is Triggered

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I switched to Emacs from Vim and because of Vims functionality of giving keys a prefix, (for example to jump up several lines etc.) i installed relative-linum-mode right away.



Now i found out about Emacs native functionality of jumping to a specific line with M-g M-g (and passing the line number in the minibuffer; the function is called goto-line) and find that much more useful than giving a prefix to C-n or C-p, which is of course not a functionality exclusive to Vim, but i found that to be too tedious in Emacs after all... Maybe i just didn't get used to it.



Now my idea is to have line numbers deactivated most of the time because i find that to look a lot cleaner, but when i press M-g M-g to activate linum-mode, passing the desired line number and after pressing enter and jumping to that line to deactivate linum-mode



Can i bind that to M-g M-g somehow? I know how to rebind keys but i can't figure out how (and if) my desired behavior is possible!










share|improve this question

























    up vote
    2
    down vote

    favorite












    I switched to Emacs from Vim and because of Vims functionality of giving keys a prefix, (for example to jump up several lines etc.) i installed relative-linum-mode right away.



    Now i found out about Emacs native functionality of jumping to a specific line with M-g M-g (and passing the line number in the minibuffer; the function is called goto-line) and find that much more useful than giving a prefix to C-n or C-p, which is of course not a functionality exclusive to Vim, but i found that to be too tedious in Emacs after all... Maybe i just didn't get used to it.



    Now my idea is to have line numbers deactivated most of the time because i find that to look a lot cleaner, but when i press M-g M-g to activate linum-mode, passing the desired line number and after pressing enter and jumping to that line to deactivate linum-mode



    Can i bind that to M-g M-g somehow? I know how to rebind keys but i can't figure out how (and if) my desired behavior is possible!










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I switched to Emacs from Vim and because of Vims functionality of giving keys a prefix, (for example to jump up several lines etc.) i installed relative-linum-mode right away.



      Now i found out about Emacs native functionality of jumping to a specific line with M-g M-g (and passing the line number in the minibuffer; the function is called goto-line) and find that much more useful than giving a prefix to C-n or C-p, which is of course not a functionality exclusive to Vim, but i found that to be too tedious in Emacs after all... Maybe i just didn't get used to it.



      Now my idea is to have line numbers deactivated most of the time because i find that to look a lot cleaner, but when i press M-g M-g to activate linum-mode, passing the desired line number and after pressing enter and jumping to that line to deactivate linum-mode



      Can i bind that to M-g M-g somehow? I know how to rebind keys but i can't figure out how (and if) my desired behavior is possible!










      share|improve this question













      I switched to Emacs from Vim and because of Vims functionality of giving keys a prefix, (for example to jump up several lines etc.) i installed relative-linum-mode right away.



      Now i found out about Emacs native functionality of jumping to a specific line with M-g M-g (and passing the line number in the minibuffer; the function is called goto-line) and find that much more useful than giving a prefix to C-n or C-p, which is of course not a functionality exclusive to Vim, but i found that to be too tedious in Emacs after all... Maybe i just didn't get used to it.



      Now my idea is to have line numbers deactivated most of the time because i find that to look a lot cleaner, but when i press M-g M-g to activate linum-mode, passing the desired line number and after pressing enter and jumping to that line to deactivate linum-mode



      Can i bind that to M-g M-g somehow? I know how to rebind keys but i can't figure out how (and if) my desired behavior is possible!







      key-bindings functions linum-mode






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      Tim Hilt

      486




      486




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Here you go:



          (global-set-key [remap goto-line] #'my-goto-line)

          (defvar my-temp-linum-mode-sym
          (if (fboundp 'display-line-numbers-mode)
          'display-line-numbers-mode
          'linum-mode)
          "Use `display-line-numbers-mode' if it's available; `linum-mode' otherwise.")

          (defun my-goto-line (line &optional buffer)
          "`goto-line' with temporary line-numbering."
          (interactive
          (let ((my-temp-linum-buffer
          ;; Use the same prefix-arg behaviour as `goto-line'.
          (if current-prefix-arg
          (other-buffer (current-buffer) t)
          (current-buffer))))
          ;; If line numbering is currently enabled in that buffer, then we
          ;; want to leave it alone.
          (when (with-current-buffer my-temp-linum-buffer
          (and (boundp my-temp-linum-mode-sym)
          (symbol-value my-temp-linum-mode-sym)))
          (setq my-temp-linum-buffer nil))
          ;; Prompt the user for the line number, using the interactive
          ;; form from `goto-line'. Line numbering will be enabled and
          ;; then disabled again during this procedure.
          (eval (cadr (interactive-form 'goto-line)))))
          ;; Pass the line and buffer arguments to `goto-line'.
          (goto-line line buffer))

          (defun my-temp-linum-set-state (arg)
          "Enable or disable line numbering."
          (and (bound-and-true-p my-temp-linum-buffer)
          (buffer-live-p my-temp-linum-buffer)
          (with-current-buffer my-temp-linum-buffer
          ;; Call the specified mode function.
          (funcall my-temp-linum-mode-sym arg))))

          (defun my-temp-linum-enable-maybe ()
          (my-temp-linum-set-state 1))

          (defun my-temp-linum-disable-maybe ()
          (my-temp-linum-set-state -1))

          (add-hook 'minibuffer-setup-hook #'my-temp-linum-enable-maybe)
          (add-hook 'minibuffer-exit-hook #'my-temp-linum-disable-maybe)





          share|improve this answer






















          • Wow that was quick! I didn't expect such an answer in this small amount of time and honestly, i wouldn't have gotten something similar on my own. Thank you!
            – Tim Hilt
            4 hours ago










          • You're welcome. It seemed like a fun problem to solve. I've added some more comments to the code to explain what it's doing.
            – phils
            4 hours ago











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "583"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f44976%2felisp-activate-and-deactivate-linum-mode-when-goto-line-is-triggered%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Here you go:



          (global-set-key [remap goto-line] #'my-goto-line)

          (defvar my-temp-linum-mode-sym
          (if (fboundp 'display-line-numbers-mode)
          'display-line-numbers-mode
          'linum-mode)
          "Use `display-line-numbers-mode' if it's available; `linum-mode' otherwise.")

          (defun my-goto-line (line &optional buffer)
          "`goto-line' with temporary line-numbering."
          (interactive
          (let ((my-temp-linum-buffer
          ;; Use the same prefix-arg behaviour as `goto-line'.
          (if current-prefix-arg
          (other-buffer (current-buffer) t)
          (current-buffer))))
          ;; If line numbering is currently enabled in that buffer, then we
          ;; want to leave it alone.
          (when (with-current-buffer my-temp-linum-buffer
          (and (boundp my-temp-linum-mode-sym)
          (symbol-value my-temp-linum-mode-sym)))
          (setq my-temp-linum-buffer nil))
          ;; Prompt the user for the line number, using the interactive
          ;; form from `goto-line'. Line numbering will be enabled and
          ;; then disabled again during this procedure.
          (eval (cadr (interactive-form 'goto-line)))))
          ;; Pass the line and buffer arguments to `goto-line'.
          (goto-line line buffer))

          (defun my-temp-linum-set-state (arg)
          "Enable or disable line numbering."
          (and (bound-and-true-p my-temp-linum-buffer)
          (buffer-live-p my-temp-linum-buffer)
          (with-current-buffer my-temp-linum-buffer
          ;; Call the specified mode function.
          (funcall my-temp-linum-mode-sym arg))))

          (defun my-temp-linum-enable-maybe ()
          (my-temp-linum-set-state 1))

          (defun my-temp-linum-disable-maybe ()
          (my-temp-linum-set-state -1))

          (add-hook 'minibuffer-setup-hook #'my-temp-linum-enable-maybe)
          (add-hook 'minibuffer-exit-hook #'my-temp-linum-disable-maybe)





          share|improve this answer






















          • Wow that was quick! I didn't expect such an answer in this small amount of time and honestly, i wouldn't have gotten something similar on my own. Thank you!
            – Tim Hilt
            4 hours ago










          • You're welcome. It seemed like a fun problem to solve. I've added some more comments to the code to explain what it's doing.
            – phils
            4 hours ago















          up vote
          2
          down vote



          accepted










          Here you go:



          (global-set-key [remap goto-line] #'my-goto-line)

          (defvar my-temp-linum-mode-sym
          (if (fboundp 'display-line-numbers-mode)
          'display-line-numbers-mode
          'linum-mode)
          "Use `display-line-numbers-mode' if it's available; `linum-mode' otherwise.")

          (defun my-goto-line (line &optional buffer)
          "`goto-line' with temporary line-numbering."
          (interactive
          (let ((my-temp-linum-buffer
          ;; Use the same prefix-arg behaviour as `goto-line'.
          (if current-prefix-arg
          (other-buffer (current-buffer) t)
          (current-buffer))))
          ;; If line numbering is currently enabled in that buffer, then we
          ;; want to leave it alone.
          (when (with-current-buffer my-temp-linum-buffer
          (and (boundp my-temp-linum-mode-sym)
          (symbol-value my-temp-linum-mode-sym)))
          (setq my-temp-linum-buffer nil))
          ;; Prompt the user for the line number, using the interactive
          ;; form from `goto-line'. Line numbering will be enabled and
          ;; then disabled again during this procedure.
          (eval (cadr (interactive-form 'goto-line)))))
          ;; Pass the line and buffer arguments to `goto-line'.
          (goto-line line buffer))

          (defun my-temp-linum-set-state (arg)
          "Enable or disable line numbering."
          (and (bound-and-true-p my-temp-linum-buffer)
          (buffer-live-p my-temp-linum-buffer)
          (with-current-buffer my-temp-linum-buffer
          ;; Call the specified mode function.
          (funcall my-temp-linum-mode-sym arg))))

          (defun my-temp-linum-enable-maybe ()
          (my-temp-linum-set-state 1))

          (defun my-temp-linum-disable-maybe ()
          (my-temp-linum-set-state -1))

          (add-hook 'minibuffer-setup-hook #'my-temp-linum-enable-maybe)
          (add-hook 'minibuffer-exit-hook #'my-temp-linum-disable-maybe)





          share|improve this answer






















          • Wow that was quick! I didn't expect such an answer in this small amount of time and honestly, i wouldn't have gotten something similar on my own. Thank you!
            – Tim Hilt
            4 hours ago










          • You're welcome. It seemed like a fun problem to solve. I've added some more comments to the code to explain what it's doing.
            – phils
            4 hours ago













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Here you go:



          (global-set-key [remap goto-line] #'my-goto-line)

          (defvar my-temp-linum-mode-sym
          (if (fboundp 'display-line-numbers-mode)
          'display-line-numbers-mode
          'linum-mode)
          "Use `display-line-numbers-mode' if it's available; `linum-mode' otherwise.")

          (defun my-goto-line (line &optional buffer)
          "`goto-line' with temporary line-numbering."
          (interactive
          (let ((my-temp-linum-buffer
          ;; Use the same prefix-arg behaviour as `goto-line'.
          (if current-prefix-arg
          (other-buffer (current-buffer) t)
          (current-buffer))))
          ;; If line numbering is currently enabled in that buffer, then we
          ;; want to leave it alone.
          (when (with-current-buffer my-temp-linum-buffer
          (and (boundp my-temp-linum-mode-sym)
          (symbol-value my-temp-linum-mode-sym)))
          (setq my-temp-linum-buffer nil))
          ;; Prompt the user for the line number, using the interactive
          ;; form from `goto-line'. Line numbering will be enabled and
          ;; then disabled again during this procedure.
          (eval (cadr (interactive-form 'goto-line)))))
          ;; Pass the line and buffer arguments to `goto-line'.
          (goto-line line buffer))

          (defun my-temp-linum-set-state (arg)
          "Enable or disable line numbering."
          (and (bound-and-true-p my-temp-linum-buffer)
          (buffer-live-p my-temp-linum-buffer)
          (with-current-buffer my-temp-linum-buffer
          ;; Call the specified mode function.
          (funcall my-temp-linum-mode-sym arg))))

          (defun my-temp-linum-enable-maybe ()
          (my-temp-linum-set-state 1))

          (defun my-temp-linum-disable-maybe ()
          (my-temp-linum-set-state -1))

          (add-hook 'minibuffer-setup-hook #'my-temp-linum-enable-maybe)
          (add-hook 'minibuffer-exit-hook #'my-temp-linum-disable-maybe)





          share|improve this answer














          Here you go:



          (global-set-key [remap goto-line] #'my-goto-line)

          (defvar my-temp-linum-mode-sym
          (if (fboundp 'display-line-numbers-mode)
          'display-line-numbers-mode
          'linum-mode)
          "Use `display-line-numbers-mode' if it's available; `linum-mode' otherwise.")

          (defun my-goto-line (line &optional buffer)
          "`goto-line' with temporary line-numbering."
          (interactive
          (let ((my-temp-linum-buffer
          ;; Use the same prefix-arg behaviour as `goto-line'.
          (if current-prefix-arg
          (other-buffer (current-buffer) t)
          (current-buffer))))
          ;; If line numbering is currently enabled in that buffer, then we
          ;; want to leave it alone.
          (when (with-current-buffer my-temp-linum-buffer
          (and (boundp my-temp-linum-mode-sym)
          (symbol-value my-temp-linum-mode-sym)))
          (setq my-temp-linum-buffer nil))
          ;; Prompt the user for the line number, using the interactive
          ;; form from `goto-line'. Line numbering will be enabled and
          ;; then disabled again during this procedure.
          (eval (cadr (interactive-form 'goto-line)))))
          ;; Pass the line and buffer arguments to `goto-line'.
          (goto-line line buffer))

          (defun my-temp-linum-set-state (arg)
          "Enable or disable line numbering."
          (and (bound-and-true-p my-temp-linum-buffer)
          (buffer-live-p my-temp-linum-buffer)
          (with-current-buffer my-temp-linum-buffer
          ;; Call the specified mode function.
          (funcall my-temp-linum-mode-sym arg))))

          (defun my-temp-linum-enable-maybe ()
          (my-temp-linum-set-state 1))

          (defun my-temp-linum-disable-maybe ()
          (my-temp-linum-set-state -1))

          (add-hook 'minibuffer-setup-hook #'my-temp-linum-enable-maybe)
          (add-hook 'minibuffer-exit-hook #'my-temp-linum-disable-maybe)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 4 hours ago

























          answered 5 hours ago









          phils

          24k23261




          24k23261











          • Wow that was quick! I didn't expect such an answer in this small amount of time and honestly, i wouldn't have gotten something similar on my own. Thank you!
            – Tim Hilt
            4 hours ago










          • You're welcome. It seemed like a fun problem to solve. I've added some more comments to the code to explain what it's doing.
            – phils
            4 hours ago

















          • Wow that was quick! I didn't expect such an answer in this small amount of time and honestly, i wouldn't have gotten something similar on my own. Thank you!
            – Tim Hilt
            4 hours ago










          • You're welcome. It seemed like a fun problem to solve. I've added some more comments to the code to explain what it's doing.
            – phils
            4 hours ago
















          Wow that was quick! I didn't expect such an answer in this small amount of time and honestly, i wouldn't have gotten something similar on my own. Thank you!
          – Tim Hilt
          4 hours ago




          Wow that was quick! I didn't expect such an answer in this small amount of time and honestly, i wouldn't have gotten something similar on my own. Thank you!
          – Tim Hilt
          4 hours ago












          You're welcome. It seemed like a fun problem to solve. I've added some more comments to the code to explain what it's doing.
          – phils
          4 hours ago





          You're welcome. It seemed like a fun problem to solve. I've added some more comments to the code to explain what it's doing.
          – phils
          4 hours ago


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f44976%2felisp-activate-and-deactivate-linum-mode-when-goto-line-is-triggered%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery