Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 319147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:38:05+00:00 2026-05-12T08:38:05+00:00

I am looking for any way to have Emacs format a Python buffer by

  • 0

I am looking for any way to have Emacs format a Python buffer by hitting a few keys. By format, I mean:

  1. Replace tabs with 4 spaces
  2. Wrap all long lines correctly at 79 chars. This includes wrapping & concatenating long strings, wrapping long comments, wrapping lists, function headers, etc.
  3. Unrelated, but when I hit enter, it’d be nice if the cursor was tabbed in automatically.

In general I’d like to just format everything according to PEP 8.

I’ve looked for a pretty printer / code beautifier / code formatter for Python to run the buffer through, but can’t find an open source one.

My .emacs is here.

For those who are going to answer “You don’t need a formatter for Python, it’s beautiful by the nature of the language,” I say to you that this is not correct. In real software systems, comments should be auto-wrapped for you, strings are going to be longer than 79 characters, tab levels run 3+ deep. Please just help me solve my issue directly without some philosophical discussion about the merits of formatting Python source.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:38:05+00:00Added an answer on May 12, 2026 at 8:38 am

    To change tabs into spaces and fill comments at the same time, you can use this command:

    (defun my-format-python-text ()
      "untabify and wrap python comments"
      (interactive)
      (untabify (point-min) (point-max))
      (goto-char (point-min))
      (while (re-search-forward comment-start nil t)
        (call-interactively 'fill-paragraph)
        (forward-line 1)))
    

    Which you can bind to the key of your choice, presumably like so:

    (eval-after-load "python"
      '(progn
         (define-key python-mode-map (kbd "RET") 'newline-and-indent)
         (define-key python-mode-map (kbd "<f4>") 'my-format-python-text)))
    

    Note the setting of the RET key to automatically indent.

    If you wanted to all tabs with spaces with just built-in commands, this is a possible sequence:

    C-x h           ;; mark-whole-buffer
    M-x untabify    ;; tabs->spaces
    

    To get the fill column and tab width to be what you want, add to your .emacs:

    (setq fill-column 79)
    (setq-default tab-width 4)
    

    Arguably, the tab-width should be set to 8, depending on how other folks have indented their code in your environment (8 being a default that some other editors have). If that’s the case, you could just set it to 4 in the 'python-mode-hook. It kind of depends on your environment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take them from the right. The one before the ;… May 13, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer It is possible to use the Jet replication built into… May 13, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer Throw in a check at the start of you function… May 13, 2026 at 1:11 am

Related Questions

In various IDEs, typing an open-curly results in the appearance of a matched pair
At work I use ClearCase and SourceSafe, but have found some time to do
(I am not talking about Hibernate or NHibernate ORM ) Windows OS (and some
I am looking for a way to quickly compare the state of a database

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.