m (Upload files from vim (using python) moved to Upload web files from Vim using Python: Page moved by JohnBot to improve title) |
(Change to TipImported template + severe manual clean) |
||
Line 1: | Line 1: | ||
{{review}} |
{{review}} |
||
+ | {{TipImported |
||
− | {{Tip |
||
|id=910 |
|id=910 |
||
+ | |previous=909 |
||
− | |title=upload files from vim (using python) |
||
+ | |next=911 |
||
− | |created=April 7, 2005 |
+ | |created=April 7, 2005 |
|complexity=basic |
|complexity=basic |
||
|author=Matthias Ihrke |
|author=Matthias Ihrke |
||
|version=5.7 |
|version=5.7 |
||
|rating=1/7 |
|rating=1/7 |
||
⚫ | |||
− | |text= |
||
− | I often have a local working copy of a remote directory tree (e.g. a website) and sometimes I want to transfer some minor changes to a file very fast (e.g. when updating a webpage). |
+ | I often have a local working copy of a remote directory tree (e.g. a website) and sometimes I want to transfer some minor changes to a file very fast (e.g. when updating a webpage). |
− | You can do this by simply typing :Upload while editing a file using the following line in |
+ | You can do this by simply typing <tt>:Upload</tt> while editing a file using the following line in vimrc: |
+ | <pre> |
||
⚫ | |||
+ | </pre> |
||
⚫ | |||
+ | <pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | upload.py: |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
− | ----------------------- |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | Use netrw (distributed with Vim) for this type of operation. See {{help|netrw}}. |
||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | if os.getenv('UPLOAD_SERVER'): mode = os.getenv('UPLOAD_SERVER') |
||
− | |||
⚫ | |||
− | |||
− | if os.getenv('UPLOAD_REMOTE_ROOT'): mode = os.getenv('UPLOAD_REMOTE_ROOT') |
||
− | |||
− | if os.getenv('UPLOAD_LOCAL_ROOT'): mode = os.getenv('UPLOAD_LOCAL_ROOT') |
||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | ----------------------- |
||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | export UPLOAD_REMOTE_ROOT=/some/directory |
||
− | |||
− | ... |
||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | have to specify the server and the corresponding directories. |
||
− | |||
− | I would also have liked to implement this functionality in VIM's internal language, but I was just too impatient :-) |
||
⚫ | |||
− | |||
⚫ | |||
− | netrw is distributed with vim, accomplishes the same thing, and is written in vim's internal language. |
||
− | |||
− | See ':help netrw' |
||
− | |||
− | '''Anonymous''' |
||
− | , April 8, 2005 4:30 |
||
---- |
---- |
||
+ | Using netrw, Vim keeps a local copy of the codument in your temp folder. |
||
− | Didn't know that... |
||
− | But it does not do quite the same... |
||
− | First you do not keep a local copy of the document (as far as I figured out) and you always have to open it quite |
||
− | uncomfortably with a long path like |
||
− | vim scp://server.name.org//path/to/file.html |
||
− | Third I have problems with syntax highlighting but that may just be me :-) |
||
− | Anyway, I improved my script a bit, so that upload data can be as well specified somewhere in the |
||
− | edited file like this ( http://www.vug.de/t/upload.py.txt ): |
||
+ | You can use a procedure like the following to use short names to access hosts. |
||
− | <upload>user:server:remote_root:local_root:mode</upload> |
||
⚫ | |||
− | This overrides the other to possibilities, a 'default' as entry results in a fallback to the other definitions (in the python |
||
− | file or environmental variables). |
||
− | I still think it's kind of nice :-) |
||
− | But I like the netrw thing as well, thanks for the tip! |
||
+ | <pre> |
||
− | mihrke--AT--uni-goettingen.de |
||
⚫ | |||
− | , April 8, 2005 12:11 |
||
⚫ | |||
− | ---- |
||
⚫ | |||
− | I think it should be |
||
⚫ | |||
− | if os.getenv('UPLOAD_MODE'): mode = os.getenv('UPLOAD_MODE') |
||
⚫ | |||
− | if os.getenv('UPLOAD_USER'): user = os.getenv('UPLOAD_USER') |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
− | tiger--AT--rt.mipt.ru |
||
+ | <pre> |
||
− | , April 12, 2005 10:15 |
||
− | ---- |
||
− | oops, yeah sure :-) |
||
− | http://www.vug.de/t/upload.py.txt |
||
− | is now correct... thanks |
||
− | |||
− | mihrke--AT--uni-goettingen.de |
||
− | , April 12, 2005 11:44 |
||
− | ---- |
||
− | I must comment on this problem :-) Someone mentioned three problems in Vim way: |
||
− | |||
− | > First you do not keep a local copy of the document (as far as I figured out) |
||
− | In vim it is kept in your temp folder. |
||
− | |||
− | > you always have to open it quite uncomfortably with a long path like |
||
− | > vim scp://server.name.org//path/to/file.html |
||
− | |||
− | You should try this. |
||
⚫ | |||
− | ---- |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | ---- |
||
− | |||
⚫ | |||
vim scp://short/~/path/to/file |
vim scp://short/~/path/to/file |
||
+ | </pre> |
||
− | ~ is |
+ | The <tt>~</tt> is your home folder (usually you keep files here). |
− | |||
− | > Third I have problems with syntax highlighting but that may just be me :-) |
||
− | Just you :-) |
||
− | dado1945--AT--gmail.com |
||
− | , October 2, 2005 12:18 |
||
---- |
---- |
||
− | <!-- parsed by vimtips.py in 0.528744 seconds--> |
Revision as of 10:00, 9 December 2007
created April 7, 2005 · complexity basic · author Matthias Ihrke · version 5.7
I often have a local working copy of a remote directory tree (e.g. a website) and sometimes I want to transfer some minor changes to a file very fast (e.g. when updating a webpage).
You can do this by simply typing :Upload while editing a file using the following line in vimrc:
:command Upload :!upload.py %;
The file upload.py must be in your PATH:
#!/usr/bin/env python import os, sys # cmd parsing if len(sys.argv)>1 and os.path.isfile(sys.argv[1]): file=sys.argv[1] else: print "Usage: upload.py <file>"; sys.exit() # can be overridden by environment variables mode='scp' server='your_server.org' user='username_on_server remote_root='/your/root/on/server' local_root='/your/local/root' # override defaults with environment variables if os.getenv('UPLOAD_MODE'): mode = os.getenv('UPLOAD_MODE') if os.getenv('UPLOAD_SERVER'): server = os.getenv('UPLOAD_SERVER') if os.getenv('UPLOAD_USER'): user = os.getenv('UPLOAD_USER') if os.getenv('UPLOAD_REMOTE_ROOT'): remote_root = os.getenv('UPLOAD_REMOTE_ROOT') if os.getenv('UPLOAD_LOCAL_ROOT'): local_root = os.getenv('UPLOAD_LOCAL_ROOT') # add other modes here if mode=='scp': if not os.getcwd().startswith(local_root): print 'file not in %s'%local_root; sys.exit() else: ext_path=os.path.join(os.getcwd().replace(local_root, remote_root), file) cmd = 'scp %s %s@%s:%s'%(file, user, server, ext_path) failure = os.system(cmd) if failure: print "Running %s failed..."%cmd
You will have to specify your server, your directory on the server, the user on the server and the local directory root which is corresponding to the remote directory.
You can specify the location either in the script, or as environment variables as export UPLOAD_REMOTE_ROOT=/some/directory
If the file you are editing is in the specified local directory or one of its subdirectories, the script will automatically upload it to the correct location.
Of course this is only of use if you have one major directory on one major server. If you have data on multiple servers, this script won't help, since you always have to specify the server and the corresponding directories.
Comments
Use netrw (distributed with Vim) for this type of operation. See :help netrw.
Using netrw, Vim keeps a local copy of the codument in your temp folder.
You can use a procedure like the following to use short names to access hosts.
Add following lines to .ssh/config
Host short Hostname very.long.hostname User username Port 12345 # if you really need it Host another Hostname another.very.long.hostname User different_username
Now try this:
vim scp://short/~/path/to/file
The ~ is your home folder (usually you keep files here).