In Vim, how to keep characters concealed even when

2019-03-18 16:14发布

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.

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

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.)

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.

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.

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?

1条回答
欢心
2楼-- · 2019-03-18 16:41

:help conceal

When the "conceal" argument is given, the item is marked as concealable. Whether or not it is actually concealed depends on the value of the 'conceallevel' option. The 'concealcursor' option is used to decide whether concealable items in the current line are displayed unconcealed to be able to edit the line.

:help concealcursor

Sets the modes in which text in the cursor line can also be concealed. When the current mode is listed then concealing happens just like in other lines.

  • n Normal mode
  • v Visual mode
  • i Insert mode
  • c Command line editing, for 'incsearch'

'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.

Also, :help conceallevel

Determine how text with the "conceal" syntax attribute |:syn-conceal| is shown:

Value Effect ~

  • 0 Text is shown normally
  • 1 Each block of concealed text is replaced with one character. If the syntax item does not have a custom replacement character defined (see |:syn-cchar|) the character defined in 'listchars' is used (default is a space). It is highlighted with the "Conceal" highlight group.
  • 2 Concealed text is completely hidden unless it has a custom replacement character defined (see |:syn-cchar|).
  • 3 Concealed text is completely hidden.
查看更多
登录 后发表回答