本文介绍了在 Vim 中,如何在光标进入该行时隐藏字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能在这里遇到了特殊情况.我希望 gVim(Linux 中的 gui 版本)无论如何都能隐藏隐藏的字符,即使光标位于该行上或该字符被选中.(应该尽可能接近如果字符不存在.)目前隐藏的字符在光标进入该行时显示自己,这会导致滚动和选择文本时文本跳动.

I may have a unique situation here. I want gVim (gui version, in Linux) to keep concealed characters concealed no matter what, even when the cursor is on that line or that character gets selected. (It should be as close to if the characters never existed as possible.) Currently the concealed characters show themselves when the cursor enters that line, which causes text to jump around when scrolling and when selecting text.

我们使用 gView(只读 gVim)来查看日志,以利用其强大的语法突出显示.问题是,这些日志包含大量转义字符和 TTY 颜色代码,这使得阅读变得困难.(^[33mSomeText^[0m)

We are using gView (read-only gVim) to view logs, so as to take advantage of its robust syntax highlighting. Problem is, these logs contain lots of escape characters and TTY color codes, that make reading difficult. (^[33mSomeText^[0m)

我用这一行来隐藏它们:

I'm using this line to hide them:

syntax match Ignore /\%o33\[[0-9]\{0,5}m/ conceal

由于文件是由非 vim 专家查看的,所以当文本隐藏起来时,它看起来会出现故障和损坏.(如果存在颜色代码,也会出现故障和损坏,如果颜色代码变黑变得不可见,也会出现故障和损坏,但在选择时仍然显示并在复制/粘贴后出现.)

Since the files are viewed by non-vim-experts, it looks glitchy and broken when the text un-conceals itself. (And also looks glitchy and broken if the color codes are present, and also looks glitchy and broken if the color codes are blacked-out to become invisible, but still show when selected and appear after copy/paste.)

这应该没问题,因为这些文件在 gview 中以只读方式打开,额外的 set nomodifiable 使得保存文件更加困难.虽然可以编辑和尝试保存日志,但这样做既是无效的,也是无害的,并且需要足够的 Vim 技能,如果有人设法编辑文件,他们知道他们在做什么.能够编辑带有隐藏文本的行的问题不适用.

This should be fine because these files are opened read-only in gview, with an extra set nomodifiable making it even more difficult to save the file. While it's possible to edit and attempt to save the logs, doing so is considered both an invalid thing to do, and a harmless thing to do, and requires enough Vim skills that if someone manages to edit a file they know what they're doing. The problem with being able to edit a line with concealed text does not apply.

如果 'conceal' 无法配置为无论如何都无法隐藏隐藏文本,则可接受的替代方法是在打开文件时用空格替换 TTY 颜色代码.但是,这必须在只读模式下完成,我们不能让 gview 在关闭窗口时抛出保存对话框,因为文件已被其 .vimrc 修改.

If 'conceal' can't be configured to keep hidden text hidden no matter what, an acceptable alternative would be to replace the TTY color codes with whitespace when the file gets opened. But, this has to be done in read-only mode, and we can't have gview throwing up a save dialog on closing the window because the file has been modified by its .vimrc.

注意:我可以完全控制读取这些文件时的 .vim 脚本文件,但无法控制 TTY 颜色代码的存在或在 gview 中打开日志文件的代码.(即我不能通过 sed 或类似的东西传递它.)理想的解决方案是任何可以从 .vimrc 中透明地核对颜色代码的东西,但我会听到任何建议.'隐藏' 功能只是我最有希望的领先优势.

Note: I am in full control of the .vim script file sourced when these are read, but cannot control the existence of the TTY color codes or the code that opens the log files in gview. (i.e. I can't pass it through sed or anything like that.) The ideal solution is anything that can transparently nuke the color codes from within a .vimrc, but I'll hear any suggestions. The 'conceal' feature is just my most promising lead.

那么,有没有办法在文件视图中永久删除这些内容,而不会在关闭时弹出对话框?

So, any ideas how to permanently get rid of these on file view without dialogs popping up on close?

推荐答案

:help hidden

当给出隐藏"参数时,该项目被标记为可隐藏.是否真正隐藏取决于它的价值'隐藏级别' 选项.'concealcursor' 选项用于决定是否当前行中可隐藏的项目不隐藏显示,以便能够编辑该行.

:help隐藏光标

设置光标行中的文本也可以隐藏的模式.当列出当前模式时,隐藏就像在其他线路.

  • n 普通模式
  • v 视觉模式
  • i 插入模式
  • c 命令行编辑,用于 'incsearch'

'v' 应用于可视区域中的所有行,而不仅仅是光标.一个有用的值是nc".这用于帮助文件中.只要你正在移动文本被隐藏,但是当开始插入文本时或选择显示隐藏文本的可视区域,以便你可以看到你在做什么.请记住,光标位置并不总是在它所在的位置显示.例如,当垂直移动时,它可能会改变列.

'v' applies to all lines in the Visual area, not only the cursor. A useful value is "nc". This is used in help files. So long as you are moving around text is concealed, but when starting to insert text or selecting a Visual area the concealed text is displayed, so that you can see what you are doing. Keep in mind that the cursor position is not always where it's displayed. E.g., when moving vertically it may change column.

另外,:help hiddenlevel

使用隐藏"语法属性确定文本如何|:syn-conceal|显示:

价值效应~

  • 0 文本正常显示
  • 1 每块隐藏的文本被替换为一个特点.如果语法项没有自定义定义的替换字符(见 |:syn-cchar|)使用了在 'listchars' 中定义的字符(默认是一个空间).它用隐藏"突出显示组突出显示.
  • 2 隐藏的文本是完全隐藏的,除非它有自定义替换字符定义(见|:syn-cchar|).
  • 3 隐藏文本完全隐藏.

这篇关于在 Vim 中,如何在光标进入该行时隐藏字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:06