Home

Awesome

mirror.vim GitHub release GitHub issues

Live demo

asciicast

Introduction

If some of your projects have multiple environments (e.g. development, staging, production - with nearly the same directory and files structure), then there is a situations when you need to connect to one of this environments via ssh and remotely edit project-related files. Usually you will do something like this:

ssh user@host
cd path/to/you/project
vim /some/file
and so on...

This plugin was created to simplify this process by maintaining special configuration file and adding different commands for quickly doing remote actions for each environment of project you working with. This remote actions use netrw under the hood. You don't need to install netrw - it's part of vim distribution and it used as default file explorer (e.g. :edit .). To get more information about editing remote files with netrw - refer to :h netrw-start.

Requirements

Installation

Use your favourite plugin manager: Pathogen, Vundle, NeoBundle or VimPlug. Add zenbro/mirror.vim to the list of plugins, source that and issue your manager's install command.

Add this lines to .vimrc (probably they already there):

set nocompatible    " disable backward compatibility with Vi
filetype plugin on  " plugins are enabled

Usage

Let's assume that you have a project /home/user/work/my_project. This project have multiple environments - development, staging and production. Development - is your current local environment. Staging and production - remote environments, each placed on their own remote server. Project structure on each environments is nearly similar (from here comes the name of this plugin). If you want to get access to multiple environment-related remote actions, you need to add information about this project to configuration file. Run this command :MirrorConfig and edit configuration file.

For our example it should look like this:

/home/user/work/my_project:
  staging: my_project@staging_host/current
  production: my_project@production_host/current

See Configuration for more details about format and structure of this file.

From now, if you open any file inside /home/user/work/my_project then multiple remote commands should be available. For example, if you want to edit some file on remote server in staging environment (my_project@staging_host), then open this file locally and run :MirrorEdit staging. You should be able to edit this remote file here, locally, with your own vim settings. If you want to see difference between file, you currently edit and version of this file on production server - use this command: :MirrorDiff production.

There are many other remote actions available.

Configuration

Default path of configuration file is ~/.mirrors. Use g:mirror#config_path if you want to change location of configuration file. To open configuration file use :MirrorConfig command. Use q to close it.

Configuration file use simplified YAML format and doesn't support things like &links, arrays, inline objects.

Example of mirrors config:

/home/user/work/project1:
  staging: project1@staging_host/current
  production: project1@production_host/current
/home/user/work/project2:
  staging: project2@another_host:23//opt/project2
/home/user/work/projects/*:
  devel: project2@some_host//some/workplace/*/src/*
ssh project1@staging_host
cd current
ssh -p 23 project2@another_host
cd /opt/project2

Third example a little bit complex. Lets assume that the "projects" directory contains the following directories: "ProjectA", "ProjectB", "ProjectC". Then the following project path and remote path

  /home/user/work/projects/*:
    devel: project2@some_host//some/workplace/*/src/*

would be expanded like this

  /home/user/work/projects/ProjectA:
    devel: project2@some_host//some/workplace/ProjectA/src/ProjectA
  /home/user/work/projects/ProjectB:
    devel: project2@some_host//some/workplace/ProjectB/src/ProjectB
  /home/user/work/projects/ProjectC:
    devel: project2@some_host//some/workplace/ProjectC/src/ProjectC

If you open any file inside your projects directories, then you should be able to do environment-specific remote actions.

Commands

Global

This command is available everywhere.

Local

Local commands are only available when you open a file inside one of the projects from configuration.

Project discovery

When you open a file and absolute path of this file containing one of the path from configuration then project discovery succeeded and local commands will be available for current buffer.

In summary, project discovery will be done after following actions:

Project discovery can be manually triggered via :MirrorConfigReload.

Default environment

When your project have only one environment, then it will be used automatically for all local commands as default - you don't need to pass it as argument. When your project have multiply environments - you need to pass it explicitly.

To change default environment for current project use one of the following commands.

Remote actions

Local file - file you are currently editing.
Remote file - version of local file on remote server.

Variables

This is all available options with their defaults:

let g:mirror#config_path = expand('~/.mirrors')
let g:mirror#open_with = 'Explore'
let g:mirror#diff_layout = 'vsplit'
let g:mirror#ssh_auto_cd = 1
let g:mirror#ssh_shell = '$SHELL --login'
let g:mirror#cache_dir = expand('~/.cache/mirror.vim')
let g:netrw_silent = 1
let g:mirror#spawn_command = '! '

FAQ

Q. Why should I always enter password when executing one of the remote actions?

A. Use SSH config or passwordless authentication with SSH-keys.

Q. What if my development environment is also on a remote server?

A. You can mount any directory from remote server via sshfs.

License

mirror.vim is released under the MIT License.