Git Product home page Git Product logo

auto-touch-on-save.vim's Introduction

auto-touch-on-save.vim

A flexible Vim plugin that allows you to automatically execute a custom command (e.g., touch) on file save, with customizable conditions and path conversion.

Motivation

This plugin was created to address a specific issue that arises when working with shared mounted files between a host system and virtual environments or containers. In such setups, system constraints may prevent inotify from functioning properly, which can lead to file change detection problems.

The primary goal of auto-touch-on-save.vim is to provide a workaround for this issue by executing a touch command on the guest system whenever a file is saved on the host. This ensures that file changes are properly detected and propagated, even when the standard file system notifications are not working as expected.

By offering customizable conditions and path conversion, this plugin allows users to tailor its behavior to their specific development environments and workflows, making it a versatile tool for developers working with various virtualized or containerized setups.

Features

  • Execute custom commands on file save
  • Customizable conditions for command execution
  • Flexible path conversion to support various workflows (e.g., local to remote path conversion)
  • Support for both Vim functions and external commands for condition checking and path conversion
  • Automatic file reload after touch to prevent unnecessary "file changed" warnings

Installation

Using vim-plug:

Plug 'na0x2c6/auto-touch-on-save.vim'

Or manually:

  1. Create the directory ~/.vim/pack/plugins/start/auto-touch-on-save/
  2. Copy the contents of this repository into that directory

Configuration

Add the following to your .vimrc file:

" Set the condition for executing the command (optional)
let g:auto_touch_on_save_condition = 'custom_condition_script.sh'

" Set the command to be executed (optional)
let g:auto_touch_on_save_command = 'custom_touch_command.sh'

" Set the path converter (optional)
let g:auto_touch_on_save_path_converter = 'sed "s|^/home/user|/remote/path|"'

" Or set a Vim function for path conversion (optional)
function! CustomPathConverter(path)
  " Your custom path conversion logic here
  return converted_path
endfunction
let g:AutoTouchOnSavePathConverter = function('CustomPathConverter')

Default Values

  • g:auto_touch_on_save_condition: 'true' (always execute)
  • g:auto_touch_on_save_command: 'touch'
  • g:auto_touch_on_save_path_converter: '' (no conversion)
  • g:AutoTouchOnSavePathConverter: Not set by default

Path Conversion Priority

  1. If g:AutoTouchOnSavePathConverter is defined as a Vim function, it will be used for path conversion.
  2. If g:AutoTouchOnSavePathConverter is not defined, but g:auto_touch_on_save_path_converter is set to a non-empty string, it will be used as an external command for path conversion.
  3. If neither is set, no path conversion will be performed.

Usage Examples

Basic Usage

" Touch the file on every save
let g:auto_touch_on_save_command = 'touch'

Custom Condition

" Only touch the file if a certain condition is met
let g:auto_touch_on_save_condition = 'test -f /path/to/flag/file'

Path Conversion using Vim Function

function! ConvertPath(path)
  return substitute(a:path, '^/home/user', '/remote/path', '')
endfunction

let g:AutoTouchOnSavePathConverter = function('ConvertPath')

Path Conversion using sed Command

" The command will receive the path via stdin and should output the converted path
let g:auto_touch_on_save_path_converter = 'sed "s|^/home/user|/remote/path|"'

This example uses sed to replace /home/user at the beginning of the path with /remote/path.

" The condition checks if a Podman machine is running.
let g:auto_touch_on_save_condition = 'podman machine list --format "{{ .Running }}" | grep -q true'
" The command uses `podman machine ssh` to execute the `touch` command inside the Podman machine.
let g:auto_touch_on_save_command = 'podman machine ssh -- touch'

auto-touch-on-save.vim's People

Contributors

na0x2c6 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.