Editing remote files securely with vim
Monday, February 13, 2006 10:44 AM
Often, the life of a sysadmin consists of editing little configuration files here and there, and often on a remote system.
Typically, a
sysadmin will remotely log into the system, navigate to the file, edit and save
it, then log out. A much simpler and quicker method of accomplishing the same
task can be done with vim, without
logging into and out of the remote system, provided you have SSH access.
For instance, to edit the file /home/joe/somesite.com/html/index.php, you could use:
<code>
$ vim scp://joe@host//home/joe/somesite.com/html/index.php
</code>
This will use scp to
download the file, fire up vim locally to edit it, and upon saving the file, scp it back to the remote host. The
basic syntax of the command is:
<code>$ vim scp://user@host/[path_to_file]</code>
Note the forward slash (/) delimiter between the hostname and
the file name. Once you fill in the path, it will be preceded by a double forward-slash.
If the file is in your home directory, this can be shortened to:
<code>$ vim scp://joe@host/~/somesite.com/html/index.php</code>
If you use this often, a simple wrapper shell script will ease
the typing:
<code>#!/bin/shvim scp://joe@host/${1}</code>
If you use this script, be sure to enclose the filename in
quotes if you use the tilde (~) character to represent home directory;
otherwise, your shell will expand it and pass it on to the script already
expanded, which could cause havoc if your home directory on the local machine
is /Users/joe but is /home/joe on the remote system. Place
the shell script somewhere in your path, perhaps as ~/bin/vimhost and then execute it as:
<code>$ vimhost "~/somesite.com/html/index.php"</code>




There are currently no comments for this post.