vim:在功能中执行击键而不触发功能

vim:在功能中执行击键而不触发功能,vim,Vim,我试图编写一个vim函数,有条件地更改enter键的行为。我希望enter键有时缩进,有时表现“正常”。通常我的意思是,如果一系列的情况不适用,就好像函数/映射不存在一样。我遇到的问题是,我使用作为触发器来调用函数,因此我不知道如何说“哦,这些情况都不适用,执行,就好像从未定义过此映射一样。” 作为一个例子,在我的 VIMRC < /代码>中考虑这一点,如果它以 a 开始,它会缩进该行,否则会触发回车。(我的vimscript非常新手,因此此函数可能不正确,但我认为想法仍然存在…) 函数!特别报

我试图编写一个vim函数,有条件地更改enter键的行为。我希望enter键有时缩进,有时表现“正常”。通常我的意思是,如果一系列的情况不适用,就好像函数/映射不存在一样。我遇到的问题是,我使用
作为触发器来调用函数,因此我不知道如何说“哦,这些情况都不适用,执行
,就好像从未定义过此映射一样。”

作为一个例子,在我的<代码> VIMRC < /代码>中考虑这一点,如果它以<代码> a <代码>开始,它会缩进该行,否则会触发回车。(我的vimscript非常新手,因此此函数可能不正确,但我认为想法仍然存在…)

函数!特别报告员()
let line=getline(“.”)
如果行=~'\va*'
“至少有一个空格,然后是有效的缩进
正常!>>
其他的
“只做常规的事情
“回声”在“其他”
调用feedkeys(“\”)
恩迪夫
端功能
inoremap:callspecialneter()

这在某种程度上与我实际尝试做的有所简化,但概念是相同的。我正在寻找一种方式来表示“我的if语句都不适用,就像这个映射不存在一样。”“。有什么方法可以做到这一点吗?

您需要为映射设置
标志。这样,映射的右侧将作为表达式进行计算

下面是我的配置中的一个示例,其中我为不同的命令返回不同的提示:

cnoremap <expr> <CR> CCR()

" make list-like commands more intuitive
function! CCR()
    let cmdline = getcmdline()
    command! -bar Z silent set more|delcommand Z
    if cmdline =~ '\v\C^(ls|files|buffers)'
        " like :ls but prompts for a buffer command
        return "\<CR>:b"
    elseif cmdline =~ '\v\C/(#|nu|num|numb|numbe|number)$'
        " like :g//# but prompts for a command
        return "\<CR>:"
    elseif cmdline =~ '\v\C^(dli|il)'
        " like :dlist or :ilist but prompts for a count for :djump or :ijump
        return "\<CR>:" . cmdline[0] . "j  " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"
    elseif cmdline =~ '\v\C^(cli|lli)'
        " like :clist or :llist but prompts for an error/location number
        return "\<CR>:sil " . repeat(cmdline[0], 2) . "\<Space>"
    elseif cmdline =~ '\C^old'
        " like :oldfiles but prompts for an old file to edit
        set nomore
        return "\<CR>:Z|e #<"
    elseif cmdline =~ '\C^changes'
        " like :changes but prompts for a change to jump to
        set nomore
        return "\<CR>:Z|norm! g;\<S-Left>"
    elseif cmdline =~ '\C^ju'
        " like :jumps but prompts for a position to jump to
        set nomore
        return "\<CR>:Z|norm! \<C-o>\<S-Left>"
    elseif cmdline =~ '\C^marks'
        " like :marks but prompts for a mark to jump to
        return "\<CR>:norm! `"
    elseif cmdline =~ '\C^undol'
        " like :undolist but prompts for a change to undo
        return "\<CR>:u "
    else
        return "\<CR>"
    endif
endfunction
cnoremap CCR()
“使类似列表的命令更直观
函数!CCR()
让cmdline=getcmdline()
命令!-bar Z静音设置更多| DELZ命令
如果cmdline=~'\v\C^(ls | files | buffers)'
类似:ls,但提示输入缓冲区命令
返回“\:b”
elseif cmdline=~'\v\C/(#nu | num | numb | numb | numb | number)$”
“类似于:g/#,但提示输入命令
返回“\:”
elseif cmdline=~'\v\C^(dli|il)'
类似于:dlist或:ilist,但提示输入:djump或:ijump的计数
返回“\:”。cmdline[0]。“j”。拆分(cmdline,“”[1]。"\\"
elseif cmdline=~'\v\C^(cli|lli)'
“类似于:clist或:llist,但提示输入错误/位置号
返回“\:sil”。重复(cmdline[0],2)。“\”
elseif cmdline=~'\C^old'
“类似:旧文件,但提示编辑旧文件”
停止
返回“\:Z|e#
cnoremap <expr> <CR> CCR()

" make list-like commands more intuitive
function! CCR()
    let cmdline = getcmdline()
    command! -bar Z silent set more|delcommand Z
    if cmdline =~ '\v\C^(ls|files|buffers)'
        " like :ls but prompts for a buffer command
        return "\<CR>:b"
    elseif cmdline =~ '\v\C/(#|nu|num|numb|numbe|number)$'
        " like :g//# but prompts for a command
        return "\<CR>:"
    elseif cmdline =~ '\v\C^(dli|il)'
        " like :dlist or :ilist but prompts for a count for :djump or :ijump
        return "\<CR>:" . cmdline[0] . "j  " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"
    elseif cmdline =~ '\v\C^(cli|lli)'
        " like :clist or :llist but prompts for an error/location number
        return "\<CR>:sil " . repeat(cmdline[0], 2) . "\<Space>"
    elseif cmdline =~ '\C^old'
        " like :oldfiles but prompts for an old file to edit
        set nomore
        return "\<CR>:Z|e #<"
    elseif cmdline =~ '\C^changes'
        " like :changes but prompts for a change to jump to
        set nomore
        return "\<CR>:Z|norm! g;\<S-Left>"
    elseif cmdline =~ '\C^ju'
        " like :jumps but prompts for a position to jump to
        set nomore
        return "\<CR>:Z|norm! \<C-o>\<S-Left>"
    elseif cmdline =~ '\C^marks'
        " like :marks but prompts for a mark to jump to
        return "\<CR>:norm! `"
    elseif cmdline =~ '\C^undol'
        " like :undolist but prompts for a change to undo
        return "\<CR>:u "
    else
        return "\<CR>"
    endif
endfunction