-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvimex.py
executable file
·137 lines (117 loc) · 4.28 KB
/
nvimex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python2
def showhelp():
print '''============
Installation
============
1. Make sure you have Neovim's python-client installed.
2. Place this script somewhere convenient. Optionally alias ':' to it.
For instance, add this line to ~/.bashrc:
[ -z "$NVIM_LISTEN_ADDRESS" ] || alias :='~/nvimex.py'
Example invocation from bash:
$ : e file1
$ : diff file1 file2
=====
Usage
=====
(1) nvimex.py {cmd} [{arg} [...]]
Execute {cmd} in the associated neovim instance.
Note: Current directory of terminal may be different from that of neovim
instance. So be careful when giving relative paths as arguments.
See usage (4) and notes.
(2) nvimex.py cd {path}
Change directory to {path}. Note that relative paths will also work.
(3) nvimex.py @
Change directory of neovim instance to the current directory of terminal.
Equivalent to `nvimex.py cd .`
(4) nvimex.py @{cmd} [{arg} [...]]
Equivalent to executing (3), followed by (1).
Note: The following commands can handle relative addresses w.r.t terminal.
These commands won't change neovim's current directory as a side effect.
badd, cd, diff, dr, drop, e, edit, sp, split, vs, vsp, vsplit
Commands that accept multiple file arguments:
badd, e, edit, sp, split, vs, vsp, vsplit
bad[d] : Add all arguments to the neovim's buffer list.
diff : Open a new tab and diffsplit the given 2 files.
e[dit] : Invokes the drop command.
vs[plit], : If only one argument is provided, split it with terminal window.
sp[lit] If more are provided, terminal window is replaced with the split
windows.
==============
Example usages
==============
Edit file1:
$ ~/nvimex.py e file1
Open file1 in a vertical split:
$ ~/nvimex.py vsp file1
Open two files in vertical split, replacing terminal window:
$ ~/nvimex.py vsp file1 file2
Diff file1 file2 in a new tab:
$ ~/nvimex.py diff file1 file2
Add all python files in current directory to bufferlist:
$ ~/nvimex.py badd *.py
Open manpage of bash in new tab (with 'powerman/vim-plugin-viewdoc' plugin):
$ ~/nvimex.py ViewDocMan bash
'''
def abspath_esc(s):
from os.path import abspath
return abspath(s).replace(' ', r'\ ')
def _exit(e):
if e == "1+":
exit("Expects at least one argument. See --help.")
elif e == "1":
exit("Expects exactly one argument. See --help.")
elif e == "2":
exit("Expects exactly two arguments. See --help.")
def main(nvim_listen_addr, cmd, *args):
from neovim import attach
from neovim.api.nvim import NvimError
nvim = attach('socket', path=nvim_listen_addr)
if cmd[0] == '@':
nvim.command('cd ' + abspath_esc('.'))
cmd = cmd[1:]
try:
if cmd == [ 'bad', 'badd' ]:
if len(args) == 0:
_exit("1+")
for f in args:
nvim.command('badd ' + abspath_esc(f))
elif cmd == 'cd':
if len(args) != 1:
_exit("1")
nvim.command(cmd + abspath_esc(args[0]))
elif cmd in [ 'dr', 'drop', 'e', 'edit' ]:
if len(args) == 0:
_exit("1+")
nvim.command(' '.join(['drop'] + [abspath_esc(f) for f in args]))
elif cmd in [ 'sp', 'split', 'vs', 'vsp', 'vsplit' ]:
if len(args) == 0:
_exit("1+")
if len(args) == 1:
nvim.command(cmd + abspath_esc(args[0]))
else:
nvim.command('e ' + abspath_esc(args[0]))
for f in args[1:]:
nvim.command(cmd + abspath_esc(f))
elif cmd == 'diff':
if len(args) != 2:
_exit("2")
nvim.command('tabnew ' + abspath_esc(args[0]))
nvim.command('diffsplit ' + abspath_esc(args[1]))
elif cmd != '':
nvim.command(' '.join((cmd,) + args))
except NvimError as e:
print e
if __name__ == '__main__':
from sys import argv
from os import environ
if len(argv) > 1 and argv[1] == '--help':
showhelp()
exit()
if 'NVIM_LISTEN_ADDRESS' not in environ:
print '$NVIM_LISTEN_ADDRESS not set!'
exit()
if len(argv) < 2:
print 'Usage: ' + argv[0] + ' {cmd} [{args} [...]]'
print 'See --help for detailed help.'
exit()
main(environ['NVIM_LISTEN_ADDRESS'], *argv[1:])