Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Using autopep8 To Format Python Files In Neovim

Install autopep8

Code

pip3 install autopep8

Install null-ls language server by putting this in ~/.config/nvim/init.lua. (Note, I'm using _lasy__ to manage plugins based off a kickstarer config. You'll need to change out if you're using another one but the plug-in name should remain the same

Code

require('lazy').setup({
  --  Other plugins....

  { 'jose-elias-alvarez/null-ls.nvim' },
}

Lower in the `~/.config/nvim/init.lua`` file add this:

Code

local null_ls = require("null-ls")
null_ls.setup({
    sources = {
      null_ls.builtins.formatting.autopep8
    },
})

You can either run `:Format`` manually or setup something to autoformat on save. (There are several ways, but I don't currently have a post up about how to do that.)

References