A NeoVim plugin written in Lua that aims to automatically saving file(s) when changed.
Neovim version >= 0.9.0
(or nightly)
For Neovim
0.5.0
<= version <0.9.0
, use tagv1.0
.
- Automatically save current file(s).
- Set conditions that file(s) must meet to be saved (e.g. filetype, existence, ...).
- Specific events that will trigger the plugin.
- Custom hook(s) (e.g. print a message when the plugin is enabled).
- Toggle (enable if disabled, disable if enabled) the plugin.
Screenshots
-
-- lua require('lazy').setup({ '0x00-ketsu/autosave.nvim', -- lazy-loading on events event = { "InsertLeave", "TextChanged" }, config = function() require('autosave').setup { -- your configuration comes here -- or leave it empty to use the default settings -- refer to the configuration section below } end })
-
-- Lua use { '0x00-ketsu/autosave.nvim', config = function() require('autosave').setup { -- your configuration comes here -- or leave it empty to use the default settings -- refer to the configuration section below } end }
Following defaults:
local autosave = require('autosave')
autosave.setup(
{
enable = true,
prompt_style = 'stdout',
prompt_message = function()
return 'Autosave: saved at ' .. vim.fn.strftime('%H:%M:%S')
end,
events = {'InsertLeave', 'TextChanged'},
conditions = {
exists = true,
modifiable = true,
filename_is_not = {},
filetype_is_not = {}
},
write_all_buffers = false,
debounce_delay = 135
}
)
For details, check out the Configuration section.
:ASToggle
: toggle the plugin on and off.:ASEnable
: enable autosave.nvim.:ASDisable
: disable autosave.nvim.
Although settings already have self-explanatory names, here is where you can find info about each one of them and their classifications!
enable
: (boolean) if true, enables autosave.nvim at startup, same as:ASEnable
.prompt_style
: (string) style to displayprompt_message
, choices:stdout
notify
prompt_message
: (string) message to be displayed when saving file(s).events
: (table): events that will trigger the plugin.write_all_buffers
: (boolean) if true, writes to all modifiable buffers that meet theconditions
.debounce_delay
: (integer) if greater than 0, saves the file at most everydebounce_delay
milliseconds, vastly improving editing performance. If 0 then saves are performed immediately afterevents
occur. It's recommend to leave the default value (135
), which is just long enough to reduce unnecessary saves, but short enough that you don't notice the delay.
These are the conditions that every file must meet so that it can be saved. If every file to be auto-saved doesn't meet all of the conditions it won't be saved.
exists
: (boolean) if true, enables this condition. If the file doesn't exist it won't save it (e.g. if younvim stuff.txt
and don't save the file then this condition won't be met)modifiable
: (boolean) if true, enables this condition. If the file isn't modifiable, then this condition isn't met.filename_is_not
: (table, string) if there is one or more filenames (should be array) in the table, it enables this condition. Use this to exclude filenames that you don't want to automatically save.filetype_is_not
: (table, string) if there is one or more filetypes (should be array) in the table, it enables this condition. Use this to exclude filetypes that you don't want to automatically save.
Function | Description |
---|---|
hook_before_enable() | Before activate the plugin |
hook_after_enable() | After activate the plugin |
hook_before_disable() | Before disactivate the plugin |
hook_after_disable() | After disactivate the plugin |
hook_before_saving() | Before saving file(s) |
hook_after_saving | After saving file(s) |
e.g.
hook_after_disable
local autosave = require('autosave')
autosave.hook_after_disable = function ()
print('Do sth after disactivate the plugin')
end
hook_before_saving
local autosave = require('autosave')
autosave.hook_before_saving = function ()
if true then
vim.g.auto_save_abort = true -- Save will be aborted
end
end
-
Check if the plugin is enabled
vim.g.autosave_state
(variable type isboolean
)e.g.
local is_enable_autosave = vim.g.autosave_state and ' 💾 ' or ''
-
Enable the plugin
require('autosave.action').enable()
-
Disable the plugin
require('autosave.action').disable()
-
Toggle (enable if disabled, disable if enabled) the plugin
require('autosave.action').toggle()
MIT