All Projects → rktjmp → fwatch.nvim

rktjmp / fwatch.nvim

Licence: MIT license
fwatch.nvim lets you watch files or directories for changes and then run vim commands or lua functions.

Programming Languages

lua
6591 projects

Projects that are alternatives of or similar to fwatch.nvim

Visual Split.vim
Vim plugin to control splits with visual selections or text objects
Stars: ✭ 190 (+233.33%)
Mutual labels:  neovim-plugin
Vim Vsnip
Snippet plugin for vim/nvim that supports LSP/VSCode's snippet format.
Stars: ✭ 224 (+292.98%)
Mutual labels:  neovim-plugin
package-info.nvim
✍️ All the npm/yarn commands I don't want to type
Stars: ✭ 248 (+335.09%)
Mutual labels:  neovim-plugin
Neovim Ruby
Ruby support for Neovim
Stars: ✭ 202 (+254.39%)
Mutual labels:  neovim-plugin
Lush.nvim
Define Neovim themes as a DSL in lua, with real-time feedback.
Stars: ✭ 204 (+257.89%)
Mutual labels:  neovim-plugin
Coc.nvim
Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.
Stars: ✭ 18,268 (+31949.12%)
Mutual labels:  neovim-plugin
Instant.nvim
collaborative editing in Neovim using built-in capabilities
Stars: ✭ 178 (+212.28%)
Mutual labels:  neovim-plugin
auto-session
A small automated session manager for Neovim
Stars: ✭ 688 (+1107.02%)
Mutual labels:  neovim-plugin
Indent Blankline.nvim
Indent guides for Neovim
Stars: ✭ 203 (+256.14%)
Mutual labels:  neovim-plugin
ftFT.nvim
I love highlights! I love f{char}!
Stars: ✭ 28 (-50.88%)
Mutual labels:  neovim-plugin
Lualine.nvim
A blazing fast and easy to configure neovim statusline written in pure lua.
Stars: ✭ 198 (+247.37%)
Mutual labels:  neovim-plugin
Nvim Go
Go development plugin for Neovim written in pure Go
Stars: ✭ 207 (+263.16%)
Mutual labels:  neovim-plugin
sphinx.nvim
Sphinx integrations for Neovim
Stars: ✭ 64 (+12.28%)
Mutual labels:  neovim-plugin
Vim Packager
Vim plugin manager that utilizes "jobs" and "pack" features.
Stars: ✭ 197 (+245.61%)
Mutual labels:  neovim-plugin
cmp-tmux
Tmux completion source for nvim-cmp and nvim-compe
Stars: ✭ 98 (+71.93%)
Mutual labels:  neovim-plugin
Deoplete Clang
deoplete.nvim source for C/C++/Obj-C/Obj-C++ with clang-python3
Stars: ✭ 186 (+226.32%)
Mutual labels:  neovim-plugin
Vim Ghost
Vim/Nvim client for GhostText - Edit browser text areas in Vim/Neovim
Stars: ✭ 245 (+329.82%)
Mutual labels:  neovim-plugin
nvim-config
My neovim config
Stars: ✭ 63 (+10.53%)
Mutual labels:  neovim-plugin
vim-readme-viewer
📖 Viewing plugin's README easily like vim help
Stars: ✭ 26 (-54.39%)
Mutual labels:  neovim-plugin
qf helper.nvim
A collection of improvements for the quickfix buffer
Stars: ✭ 70 (+22.81%)
Mutual labels:  neovim-plugin

fwatch.nvim

fwatch.nvim lets you watch files or directories for changes and then run vim commands or lua functions.

fwatch.watch("a_file", "echo 'it changed!'")
fwatch.watch("b_file", {
  on_event = function()
    print("it changed!")
  end
})

The code is extremely simple and I encourage you to read the source, as you may not require a "plugin" to achieve your goals. fwatch.nvim may just act as some example code for you.

