Skip to main content
Use backticks for syntax highlighting and use double ;; for single line comments
Source Link
(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)
(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)
;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
  ; IF we are not in a TTY, unbind C-i from TAB
    (progn
      (define-key input-decode-map [?\C-i] [C-i])
      ; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  ; ELSE IF we are in a TTY, create a replacement for TAB
  (defun my/tab-replacement (&optional START END)
    (interactive "r")
    (if (use-region-p)
      ; IF active region, use indent-region
        (indent-region START END)
      ; ELSE IF no active region, use default tab command
      (indent-for-tab-command)))
  ; Bind our quick-and-dirty TAB replacement to the TAB key
  (global-set-key (kbd "TAB") 'my/tab-replacement))
;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
    ;; IF we are not in a TTY, unbind C-i from TAB
    (progn
      (define-key input-decode-map [?\C-i] [C-i])
      ;; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  ;; ELSE IF we are in a TTY, create a replacement for TAB
  (defun my/tab-replacement (&optional START END)
    (interactive "r")
    (if (use-region-p)
        ;; IF active region, use indent-region
        (indent-region START END)
      ;; ELSE IF no active region, use default tab command
      (indent-for-tab-command)))
  ;; Bind our quick-and-dirty TAB replacement to the TAB key
  (global-set-key (kbd "TAB") 'my/tab-replacement))
(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)
;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
  ; IF we are not in a TTY, unbind C-i from TAB
    (progn
      (define-key input-decode-map [?\C-i] [C-i])
      ; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  ; ELSE IF we are in a TTY, create a replacement for TAB
  (defun my/tab-replacement (&optional START END)
    (interactive "r")
    (if (use-region-p)
      ; IF active region, use indent-region
        (indent-region START END)
      ; ELSE IF no active region, use default tab command
      (indent-for-tab-command)))
  ; Bind our quick-and-dirty TAB replacement to the TAB key
  (global-set-key (kbd "TAB") 'my/tab-replacement))
(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)
;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
    ;; IF we are not in a TTY, unbind C-i from TAB
    (progn
      (define-key input-decode-map [?\C-i] [C-i])
      ;; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  ;; ELSE IF we are in a TTY, create a replacement for TAB
  (defun my/tab-replacement (&optional START END)
    (interactive "r")
    (if (use-region-p)
        ;; IF active region, use indent-region
        (indent-region START END)
      ;; ELSE IF no active region, use default tab command
      (indent-for-tab-command)))
  ;; Bind our quick-and-dirty TAB replacement to the TAB key
  (global-set-key (kbd "TAB") 'my/tab-replacement))
removed superfluous `progn` as per user mgalgs suggestion
Source Link
nispio
  • 8.3k
  • 2
  • 38
  • 74

I don't think that this can be achieved from a terminal, but in GUI mode you could try this:

(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)

I do the same thing with C-m so that it can be distinguished from RET

EDIT:

The following should work whether you are in GUI or TTY mode:

;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
    (progn
      ; IF we are not in a TTY, unbind C-i from TAB
    (progn
      (define-key input-decode-map [?\C-i] [C-i])
      ; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  (progn
    ; ELSE IF we are in a TTY, create a replacement for TAB
    (defun my/tab-replacement (&optional START END)
      (interactive "r")
      (if (use-region-p)
          ; IF active region, use indent-region
          (indent-region START END)
        ; ELSE IF no active region, use default tab command
        (indent-for-tab-command)))
    ; Bind our quick-and-dirty TAB replacement to the TAB key
    (global-set-key (kbd "TAB") 'my/tab-replacement)))

It is not pretty, but it seems to do the job. I welcome any refinements or edits to this code as necessary.

I don't think that this can be achieved from a terminal, but in GUI mode you could try this:

(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)

I do the same thing with C-m so that it can be distinguished from RET

EDIT:

The following should work whether you are in GUI or TTY mode:

;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
    (progn
      ; IF we are not in a TTY, unbind C-i from TAB
      (define-key input-decode-map [?\C-i] [C-i])
      ; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  (progn
    ; ELSE IF we are in a TTY, create a replacement for TAB
    (defun my/tab-replacement (&optional START END)
      (interactive "r")
      (if (use-region-p)
          ; IF active region, use indent-region
          (indent-region START END)
        ; ELSE IF no active region, use default tab command
        (indent-for-tab-command)))
    ; Bind our quick-and-dirty TAB replacement to the TAB key
    (global-set-key (kbd "TAB") 'my/tab-replacement)))

It is not pretty, but it seems to do the job. I welcome any refinements or edits to this code as necessary.

I don't think that this can be achieved from a terminal, but in GUI mode you could try this:

(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)

I do the same thing with C-m so that it can be distinguished from RET

EDIT:

The following should work whether you are in GUI or TTY mode:

;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
  ; IF we are not in a TTY, unbind C-i from TAB
    (progn
      (define-key input-decode-map [?\C-i] [C-i])
      ; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  ; ELSE IF we are in a TTY, create a replacement for TAB
  (defun my/tab-replacement (&optional START END)
    (interactive "r")
    (if (use-region-p)
      ; IF active region, use indent-region
        (indent-region START END)
      ; ELSE IF no active region, use default tab command
      (indent-for-tab-command)))
  ; Bind our quick-and-dirty TAB replacement to the TAB key
  (global-set-key (kbd "TAB") 'my/tab-replacement))

It is not pretty, but it seems to do the job. I welcome any refinements or edits to this code as necessary.

added a function that should work in either mode
Source Link
nispio
  • 8.3k
  • 2
  • 38
  • 74

I don't think that this can be achieved from a terminal, but in GUI mode, you could try this:

(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)

I do the same thing with C-m so that it can be distinguished from RET

EDIT:

The following should work whether you are in GUI or TTY mode:

;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
    (progn
      ; IF we are not in a TTY, unbind C-i from TAB
      (define-key input-decode-map [?\C-i] [C-i])
      ; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  (progn
    ; ELSE IF we are in a TTY, create a replacement for TAB
    (defun my/tab-replacement (&optional START END)
      (interactive "r")
      (if (use-region-p)
          ; IF active region, use indent-region
          (indent-region START END)
        ; ELSE IF no active region, use default tab command
        (indent-for-tab-command)))
    ; Bind our quick-and-dirty TAB replacement to the TAB key
    (global-set-key (kbd "TAB") 'my/tab-replacement)))

It is not pretty, but it seems to do the job. I welcome any refinements or edits to this code as necessary.

I don't think that this can be achieved from a terminal, but in GUI mode, you could try this:

(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)

I do the same thing with C-m so that it can be distinguished from RET

I don't think that this can be achieved from a terminal, but in GUI mode you could try this:

(define-key input-decode-map [?\C-i] [C-i])
(global-set-key (kbd "<C-i>") 'indent-region)

I do the same thing with C-m so that it can be distinguished from RET

EDIT:

The following should work whether you are in GUI or TTY mode:

;; Unbind <C-i> from the TAB key and bind it to indent-region.
;; Since TAB and <C-i> cannot be differentiated in TTY emacs,
;; the workaround is to conditionally bind TAB to indent-region
;; when there is an active region selected.
(if (window-system)
    (progn
      ; IF we are not in a TTY, unbind C-i from TAB
      (define-key input-decode-map [?\C-i] [C-i])
      ; ... and remap it to indent-region
      (global-set-key (kbd "<C-i>") 'indent-region))
  (progn
    ; ELSE IF we are in a TTY, create a replacement for TAB
    (defun my/tab-replacement (&optional START END)
      (interactive "r")
      (if (use-region-p)
          ; IF active region, use indent-region
          (indent-region START END)
        ; ELSE IF no active region, use default tab command
        (indent-for-tab-command)))
    ; Bind our quick-and-dirty TAB replacement to the TAB key
    (global-set-key (kbd "TAB") 'my/tab-replacement)))

It is not pretty, but it seems to do the job. I welcome any refinements or edits to this code as necessary.

Source Link
nispio
  • 8.3k
  • 2
  • 38
  • 74
Loading