简体   繁体   English

使Vim映射安静

[英]Making Vim mappings quiet

I have a mapping that I use to print the highlighting on a line. 我有一个映射,用于在一行上打印突出显示。 I got the idea from other posters here so thanks for that. 我从其他海报中得到了这个想法,谢谢你。 Here's what I do: 这是我做的:

function! PrintSyntaxItem()
    let l:colorsyntax = synIDattr(synID(line("."), col("."), 0), "name")
    execute "highlight" l:colorsyntax
endfunction

and I map it like this: 我像这样映射:

nnoremap <A-s> :call PrintSyntaxItem()<CR>

However when I execute it, I get the command line echoed as well as the output that I want which leads to getting a "Press ENTER" prompt. 然而,当我执行它时,我得到命令行回显以及我想要的输出,这导致获得“按ENTER”提示。 Ie in the output I see: 即在输出中我看到:

:execute "highlight" synIDattr(synID(line("."), col("."), 0), "name")
vimBracket     xxx links to Delimiter
Press ENTER or type command to continue

I'd like to lose the :execute line and then the Press ENTER line would go away as well. 我想丢失:execute行,然后按ENTER行也会消失。 Is there any way to do this? 有没有办法做到这一点? If I put silent in front of the execute I still get that line printed out but lose the highlight output (as well as the Press ENTER prompt), but then to get back my desired output I just prefix it with unsilent and I get it but... 如果我在execute前放置silent ,我仍然会打印出那条线,但是输掉了highlight输出(以及按ENTER键提示),但是为了得到我想要的输出,我只是在unsilent加上unsilent ,我得到了但是...

Basically I want to either suppress the echo of the :execute line or clear it after the fact but I'm not sure how to do either and trawling the docs for info on manipulating the messages hasn't borne any fruit. 基本上我想要抑制:execute行的回声或者在事实之后清除它但是我不知道如何做并且拖网文件以获取关于操纵消息的信息并没有任何结果。

Thanks. 谢谢。

It's not the command that you should silence but, as you wrote in the title of your question, the mapping itself: 这不是你应该沉默的命令,但正如你在问题的标题中写的那样,映射本身:

nnoremap <silent> <A-s> :call PrintSyntaxItem()<CR>

That said, the execute command looks like it can't be silenced easily. 也就是说,执行命令看起来不容易被静音。 I'll look into that. 我会调查一下。

edit 编辑

I've tried all the tricks I could find, including :redir => and a dozen of combinations of [<]silent[>] but I couldn't obtain the desired result. 我已经尝试了所有可以找到的技巧,包括:redir =>[<]silent[>]的十几种组合,但我无法获得所需的结果。 Either I get the prompt or I don't get anything. 要么得到提示,要么我什么也得不到。 I'm terribly sorry! 我非常抱歉!

The press enter prompt comes up because the output of highlight takes up multiple lines. 出现按下输入提示,因为highlight的输出占用多行。 You could get rid of the extra line by redirecting the output, removing \\n , and then echoing it: 您可以通过重定向输出,删除\\n ,然后回显它来摆脱额外的行:

function! PrintSyntaxItem()
    let l:output = ''
    redir => l:output
    silent exec "hi" synIDattr(synID(line("."), col("."), 0), "name")
    redir END
    echo substitute(l:output, '\n', '', '')
endfunction

However, the xxx sample is no longer properly highlighted. 但是, xxx样本不再正确突出显示。 Instead, you can hack the press enter prompt away by temporarily changing cmdheight : 相反,您可以通过暂时更改cmdheight来破解按下输入提示:

nnoremap <silent> <A-s> :set ch=2 \| exec "hi"
    \ synIDattr(synID(line("."), col("."), 0), "name") \| set ch=1<CR>

This prevents the prompt from being printed in the first place by initially changing the command line height to 2, and then reverting it to 1 afterward in order to cut off the empty line. 通过最初将命令行高度更改为2,然后将其恢复为1以便切断空行,这可以防止首先打印提示。 I did away with the function altogether, but you can of course call it between the set ch if you prefer. 我完全取消了这个功能,但如果你愿意,你当然可以在set ch之间调用它。

Rather than fudging with the command-line height, how about highlighting the entire line instead of just the xxx part? 而不是用命令行高度捏造,如何突出显示整行而不仅仅是xxx部分?

function! ShowSyntaxItem()
    redir => l:output
    silent exec "hi" synIDattr(synID(line("."), col("."), 0), "name")
    redir END
    let l:parts = matchlist(output, '\v\n@<=(\S+)(.*$)')
    if (len(l:parts) >= 3)
      redraw | exec "echohl ".l:parts[1] | exec "echo '".l:parts[0]."'" | echohl None
    endif
endfunction

nnoremap <silent> <Leader>as :call PrintSyntaxItem()<CR>

Note, there's no need to declare l:output or re-init it if it exists before the redir as stated in :he redir . 注意,没有必要声明l:output或重新初始化它,如果它存在于redir之前,如下所述:he redir This is pretty handy, and shall be going into ye olde vimrc. 这非常方便,并且应该进入你的vimrc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM