emacsspacemacs

Emacs and python-mode automatic indentation


python-mode automatic indentation makes a lot of assumptions, it seems, based on style. How can I configure the way it indents in certain cases, or alternatively, make it indent like vim.

For example, if I have the following function:

def some_function(
    arg1,
    arg2,
):
    pass

If I go to the end of arg2 line, press enter, and type arg3,, this is what I will get:

def some_function(
    arg1,
    arg2,
        arg3,
):
    pass

What I want is:

def some_function(
    arg1,
    arg2,
    arg3,
):
    pass

How can I make it do what I want here?

I've tried (setq indent-line-function 'indent-relative), which sort of does what I want, except if I hit enter twice, the cursor will be at the begging of the line (it seems like it only examines the last line to determine relativity).

In vim, I can type def some_function( and press enter, it will automatically indent 4 spaces. If I type enter twice, it will still be indented 4 spaces. This is what I'd like most.

Thanks in advance!


Solution

  • Looks like this solves the automatic indentation problem that I'm experiencing. This is NOT how to get emacs to use vim style indentation. If I figure that out, I'll add it to this answer.

    (add-hook 'python-mode-hook
              (lambda ()
                (setq python-indent-def-block-scale 1)))