RSS Amplifier

Rida Ayed · Feb 14, 2025

Ignore regions when formatting buffers with three keys

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

I’m quite fond of keeping my code formatted. For example I use yapf to format python code. Sometimes I want particular regions not to be formatted . In particular I want rows to stay rows but one line. The solution is to indicate such regions for the formatter Without indication, the buffer is formatted from this: rows = [ ( 'john' , 'acme' ), ( 'jane' , 'acme' ) ] to that: rows = [( 'john'…

I’m quite fond of keeping my code formatted.
For example I use yapf to format python code.
Sometimes I want particular regions not to be formatted.
In particular I want rows to stay rows but one line.

The solution is to indicate such regions for the formatter

Without indication, the buffer is formatted from this:

rows = [
    ('john', 'acme'),
    ('jane', 'acme')
]

to that:

rows = [('john', 'acme'), ('jane', 'acme')]

With added indication the formatter ignores such regions:

rows = [
    # yapf: disable
    ('john', 'acme'),
    ('jane', 'acme')
    # yapf: enable
]

Now manually typing these opening and closing indicators each time would be a bit cumbersome, wouldn’t it?
Luckily emacs’ evil-surround has you covered.

Add this to your init.el

(with-eval-after-load 'evil-surround
  (setq-default evil-surround-pairs-alist
                (push '(?y . ("# yapf: disable" . "# yapf: enable")) evil-surround-pairs-alist)))

Now visually selecting any desired region and hitting gSy surrounds as explained.

Read on /posts/ignore-regions-when-formatting-buffers/

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.