In emacs, how do I align closing parentheses with the start of the opening line?

11

5

The default indentation style for multi-line function calls in many emacs modes is to align the closing round bracket with the other arguments to the function, thus:

function_one(
    arg1,
    arg2
    );

I would prefer it if the closing bracket aligned with the start of the line that contains the opening bracket. For example:

function_one(
    function_two(
        f2_arg1,
        f2_arg2
    ),
    f1_arg2,
    f1_arg3
);

How do I do this?

mavit

Posted 2013-01-16T20:15:32.143

Reputation: 541

1I have been ignoring the same urge for years! Today is the day to fix it. – Prof. Falken contract breached – 2018-01-12T08:26:13.270

Answers

12

  • For the many modes derived from CC Mode (for example, c-mode, java-mode, php-mode), customise c-offsets-alist so that arglist-close is set to c-lineup-close-paren.
  • For cperl-mode, customise cperl-indent-parens-as-block to true.
  • For cperl-mode, GNU Emacs 24.3+, set cperl-close-paren-offset to the negative of cperl-indent-level
  • For perl-mode in GNU emacs 24.3 and greater, customise perl-indent-parens-as-block to true.
  • For python-mode, this behaviour is found in GNU emacs 24.3 and greater.

You can customise a variable by typing M-x customize-variable. Alternatively, add the following lines to your ~/.emacs:

(add-to-list 'c-offsets-alist '(arglist-close . c-lineup-close-paren))
(setq cperl-indent-parens-as-block t)
(setq perl-indent-parens-as-block t)

mavit

Posted 2013-01-16T20:15:32.143

Reputation: 541

Is there any way of getting this to work in python-mode with Emacs 23.1.1? – ishmael – 2014-10-10T03:31:22.387

The solution for Emacs 23.1.1 is simply to get the latest python-mode.el here. Untar it in your ~/.emacs.d directory, then in your .emacs file, add this: (add-to-list 'load-path (expand-file-name "~/.emacs.d/python-mode.el-6.1.3")) (require 'python-mode)

– ishmael – 2014-10-10T04:13:23.283

And how do I make this work in C or C++? – Prof. Falken contract breached – 2018-01-12T08:45:46.743

Is there any way to get the other style in python-mode on Emacs >= 24.3? – mgalgs – 2018-10-05T20:45:16.097