-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisel
executable file
·60 lines (53 loc) · 2.1 KB
/
visel
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
#!/usr/bin/env python
# Helps you view/edit the current selection in vi/vim/gvim/vimx/...
# I bind this to key combos like Meta+v and Meta+c.
# Creates a temporary file, pastes the selection/clipboard in that file
# and fires up vi. Note that the temporary file is deleted after vi is
# closed and that the modified selection/clipboard is not (currently)
# pushed back to the selection/clipboard.
# Author: David McClosky
# Homepage: http://zorglish.org
# Code: http://github.com/dmcc
import subprocess
import tempfile
import time
def edit_selection(mode, registers, vi_command, verbose=False):
time_desc = time.strftime('%H.%M')
prefix = f'{mode}-{time_desc}-'
with tempfile.NamedTemporaryFile(prefix=prefix) as f:
if verbose:
print('tempfile:', f.name)
# start vim, ask it paste text from the appropriate register(s), and
# save it to the temporary file. since it's a NamedTemporaryFile,
# it will be erased afterwards.
puts = []
for register in registers:
puts.extend(['-c', ":put %s" % register])
full_command = [vi_command, '--nofork', '--servername', mode] + \
puts + ['-c', ":write! %s" % f.name]
subprocess.call(full_command)
if __name__ == "__main__":
import optparse
parser = optparse.OptionParser(usage="%prog [selection|clipboard|both]")
parser.add_option('-v', '--verbose', action='store_true',
help='Print more debugging information.')
parser.add_option('-c', '--command', default='gvim',
help='Command to run for vi (default: gvim)')
opts, args = parser.parse_args()
if not len(args) == 1:
parser.print_help()
raise SystemExit
mode = args[0].lower()
if mode[0] == 's':
mode = 'selection'
registers = '*'
elif mode[0] == 'c':
mode = 'clipboard'
registers = '+'
elif mode[0] == 'b':
mode = 'bothclipboards'
registers = ['+', '*']
else:
parser.print_help()
raise SystemExit
edit_selection(mode, registers, opts.command, opts.verbose)