如何在调用自定义VIM(Python)函数后将状态发送到VIM状态行

2024-09-24 02:14:59 发布

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

我刚刚创建了我的第一个VIM脚本,是用Python编写的。这是一个从目录(/vim/etc/colors)切换颜色方案的简单脚本。我想知道如何在配色方案更改后用所选配色方案的名称将通知发送到vim的“statusline”。在

rson回答了我的问题,这里有一个更新(并调试过)的脚本版本,供感兴趣的人使用(就我所能测试的而言,效果很好)

执行了AI和Caleb的建议,谢谢!以下内容:

" toggleColorScheme 0.9 (l) 2009 by Jasper Poppe <jpoppe@ebay.com>

" cycle through colorschemes with F8 and Shift+F8
nnoremap <silent><F8> :call ToggleColorScheme("1")<CR>
nnoremap <silent><s-F8> :call ToggleColorScheme("-1")<CR>

" set directory with color schemes to cycle through
let g:Toggle_Color_Scheme_Path = "/etc/vim/colors"


function! ToggleColorScheme(paramater)
python << endpython
import vim
import os

paramater = (vim.eval('a:paramater'))
scheme_path = vim.eval('g:Toggle_Color_Scheme_Path')

colorschemes = [color.split('.')[0] for color in os.listdir(scheme_path) if color.endswith('.vim')]
colorschemes.sort()

if not vars().has_key('position'):
    start_scheme = vim.eval('g:colors_name') + '.vim'
    if start_scheme in colorschemes:
        position = colorschemes.index(start_scheme)
    else:
        position = 0

position += int(paramater)
position %= len(colorschemes)

vim.command('colorscheme %s' % colorschemes[position])
vim.command('redraw | echo "%s"' % colorschemes[position])
vim.command('return 1')
endpython
endfunction

Tags: 脚本ifeval方案positionvimstartcommand
2条回答

既然你在这里更新脚本

而不是

if argument == 'next':
    position += 1
    if position == len(colorschemes) - 1:
        position = 0
elif argument == 'prev':
    position -= 1
    if position == -1:
        position = len(colorschemes) - 1

也许吧

^{pr2}$

vim.command('redraw | echo "%s"' % colorschemes[position])

来自:help echo

A later redraw may make the message disappear again. And since Vim mostly postpones redrawing until it's finished with a sequence of commands this happens quite often. To avoid that a command from before the ":echo" causes a redraw afterwards (redraws are often postponed until you type something), force a redraw with the |:redraw| command. Example:

:new | redraw | echo "there is a new window"

相关问题 更多 >