home ~ projects ~ socials

Using autopep8 To Format Python Files In Neovim

Install autopep8

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

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

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

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

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.)

-- end of line --

References