1

I am using tab for indent alignment, I believe its a built-in emacs behavior in python-mode. In most cases if cursor is at the beginning of the line and when I press TAB, first** cursor jumps to first character after the white-space, and on the the second TAB it starts aligning the indent. I show the cursor's location using [|].

Normal behavior example: After I press TAB, cursor jumps right next to if

________________________________________________ first TAB_________________________second TAB

 for j in range(10):       |   for j in range(10):       |     for j in range(10):
[|]   if idx > 0:          |      [|]if idx > 0:         |  [|]if idx > 0:    
         print("hello")    |             print("hello")  |         print("hello")

But it does not work on the second if condition. If there is a second inner if condition or its else and my cursor is at the beginning of its line, when I press TAB, it adds indent and jump right next to if.

 for j in range(10):        |   for j in range(10): 
      if idx > 0:           |       if idx > 0: 
          print("hello")    |           print("hello") 
                            |
 [|]  if idx > 0:           |        [|]if idx > 0:  # After the first TAB
          print("hello")    |           print("hello") 
 [|]  else:                 |        [|]else:  # After the first TAB
        print("doo")        |           print("doo")

Is it possible instead to first jump next to if like on the first example and on the second TAB perform the indent alignment?

Wanted behavior:

 for j in range(10):       |   for j in range(10): 
      if idx > 0:          |       if idx > 0: 
          print("hello")   |           print("hello") 
                           |
 [|]  if idx > 0:          |    [|]if idx > 0:  # on the second TAB indent alignment will be done
          print("hello")   |           print("hello") 
 [|]  else:                |    [|]else:
          print("doo")     |           print("doo")

My setup:

(add-hook 'python-mode-hook 'lsp)
(add-hook 'python-mode-hook #'lsp-deferred)

(add-hook 'python-mode-hook
  (lambda ()
    (setq indent-tabs-mode  nil
          python-indent     4
          tab-width         4)
    (let ((inhibit-message  t))
      (my-set-fill-column 80))))

Example Python code:

#!/usr/bin/env python3

for j in range(10):
    if idx > 0:
        print("hello")

    if idx == 1:
        if idx == 1:
            print("hello")
    else:
        print("some")

Possible approach which did not work:

(defun smarter-move-beginning-of-line (arg)
  (interactive "^p")
  (setq arg (or arg 1))

  ;; Move lines first
  (when (/= arg 1)
    (let ((line-move-visual nil))
      (forward-line (1- arg))))

  (let ((orig-point (point)))
    (back-to-indentation)
    (when (= orig-point (point))
      (indent-for-tab-command))))
4
  • Maybe this can help: emacsredux.com/blog/2013/05/22/… It does your wanted behaviour
    – Swedgin
    Jun 8 at 8:57
  • @Swedgin Some how we need to merge the solution on the linked comment along with tag alignment. After jumping to the beginning of the line, if its on the begginning I want to apply alignment
    – alper
    Jun 10 at 13:28
  • Yes, sadly my elisp isn't that good to do that :)
    – Swedgin
    Jun 11 at 7:02
  • 1
    @Swedgin ah same here :( Hope a emacs gurue would help us out :)
    – alper
    Jun 11 at 14:50

Your Answer

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

Browse other questions tagged or ask your own question.