Skip to main content
  1. Blog/

Using Nushell in neovim

·394 words·2 mins

I took me a little while completely getting Nushell to work as the shell in neovim, but having Nushell at my fingertips directly in my text editor was well worth the effort.

I am using astronvim and altered the astrocore.lua file like so:

    shell = "/home/lk/.cargo/bin/nu",
    shellcmdflag = "--login --stdin --no-newline -c",
    shellredir = "out+err> %s",
    shellpipe = "| complete | update stderr { ansi strip } | tee { get stderr | save --force --raw %s } | into record",
    shelltemp = false,
    shellxescape = "",
    shellxquote = "",
    shellquote = "",

Some more generel explanation and settings can be found here:

integrations/nvim/init.lua at main · Nushell/integrations

Xavier xav.ie was so helpful as to provide some further details for people using some other (posix) shells in addition to Nushell.

  -- better nu support in nvim
  -- https://www.kiils.dk/en/blog/2024-06-22-using-nushell-in-neovim/
  local posix_shell_options = {
    shellcmdflag = "-c",
    shellpipe = "2>&1 | tee",
    shellquote = "",
    shellredir = ">%s 2>&1",
    shelltemp = true,
    shellxescape = "",
    shellxquote = "",
  }

  local nu_shell_options = {
    shellcmdflag = "--login --stdin --no-newline -c",
    shellpipe = "| complete | update stderr { ansi strip } | tee { get stderr | save --force --raw %s } | into record",
    shellquote = "",
    shellredir = "out+err> %s",
    shelltemp = false,
    shellxescape = "",
    shellxquote = "",
  }

  local function set_options(options)
    for k, v in pairs(options) do
      vim.opt[k] = v
    end
  end

  local function apply_shell_options()
    -- check if the shell ends with "nu"
    if vim.opt.shell:get():match("nu$") ~= nil then
      set_options(nu_shell_options)
    else
      set_options(posix_shell_options)
    end
  end

  apply_shell_options()

  -- listen for changes to the shell option
  vim.api.nvim_create_autocmd("OptionSet", {
    pattern = "shell",
    callback = function()
      apply_shell_options()
    end,
  })

As you can see, it just makes it so it only applies nu options if your shell is already nu (mine inherits from $env.SHELL). If your shell is not nu, then it uses the posix shell options (the defaults).

Thanks, Xavier!

PS. I added the --login flag to get acces to my custom commands. They are very useful for me in nvim since I use it mainly to generate journalistic content. Especially my custom commands concerning aichat and “tucs”.

To read more about tuc and generative ai, see this blog posting:

Working with tuc in Nushell · Kiils

And more generally about tuc as a data structure, please see:

tuc as text based data structure for online research · Kiils