Skip to content
Cameron Ring edited this page Aug 21, 2025 · 1 revision

By default, when nvim is run with a single directory argument, AutoSession will try to restore the session for that directory. If nvim is run with multiple directories or any file arguments, AutoSession won't try to restore a session and won't auto-save a session on exit (if enabled). Those behaviors can be changed with these config parameters:

opts = {
  args_allow_single_directory = true, -- boolean Follow normal session save/load logic if launched with a single directory as the only argument
  args_allow_files_auto_save = false, -- boolean|function Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
}

For args_allow_single_directory, if you frequently use netrw to look at directories, you might want to add it to bypass_save_filetypes if you don't want to create a session for each directory you look at:

opts = {
  bypass_save_filetypes = { "netrw" },
}

Also, if you use a plugin that handles directory arguments (e.g. file trees/explorers), it may prevent AutoSession from loading or saving sessions when launched with a directory argument. You can avoid that by lazy loading that plugin (e.g. Oil, NvimTree).

If args_allow_files_auto_save is true, AutoSession won't load any session when nvim is launched with file argument(s) but it will save on exit. What's probably more useful is to set args_allow_files_auto_save to a function that returns true if a session should be saved and false otherwise. AutoSession will call that function on auto save when run with arguments. Here's one example config where it will save the session if at least two buffers are open after being launched with arguments:

require("auto-session").setup {
  args_allow_files_auto_save = function()
    local supported = 0

    local buffers = vim.api.nvim_list_bufs()
    for _, buf in ipairs(buffers) do
      -- Check if the buffer is valid and loaded
      if vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_buf_is_loaded(buf) then
        local path = vim.api.nvim_buf_get_name(buf)
        if vim.fn.filereadable(path) ~= 0 then
          supported = supported + 1
        end
      end
    end

    -- If we have more 2 or more supported buffers, save the session
    return supported >= 2
  end,
}

Another possibility is to only save the session if there are at least two windows with buffers backed by normal files:

require("auto-session").setup {
  args_allow_files_auto_save = function()
    local supported = 0

    local tabpages = vim.api.nvim_list_tabpages()
    for _, tabpage in ipairs(tabpages) do
      local windows = vim.api.nvim_tabpage_list_wins(tabpage)
      for _, window in ipairs(windows) do
        local buffer = vim.api.nvim_win_get_buf(window)
        local file_name = vim.api.nvim_buf_get_name(buffer)
        if vim.fn.filereadable(file_name) ~= 0 then
          supported = supported + 1
        end
      end
    end

    -- If we have 2 or more windows with supported buffers, save the session
    return supported >= 2
  end,
}

Clone this wiki locally