Skip to content

ayush-garg341/code_sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

🔁 code_sync.nvim

A lightweight Neovim plugin to sync your local code to remote servers with minimal configuration. Runs in background, does not freeze the main window.


✨ What It Does & Why It Exists

  • I wanted a simple, minimal plugin to sync code from my local machine to a remote server while coding in Neovim—my go-to editor.
  • Existing plugins were either too bloated or difficult to configure.
  • So I built this: a single-purpose, Lua-based plugin that does one thing well—syncing code from local to remote.
  • You just need to define your sync configuration in ~/.config/.code_sync.lua, and you're ready to go!
  • If you find this plugin useful and want to contribute, check out the 🛠️ Future Todos section.

📦 Installation

Install using packer.nvim:

use({
    "ayush-garg341/code_sync",
    config = function()
        require("code_sync").setup()
    end,
})

Or refer to this config reference.


🚀 Features

  • Support the dry run so that you can see the actual changes that will be made without actually making change.
  • Hot reload the config file i.e when you make any change in sync configuration file ~/.config/.code_sync.lua the changes will reflect in the next sync command.
  • All floating windows closes by ESC. You can map this key to something like caps lock to ease.

🛠️ Sample Configuration

Save your config in: ~/.config/.code_sync.lua

🔐 Key-Based SSH (e.g. using a .pem file)

{
  protocol = "rsync",
  ["data-science"] = {
    test = {
      {
        target = "ayush@<hostname>:/home/ayush/data-science",
        exclude_file = "/Users/elliott/.rsyncignore",
        keypath = "~/Downloads/some.pem",
        method = "key_based"
      }
    }
  },
  ...
}

🔑 Keyless SSH (Public Key copied to server)

Steps:

ssh-keygen -t rsa -b 4096 -C "yourname@yourhost"
ssh-copy-id -i ~/.ssh/id_rsa.pub user@host

Update ~/.ssh/config for shorthand access:

Host myserver
  HostName 192.168.1.42
  User ubuntu
  IdentityFile ~/.ssh/id_rsa

Host workserver
  HostName 10.0.0.5
  User devops
  IdentityFile ~/.ssh/id_work

Sample Lua config:

{
  protocol = "rsync",
  ["project-name"] = {
    test = {
      {
        target = "myserver:/home/ubuntu/project-name",
        exclude_file = "~/.rsyncignore",
        method = "key_less"
      }
    }
  }
}

🔓 Password-Based SSH

Install sshpass:

# Linux
sudo apt install sshpass

# macOS
brew install hudochenkov/sshpass/sshpass

Lua Config:

{
  protocol = "rsync",
  ["project-name"] = {
    test = {
      {
        target = "user@host:/home/user/project-name",
        keypath = "yourpassword",
        method = "pwd_based"
      }
    }
  }
}

🧾 Configuration Key Reference

Let's understand the keys used in the config:

  • protocol: One of the supported sync methods: scp, rsync, ftp, sftp.
    (Currently, only rsync is supported.)
  • keypath: Path to the SSH private key (.pem) file for authentication.
  • ["data-science"], user_agent, redis_full_dv_process, ...:
    These represent the currently opened project names in Neovim which you want to sync. These act as sources.
    • test, stage, dev:
      Environment keys indicating where the code should be synced.
      • Each environment key holds an array of targets.
        (You can sync code to multiple test, stage, or dev servers.)
    • target: Destination server and directory where code should be synced.
    • exclude_file: Path to a file like .rsync_ignore that lists files/folders to be excluded from syncing (similar to .gitignore).
    • exclude: Inline list of files/folders to be excluded (array).

🔁 Using Custom Ports

  • Define ports in your ~/.ssh/config:
Host mypi
  HostName 192.168.1.100
  User pi
  Port 2222
  IdentityFile ~/.ssh/id_rsa
  • Or modify the target string to include -p <port> before the SSH address.

🧪 Usage

Three commands are currently supported:

Command Description
:CodeSync Syncs code to the defined environment (test/stage/dev).
:CodeSyncList Shows running sync jobs with their job IDs.
:CodeSyncCancel Cancels a sync job given its ID.

🔧 Example

:CodeSync test --project   " Sync entire project to test env
:CodeSync test --cwd       " Sync current directory to test env
:CodeSync dev              " Sync current opened file to dev env

" To dry run the command ( emphasize safety ), add the option --dry at the end of the command.

:CodeSync test --project --dry " Dry run entire project to test env

Note: prod sync is intentionally not supported. Production sync should go through CI/CD pipelines.


📋 Future Todos

  • 📊 Better logging for sync success/failure
  • ⏱️ Auto-sync at user-defined intervals
  • 🧠 Smarter file tracking and change detection
  • ✅ Add support for scp, sftp.

🤝 Contributing

All contributions are welcome! 🚀

📌 How to Contribute

  1. Fork the repository
  2. Create a new branch: git checkout -b my-feature
  3. Make your changes and test thoroughly
    • Soft link the project code to your packer folder where you have all other neovim packages to test the functionality you added.
ln -s ~/personal/code_sync /Users/elliott/.local/share/nvim/site/pack/packer/start/code_sync # To link the code
rm -rf /Users/elliott/.local/share/nvim/site/pack/packer/start/code_sync # To remove the old link
  1. Commit and push: git commit -m "Add new feature" then git push origin my-feature
  2. Open a Pull Request with a description of what you’ve changed

Made with ❤️ by @ayush-garg341

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages