Susensio / magic-bang.nvim

Neovim plugin written in pure Lua that automagically inserts a shebang line when needed
MIT License
7 stars 2 forks source link
lua neovim nvim nvim-plugin shebang

🪄 #Magic Bang!

A simple Neovim plugin written in pure Lua that automagically inserts a shebang line when needed and makes the file executable.

How it works

sample

It checks if the current file is in $PATH and reads its extension to insert the corresponding shebang. If no extension is present, default shebang is used.

On exit, the file is made executable only if the shebang is still present.

:Bang command adds a shebang manually.

Installation

lazy with lazyloading

{
  "susensio/magic-bang.nvim",
  config = true,
  event = "BufNewFile",
  cmd = "Bang",
}

packer

use {
    "susensio/magic-bang.nvim",
    config = function() require("magic-bang").setup() end
}

vim-plug

Plug "susensio/magic-bang.nvim"
lua require("magic-bang").setup()

Usage

If a new file is in $PATH, the :Bang command is fired automatically. A manual user command is provided :Bang [<binary>] with an optional binary argument, usefull when editing previously created files or source files without extension.

-- Try to set shebang based on extension, or insert default shebang
:Bang

-- Force specific shebang by declaring binary
:Bang python3

Customization

You can set custom shebangs or override defaults in setup with bins = { extension = binary }, using either a binary command (which will be resolved with /usr/bin/env) or a full path:

require("magic-bang").setup({
    bin = {
        ksh = "/usr/bin/ksh",
        py = "python3.11",
        scala = nil
    }
})

Default options are:

{
    bins = {
        awk = "awk",
        hs = "runhaskell",
        jl = "julia",
        lua = "lua",
        m = "octave",
        mak = "make",
        php = "php",
        pl = "perl",
        py = "python3",
        r = "Rscript",
        rb = "ruby",
        scala = "scala",
        sh = "bash",
        tcl = "tclsh",
        tk = "wish",
    },
    automatic = true,         -- insert shebang on new file when in $PATH
    command = true,           -- define Bang user command
    executable = true,        -- make file executable on exit
    default = "/bin/bash"     -- default shebang for `:Bang` without args
}

Acknowledgments

To samirettali for his shebang.nvim that inspired this pluggin.

FAQ

Q: How does it differ from forked samirettali/shebang.nvim?

A: First, it only makes a file executable if the shebang is still present on exit. This prevent false positives. Second, a :Bang user command can manually add a shebang, helping with false negatives.