1

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?

  • 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 6 '17 at 10:07
  • @wvxvw it solves the problem unless I am using tabs for indentation! – Startec Nov 7 '17 at 1:13
  • 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 7 '17 at 6:46
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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.