以交互方式切换到vim之类的编辑器来更改Python字符串

时间:2016-07-25 10:50:56

标签: python vim curses

我有以下源代码:

import sys, os
import curses
import textwrap

if __name__ == "__main__":
  curses.setupterm()
  sys.stdout.write(curses.tigetstr('civis'))
  os.system("clear")
  str = "abcdefghijklmnopqrstuvwxyz" * 10 # only example
  for line in textwrap.wrap(str, 60):
    os.system("clear")
    print "\n" * 10
    print line.center(150)
    sys.stdin.read(1) # read one character or #TODO
    #TODO 
    # x = getc() # getc() gets one character from keyboard (already done)
    # if x == "e": # edit
    #    updatedString = runVim(line)
    #    str.replace(line, updatedString)

  sys.stdout.write(curses.tigetstr('cnorm'))

程序在字符串中移动60个字符。 我希望有编辑可能性(在#TODO的地方)以防万一 我想更改刚刚显示的字符串。

按键时是否可以打开一个小的vim缓冲区?我会进行编辑,当我按:w时,它会更新字符串。我希望vim编辑器不要改变终端中字符串的位置(我喜欢它居中)。

3 个答案:

答案 0 :(得分:1)

def runVim(ln):
  with tempfile.NamedTemporaryFile(suffix=".txt") as tmp:
    tmp.write(ln)
    tmp.flush()
    call(['vim', '+1', '-c set filetype=txt', tmp.name]) # for centering +1 can be changed
    with open(tmp.name, 'r') as f:
      lines = f.read()
  return lines


...
x = getch()
  if x == "e":
    updatedString = runVim(line)
    str = str.replace(line, updatedString)
print str
...

答案 1 :(得分:0)

<强>观

我们选择\w\o作为我们的目的。 将光标置于“待办事项”或您喜欢的任何其他字词上,然后按\w。 然后,它会打开新选项卡。您可以在新缓冲区中写入任何内容,然后按 :nmap \o cw<ESC>:tabnew<CR> :nmap \w ggvG"wy:tabclose<CR>"wp 。它将复制整个事物并关闭缓冲区,然后粘贴到当前缓冲区中的光标位置。

<强>映射

imageHeight - True height of the image (if scaled with the CSS width and height properties) 
imageWidth - True width of the image (if scaled with the CSS width and height properties) 

答案 2 :(得分:0)

不完全是:你不能这样做。程序通常无法读取您在屏幕上打印的内容。

可以创建一个在屏幕上显示文本的程序(知道它写的是什么)将该信息传递给编辑器。例如,lynx(使用curses的应用程序)在屏幕上显示格式化的HTML页面,并提供将表单文本字段的内容传递给编辑器的功能,从编辑器中读取更新的文件并重新显示表格中的信息。