Elisp - Activate and Deactivate Linum-Mode when Goto-Line is Triggered
Clash 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!
key-bindings functions linum-mode
add a comment |Â
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!
key-bindings functions linum-mode
add a comment |Â
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!
key-bindings functions linum-mode
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
key-bindings functions linum-mode
asked 6 hours ago


Tim Hilt
486
486
add a comment |Â
add a comment |Â
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)
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
add a comment |Â
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)
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
add a comment |Â
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)
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
add a comment |Â
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)
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)
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password