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

2024-05-03 09:44:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下源代码:

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个字符。 我想有编辑的可能性(在#待办事项的地方),以备不时之需 我想更改刚刚显示的字符串。你知道吗

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


Tags: 字符串importifosstdoutsyslinesystem
3条回答
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
...

不完全是这样:你不能那样做。程序通常无法读取打印到屏幕上的内容。你知道吗

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

想法:

让我们选择\o\w作为我们的目标。 将光标放在“TO DO”或任何其他您喜欢的单词上,然后按\o。 然后,它会打开新选项卡。您可以在新缓冲区中写入任何内容,然后按\w。它将复制整个内容并关闭缓冲区,然后粘贴到当前缓冲区中的光标位置。你知道吗

映射

   :nmap \o cw<ESC>:tabnew<CR>
   :nmap \w ggvG"wy:tabclose<CR>"wp

相关问题 更多 >