3

In certain modes, such as Python I have noticed that Emacs will not indent how I want it it to.

It is getting "smart".

For instance, in this block of code:

def vec_select(veclist, k):                                                                                                                                                                                        
    """Return a sublist of veclist consisisting of the vectors v in veclist where v[k] is zero                                                                                                                     

    Args:                                                                                                                                                                                                          
        veclist: A list of vectors over the same domain                                                                                                                                                            
        k: An element of the domain                                                                                                                                                                                
    #RIGHT HERE

    """

I am unable to double indent (where #RIGHT HERE is written).

Going to the beginning of a line and pressing tabs makes the text flush with "Args" (one indent) but pressing TAB again unindents (to the beginning of the line).

I find this behavior very annoying. When I press TAB I want to indent (regardless of what mode I am in).

Is there a way to force this?

Drew
  • 75,699
  • 9
  • 109
  • 225
Startec
  • 1,354
  • 1
  • 13
  • 30
  • 1
    What you describe is specific to Python mode, it's not universal. In Python mode the authors decided to implement an action for `TAB` that will cycle through all possible indents. This is not ideal for docstrings. A good solution to your problem would be to have Python mode docstrings to override the mode's default indentation function to do something that makes more sense in that context. I could try to write up some code to do that, but it will take time... (possibly, not today), so this isn't a complete answer. Anyways, typing `SPC` four times is annoying, but solves the problem in the end. – wvxvw Nov 06 '17 at 10:07
  • @wvxvw it solves the problem unless I am using tabs for indentation! – Startec Nov 07 '17 at 01:13
  • 1
    Well, you can always force Emacs to insert a character literally, overriding whatever current mode keymap would do by typing `C-q` followed by the desired character, so `C-q TAB` in your case. – wvxvw Nov 07 '17 at 06:46
  • Related: https://emacs.stackexchange.com/questions/21086/python-docstrings-only-allow-two-indent-levels and https://emacs.stackexchange.com/questions/26435/how-can-i-disable-indentation-rules-within-docstrings-in-python-mode – npostavs Feb 04 '18 at 19:11

2 Answers2

1

I use the following functions to indent and unindent line or region:

(defun region-line-beg ()
  (if (region-active-p)
      (save-excursion (goto-char (region-beginning)) (line-beginning-position))
    (line-beginning-position)))
(defun region-line-end ()
  (if (region-active-p)
      (save-excursion (goto-char (region-end)) (line-end-position))
    (line-end-position)))
(defun keyboard-indent (&optional arg)
  (interactive)
  (let ((deactivate-mark nil))  ; keep region
    (indent-rigidly (region-line-beg) (region-line-end) (* (or arg 1) tab-width))))
(defun keyboard-unindent (&optional arg)
  (interactive)
  (keyboard-indent (* -1 (or arg 1))))

You can bind TAB to keyboard-indent to indent line or region.

AhLeung
  • 1,083
  • 5
  • 14
1

Indentation in Emacs is usually part of the rules for the particular mode you are in. I would not recommend overwriting with global rules, instead look into the particular mode and see how indentation can be configured.

If you literally want to insert a tab character you can always use C-q tab

Erik B
  • 156
  • 5