让 Vim 将所有空格显示为一个字符
我找不到一种方法让 Vim 将所有空格显示为一个字符。 我发现的只是制表符、尾随空格等。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我找不到一种方法让 Vim 将所有空格显示为一个字符。 我发现的只是制表符、尾随空格等。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(23)
正如其他人所说,您可以使用
which will 结合
显示不可见字符。
现在,没有一个显式选项可用于显示空格,但在列表字符中,您可以设置一个字符来显示除空格之外的所有内容。例如,我的看起来像这样
,现在,在您使用
未明确显示为其他内容的所有内容之后,实际上是一个普通的旧空白。
像往常一样,要了解
listchars
的工作原理,请使用帮助。它提供了有关可以显示哪些字符(例如尾随空格)以及如何显示的重要信息:向其添加切换可能会有所帮助,以便您可以轻松地看到编辑过程中的更改(来源:VIM:将列表设置为 .vimrc 中的切换):
As others have said, you could use
which will, in combination with
display invisible characters.
Now, there isn't an explicit option which you can use to show whitespace, but in listchars, you could set a character to show for everything BUT whitespace. For example, mine looks like this
so, now, after you use
everything that isn't explicitly shown as something else, is then, really, a plain old whitespace.
As usual, to understand how
listchars
works, use the help. It provides great information about what chars can be displayed (like trailing space, for instance) and how to do it:It might be helpful to add a toggle to it so you can see the changes mid editing easily (source: VIM :set list! as a toggle in .vimrc):
从补丁 7.4.710 开始,您现在可以使用以下命令设置要显示的字符来代替空格列表字符!
因此,要将所有空白字符显示为字符,您可以执行以下操作:
完成后,要隐藏不可见的字符,您可以:
讨论邮件列表:https://groups.google.com/forum/?fromgroups#!topic/vim_dev/pjmW6wOZW_Q
As of patch 7.4.710 you can now set a character to show in place of space using listchars!
So, to show ALL white space characters as a character you can do the following:
When you are finished, to hide the non-visible chars you would:
Discussion on mailing list: https://groups.google.com/forum/?fromgroups#!topic/vim_dev/pjmW6wOZW_Q
:设置列表
以启用。:set nolist
禁用。:set list
to enable.:set nolist
to disable.我认为这里的其他答案更全面,但我想我会分享一个我通常用来在视觉上区分制表符和空格的技巧:
这些是 空白编程语言 - 选项卡显示为绿色,空格显示为红色。 :)
可以与
:set list
结合使用,正如许多其他答案所提到的,虽然选项卡将显示为 ^I 而没有绿色高亮,但空格将显示为红色。I think other answers here are more comprehensive, but I thought I'd share a trick I usually use to differentiate tabs and spaces visually:
These are syntax highlighting rules for the Whitespace programming language - tabs show in green and spaces in red. :)
Can be combined with
:set list
as mentioned by many other answers, although the tabs will then show as ^I without a green higlight, but the spaces will show in red.:set list
会将所有空格显示为一个字符。除了空间之外的所有东西看起来都与其正常状态不同,这意味着如果您仍然看到一个普通的旧空间,那么它确实是一个普通的旧空间。 :):set list
will show all whitespaces as a character. Everything but a space will look different than its normal state, which means that if you still see a plain old space, it's really a plain old space. :)如果设置:
然后搜索空格,则每个空格字符将显示为下划线字符。
您可以在一个方便的函数中使用此命令来切换空格的“下划线”。
将函数映射到快捷键:
注意: 在设置颜色方案后在 vimrc 中定义函数。
If you set:
and then perform a search for a space, every space character will be shown as an underline character.
You can use this command in a handy function that toggles "underscoring" of spaces.
Map the function to a shortcut key with:
NB: Define the function in vimrc after the colorscheme has been set.
根据当前缓冲区的语法规则,类似这样的内容可能会起作用:
这需要具有 +conceal 功能的 vim 7.3
更新 10/24/2014
对此进行一点扩展。当然可以为锥形字符定义一些突出显示。
您可以配置隐藏字符的外观。对于突出显示,您必须至少配置一次“隐藏”突出显示组(请参阅
:h hl-Conceal
中的帮助。这可以在您的颜色方案中完成,然后您不需要重新配置它但这会影响所有隐藏的字符(例如,如果您的语法脚本隐藏了更多项目,它们将显示为空白字符)。:hi Conceal ctermfg=7 ctermbg=NONE guifg=LightGrey guibg=NONE
似乎有一个特殊性,如果语法脚本使用
skipwhite
关键字,Vim 不会突出显示空格(也许这会)。 ,我发布了补丁)列表
模式。在撰写本文时,最新的似乎是这个。 (这意味着,您需要构建自己的 Vim 才能使用它)。conceallevel
和concealcursor
是窗口本地选项。这意味着它们在不同的窗口中可以不同(并且也可能由文件类型插件或其他插件脚本设置)。BufWinEnter
或Syntax
甚至FileType
自动命令来完成。 (我还没有测试哪一个真正有效)。最后两项意味着,您必须设置一些自动命令来重置语法规则和相应的选项。对于第一个,人们可能希望使用 ColorScheme 自动命令设置突出显示(以便隐藏的字符始终看起来相同,与颜色方案实际设置的内容无关)。对于完整的解决方案,请查看 romainl 答案,这应该会给您一个开始。如果您设置了一个功能,您可以轻松设置一个切换命令来打开或关闭显示特殊突出显示。
更新 10/26/2014 我制作了一个插件这个问题。
更新 04/22/2015 Vim 中包含了一个补丁,可以使用
list
选项实现这一点。只需设置set list listchars+=space:␣
此功能自 Vim 7.4.711 起生效
Depending on your syntax rules for the current buffer, something like this could work:
This needs a vim 7.3 with +conceal feature
Update 10/24/2014
To expand a little bit on that. It is of course possible to define some highlighting for the conealed characters.
You can configure, how the concealed chars look. For highlighting, you would have to at least once configure the 'Conceal' highlighting group (See the help at
:h hl-Conceal
This can be done in your colorscheme and then you do not need to reconfigure it again. But this affects all concealed chars (e.g. if your syntax script conceals some more items, they will be displayed as your white space chars). That could look like this::hi Conceal ctermfg=7 ctermbg=NONE guifg=LightGrey guibg=NONE
There seems to be a particularity that Vim will not highlight spaces, if the syntax script uses the
skipwhite
keyword. There will be no way around (perhaps this will be fixed, I posted a patch)list
mode. The latest one at the time of writing seems to be this one. (This means, you need to built your own Vim to use this).conceallevel
andconcealcursor
are window local options. That means they can be different in different windows (and will possibly be also set by filetype plugins or other plugin scripts).BufWinEnter
or possibly also aSyntax
or evenFileType
autocommand. (I have not tested which one actually works).The last two items means, you would have to setup some autocommands that reset the syntax rules and the correesponding options. For the first one, one might want to setup the highlighting using a
ColorScheme
autocommand (so that the concealed chars always look the same, independent of what a color scheme actually sets up). For a complete solution, look into romainl answer, that should give you a start. If you setup a function, you can easily setup a toggle command to switch displaying special Highlighting on or off.Update 10/26/2014 I made a plugin out of this question.
Update 04/22/2015 A patch has been included in Vim that makes this possible using the
list
option. Simply setset list listchars+=space:␣
This works as of Vim 7.4.711
我用它
来突出显示空白。它搜索所有空白,然后启用突出显示以使它们弹出。但是,它不打印特殊字符。
I use this
to highlight white spaces. It searches for all white spaces, and then enables the highlight to make them pop out. However, it does not print a special character.
如果您所说的空格指的是 ' ' 字符,我的建议只是搜索/替换。正如其他人所暗示的,
set list
将非打印字符更改为在listchars
中配置的可见字符。要明确地将空格显示为其他字符,类似于下面的内容应该可以解决问题:
:%s/ /█/g
然后只需撤消更改即可再次返回。
(为了获得 █,我按下了这个确切的按键序列::%s/ /CTRL-KFB/g)
If by whitespaces you mean the ' ' character, my suggestion would just be a search/replace. As the others have hinted,
set list
changes non printing characters to a visible character that's configured inlistchars
.To explicitly show spaces as some other character, something similar to the below should do the trick:
:%s/ /█/g
Then just undo the change to go back again.
(to get the █ I pressed this exact key sequence: :%s/ /CTRL-KFB/g)
要突出显示空格,只需搜索它:
/;
笔记:
启用搜索结果突出显示
突出显示空格和标签:
/[<空格><制表符>]
删除突出显示的快速方法是搜索其他内容:
/asdf
(只需键入任意简短的随机字符列表)
To highlight spaces, just search for it:
/<space>
Notes:
To highlight spaces & tabs:
/[<space><tab>]
A quick way to remove the highlights is to search for anything else:
/asdf
(just type any short list of random characters)
下面的代码基于 Christian Brabandt 的回答,似乎符合 OP 的要求:
将这些行附加到您的
~/.vimrc
并启动一个新的 Vim 会话来查看仍然不完美 魔法发生了。随意编辑默认颜色并隐藏字符。
注意:多种语言的
*FuncBody
语法组中的某些内容会阻止中间点的显示。我(还不知道?)如何使该解决方案更加可靠。The code below is based on Christian Brabandt's answer and seems to do what the OP wants:
Append those lines to your
~/.vimrc
and start a new Vim session to see the still imperfect magic happen.Feel free to edit the default colors and conceal character.
Caveat: something in the
*FuncBody
syntax group in several languages prevents the middle dot from showing. I don't know (yet?) how to make that solution more reliable.我没有从现有答案中找到我想要的答案。下面的代码将以亮红色突出显示所有尾随空格。只需将以下内容添加到您的
.vimrc
I didn't find exactly what I wanted from the existing answers. The code below will highlight all trailing spaces bright red. Simply add the following to your
.vimrc
我对这个问题的所有其他答案感到沮丧,因为它们都没有以有用的方式突出空格字符。将空格显示为字符对于空白格式的语言特别有帮助,在这种语言中混合制表符和空格是有害的。
我的解决方案是显示制表符并在多个空格下划线。它借用了 mrucci 的答案 和 本教程。因为它使用语法突出显示,所以它是持久的:
使用它,制表符显示为
|
,空格显示为_
,这使得我在混合代码样式时很容易分辨。我发现的唯一缺点是此代码片段不会调整背景颜色以匹配上下文(如评论中)。
I was frustrated with all of the other answers to this question, because none of them highlight the space character in a useful way. Showing spaces as characters would particularly help for whitespace-formatted languages, where mixing tabs and spaces is harmful.
My solution is to show tabs and underline multiple spaces. It borrows from mrucci's answer and this tutorial. Because it uses syntax highlighting, it's persistent:
Using this, tabs are displayed as
|
and spaces as_
, which makes it very easy to tell when I'm mixing code styles.The only downside I've found is that this snippet doesn't adjust background color to match the context (like in a comment).
覆盖 Unicode 空白字符:
结果:
│
”(两个字符:一个长管道,然后是一个序号空格;它们在colorscheme murphy
中为灰色)。·
”(一个字符;在colorscheme murphy
中为灰色)。To cover Unicode whitespace characters:
The result:
│
" (two characters: a long pipe and then an ordinal space; they are gray incolorscheme murphy
).·
" (one character; it's gray incolorscheme murphy
).上面的所有答案都试图使空格在 vim 中可见。如果你真的坚持将可见空间设置为点,还有另一种方法......
如果在 vim 中无法完成,请完全更改你的字体。我复制了 Ubuntu One Mono 字体并使用 FontForge 对其进行了编辑。请记住更改字体的全名、系列、首选系列、兼容全名(在 FontFoge 中,它位于字体信息中的 TTF 名称下),以便将其作为单独的字体。
只需编辑空格字符以在中间有一个点并将字体保存到 ~/.fonts
现在你可以将它用于你的 gvim 或整个终端......
我复制了“!”字符,删除线并将点移到中间。花了5分钟多一点...
注意:更改空格字符(0x20)会导致整个vim屏幕上有点的不便...(但它会将空格与制表符分开...)
all of the answers above try to make spaces visible from within vim. If you really insist on having visible spaces as dots, there's another approach...
If it cannot be done in vim, change your font entirely. I copied the Ubuntu One Mono font and edited it using FontForge. Remember to change the font's fullname, family, preferred family, compatible full (in FontFoge it's under TTF Names in the font info), in order to have it as a separate font.
Simply edit the space character to have a dot in the middle and save the font to ~/.fonts
Now you can use it for your gvim or the entire terminal...
I copied the "!" character, removed the line and moved the dot to the middle. It took a little more than 5 minutes...
Note: changing the space character (0x20) results in the inconvenience of having dots on the entire vim screen... (but it will separate the spaces from tabs...)
您可以用来
真正查看线条的结构。您将明确看到制表符和换行符。当你看到一片空白时,它确实是一片空白。
You could use
to really see the structure of a line. You will see tabs and newlines explicitly. When you see a blank, it's really a blank.
避免了“你必须搜索空间才能让它们显示”的情况,但不能将 afaict 配置为对空间执行非突出显示的操作。 CursorLine 可以是任何高亮组,在默认主题中它是一个简单的下划线。
avoids the "you have to search for spaces to get them to show up" bit but afaict can't be configured to do non-hilighting things to the spaces. CursorLine can be any hilighting group and in the default theme it's a plain underline.
我喜欢用特殊字符来显示空白,这样更清晰。甚至可以切换的地图也是一个关键功能,用于快速检查。
您可以在自 2004 年以来未更新的旧 vim 脚本中找到此功能:
vim-scripts/ [电子邮件受保护]
感谢项目 vim-scripts 和 vundle 你可以让这个插件复活
< a href="https://github.com/vim-scripts/cream-showinvisibles" rel="noreferrer">vim-scripts/cream-showinvisibles@github
更好的是,我的两分钱是添加可配置的快捷方式(而不是预定义的F4
)将其添加到 ~/.vimrc
在 vim 上安装插件
即可
I like using special characters to show whitespace, is more clear. Even a map to toggle is a key feature, for a quick check.
You can find this features in an old vim script not updated since 2004:
vim-scripts/[email protected]
Thanks to project vim-scripts and vundle you can come back to life this plugin
vim-scripts/cream-showinvisibles@github
Even better, my two cents on this is to add a configurable shortcut (instead of predefined F4)
so add this to ~/.vimrc
install plugin on vim
and there you go
将 这些 hack 作为注释保留在 .vimrc 中,因此在 shell 中,只需:
Keep those hacks in the .vimrc as comments, so in the shell, simply :
搜索
突出显示
.vimrc
中的,即搜索空格制表符和回车符
或搜索所有空白字符
或搜索所有非空白字符(空白字符未显示,因此您会看到空白字符在单词之间,但不是尾随空白字符)
以显示所有尾随空白字符 - 在行尾
highlight search
in
.vimrc
that isand search for space tabs and carriage returns
or search for all whitespace characters
of search for all non white space characters (the whitespace characters are not shown, so you see the whitespace characters between words, but not the trailing whitespace characters)
to show all trailing white space characters - at the end of the line
:se
就足够了,不需要:set
。:se
is enough,:set
isn't needed.您还可以突出显示空格(用块替换空格):(
在写入之前撤消它)
you can also highlight the spaces (replacing the spaces with a block):
(before writing undo it)
将其添加到我的 .vimrc 对我有用。只要确保没有其他任何冲突即可。
Adding this to my .vimrc works for me. Just make sure you don't have anything else conflicting..