Requirements

  • Neovim
    • Tested with 0.5, 0.4 probably ok.
  • Uses libuv/luv, which may or may not require an inotify (or similar) package to be installed on your OS.
    • May not work on Windows or remote filesystems.

Installation

Install via any package managment system:

make_pact 'rktjmp/fwatch.nvim'

Usage

Watch a file and run a vim command

local fwatch = require('fwatch')
fwatch.watch("a_file", "echo 'a_file changed'")

Watch a file and run a lua function

local fwatch = require('fwatch')
fwatch.watch("a_file", {
  on_event = function()
    print('a_file changed')
  end
})

Watch a directory and run a lua function

local fwatch = require('fwatch')
local limit = 10

fwatch.watch("a_dir", {
  on_event = function(filename, events, unwatch)
    limit = limit - 1
    if limit <= 0 then
      -- *immediately* stop.
      -- without return, the callback would continue executing,
      -- but not be run on the next change.
      print("10 changes, very sleepy.")
      return unwatch()
    end

    print("a_dir/" .. filename .. " was changed")
  end,
  on_error = function(error, unwatch)
    -- disable watcher
    unwatch()
    -- note, print still occurs even though we unwatched *future* events
    print("An error occured: " .. error)
  end
})

API

fwatch.watch(path, vim_command_or_function_table)

Watch path, execute vim_command_or_function_table on change or rename. Returns a handle to the watcher.

path :: string

Path to file or directory to be watched. path may be relative to the current working directory or absolute. path is not expanded, so ~/my_file.txt is not expanded to /home/me/my_file.txt. You must use existing vim or lua functions to expand paths.

vim_command_or_function_table :: string | {on_event :: function, on_error :: function}

Either a string, which is executed as a vim ex command, or a table of callbacks.

The string is passed to vim.cmd and is automatically prefixed with :.

-- reload colorscheme whenever path changes
fwatch.watch(path, "colorscheme herbert")

The table must contain an on_event = function key-value pair, it may contain an on_error = function key-value pair.

fwatch.watch(path, {
  on_event = function()
    -- reload colorscheme whenever path changes
    vim.cmd("colorscheme herbert")
  end
})

Callbacks

on_event

Called when the watched path changes. on_event is passed:

  • filename :: string

    filename is the name of the changed file.

  • events :: {change :: boolean, rename :: boolean}

    events describes what type of change occured.

  • unwatch_cb :: function.

    unwatch_cb will detatch the watcher when called.

on_error

Called when the watcher fails for some reason. on_error is passed:

  • error :: string

    error describes the error that occured.

  • unwatch_cb :: function.

    unwatch_cb will detatch the watcher when called.

In lua, function parameters are optional, so you may ignore any you do not need. The following are all valid callbacks:

on_event = function()
  print("an event")
end
on_event = function(filename)
  print("an event for " .. filename)
end
on_event = function(filename, events, unwatch)
  if events.rename then print(filename .. " was renamed")
  if events.change then print(filename .. " was changed")
  if events.rename and events.change then
    unwatch()
  end
end

fwatch.once(path, vim_command_or_function_table)

Watch path, execute vim_command_or_function_table on change or rename only once. Returns a handle to the watcher. See fwatch.watch for more details.

fwatch.unwatch(handle)

Disables given watcher.

local handle = fwatch.watch(path, command)
fwatch.unwatch(handle)

Warnings

Watching a file you edit with vim will likely fire multiple times and detatch your watcher because vim swaps files around on save. See using inotify/wait along with vim on stackoverflow.

You could call unwatch() in every callback and re-call fwatch.watch() with the given filename to watch the new file.

Maybe

:Fwatchers - show list of watchers?

:Fwatch "path" "command" ?

Artwork

GH image is a play on the cover art for the fantastic game Firewatch by the brilliant team of Campo Santo, orignal art at least partly by the talented Olly Moss.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].