[英]Make Vim show ALL white spaces as a character
I can't find a way to make Vim show all white spaces as a character.我找不到让 Vim 将所有空格显示为字符的方法。 All I found was about tabs, trailing spaces etc.
我发现的只是关于制表符、尾随空格等。
As others have said, you could use正如其他人所说,你可以使用
:set list
which will, in combination with这将与
:set listchars=...
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.现在,没有可用于显示空格的显式选项,但在 listchars 中,您可以设置一个字符以显示除空格之外的所有内容。 For example, mine looks like this
例如,我的看起来像这样
:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
so, now, after you use所以,现在,在你使用之后
:set list
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.像往常一样,要了解
listchars
工作原理,请使用帮助。 It provides great information about what chars can be displayed (like trailing space, for instance) and how to do it:它提供了有关可以显示哪些字符(例如尾随空格)以及如何显示的重要信息:
:help listchars
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 ):向它添加一个切换可能会有所帮助,这样您就可以轻松查看编辑中的更改(来源: VIM :set list! 作为 .vimrc 中的切换):
noremap <F5> :set list!<CR>
inoremap <F5> <C-o>:set list!<CR>
cnoremap <F5> <C-c>:set list!<CR>
As of patch 7.4.710 you can now set a character to show in place of space using listchars!从补丁7.4.710 开始,您现在可以使用 listchars 设置一个字符来代替空格显示!
:set listchars+=space:␣
So, to show ALL white space characters as a character you can do the following:因此,要将所有空白字符显示为字符,您可以执行以下操作:
:set listchars=eol:¬,tab:>·,trail:~,extends:>,precedes:<,space:␣
:set list
When you are finished, to hide the non-visible chars you would:完成后,要隐藏不可见的字符,您可以:
:set nolist
Discussion on mailing list: https://groups.google.com/forum/?fromgroups#!topic/vim_dev/pjmW6wOZW_Q邮件列表讨论: https : //groups.google.com/forum/?fromgroups#!topic/ vim_dev/ pjmW6wOZW_Q
:set list
to enable. :set list
启用。
:set nolist
to disable. :set nolist
禁用。
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:我认为这里的其他答案更全面,但我想我想分享一个我通常用来在视觉上区分制表符和空格的技巧:
:syntax on
:set syntax=whitespace
These are syntax highlighting rules for the Whitespace programming language - tabs show in green and spaces in red.这些是Whitespace 编程语言的语法高亮规则 - 标签显示为绿色,空格显示为红色。 :)
:)
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
结合使用,尽管选项卡将显示为 ^I 没有绿色突出显示,但空格将显示为红色。
:set list
will show all whitespaces as a character. :set list
会将所有空格显示为一个字符。 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.一切,但空间看起来会比正常状态,这意味着如果你仍然可以看到一个普通的老式空间,这真是一个普通的旧空间的不同。 :)
:)
If you set:如果你设置:
:highlight Search cterm=underline gui=underline ctermbg=none guibg=none ctermfg=none guifg=none
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.您可以在切换空格“下划线”的便捷功能中使用此命令。
set hls
let g:HLSpace = 1
let g:HLColorScheme = g:colors_name
function ToggleSpaceUnderscoring()
if g:HLSpace
highlight Search cterm=underline gui=underline ctermbg=none guibg=none ctermfg=none guifg=none
let @/ = " "
else
highlight clear
silent colorscheme "".g:HLColorScheme
let @/ = ""
endif
let g:HLSpace = !g:HLSpace
endfunction
Map the function to a shortcut key with:将函数映射到快捷键:
nmap <silent> <F3> <Esc>:call ToggleSpaceUnderscoring()<CR>
NB: Define the function in vimrc after the colorscheme has been set.注意:在设置颜色方案后在 vimrc 中定义函数。
Depending on your syntax rules for the current buffer, something like this could work:根据当前缓冲区的语法规则,这样的事情可能会起作用:
:syn match WhiteSpace / / containedin=ALL conceal cchar=Æ
:setl conceallevel=2 concealcursor=nv
This needs a vim 7.3 with +conceal feature这需要一个带有+隐藏功能的vim 7.3
Update 10/24/2014 To expand a little bit on that. 2014 年 10 月 24 日更新对此进行一些扩展。 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 (eg if your syntax script conceals some more items, they will be displayed as your white space chars). That could look like this:对于突出显示,您至少必须配置一次“隐藏”突出显示组(请参阅帮助
:h hl-Conceal
这可以在您的颜色方案中完成,然后您不需要再次重新配置它。但这会影响所有隐藏的字符(例如,如果您的语法脚本隐藏了更多项目,它们将显示为您的空白字符)。这可能如下所示:
: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.如果语法脚本使用
skipwhite
关键字,Vim 似乎不会突出显示空格的skipwhite
。 There will be no way around (perhaps this will be fixed, I posted a patch )没有办法解决(也许这会被修复,我发布了一个补丁)
list
mode.list
模式下的外观。 The latest one at the time of writing seems to be this one .conceallevel
and concealcursor
are window local options.conceallevel
和concealcursor
是窗口本地选项。 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 a Syntax
or even FileType
autocommand.BufWinEnter
或Syntax
甚至FileType
自动命令来完成。 (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).对于第一个,人们可能希望使用
ColorScheme
自动命令设置突出显示(这样隐藏的字符看起来总是相同的,而与实际设置的颜色方案无关)。 For a complete solution, look into romainl answer, that should give you a start.要获得完整的解决方案,请查看 romainl 答案,这应该会给您一个开始。 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. 2014 年 10 月 26 日更新我根据这个问题制作了一个插件。
Update 04/22/2015 A patch has been included in Vim that makes this possible using the list
option. 2015 年 4 月 22 日更新Vim 中包含了一个补丁,可以使用
list
选项实现这一点。 Simply set set list listchars+=space:␣
This works as of Vim 7.4.711只需设置
set list listchars+=space:␣
这适用于Vim 7.4.711
I use this我用这个
/\s
:set hlsearch
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.
但是,它不打印特殊字符。
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 in listchars
.正如其他人所暗示的那样,
set list
将非打印字符更改为在listchars
配置的可见字符。
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 / (为了得到 █,我按下了这个确切的键序列:: % s / / CTRL-K F B / g )
/ CTRL-K F B / g )
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删除突出显示的一种快速方法是搜索其他任何内容:/asdf
(just type any short list of random characters) (只需输入任何简短的随机字符列表)
The code below is based on Christian Brabandt's answer and seems to do what the OP wants:下面的代码基于 Christian Brabandt 的回答,似乎符合 OP 的要求:
function! Whitespace()
if !exists('b:ws')
highlight Conceal ctermbg=NONE ctermfg=240 cterm=NONE guibg=NONE guifg=#585858 gui=NONE
highlight link Whitespace Conceal
let b:ws = 1
endif
syntax clear Whitespace
syntax match Whitespace / / containedin=ALL conceal cchar=·
setlocal conceallevel=2 concealcursor=c
endfunction
augroup Whitespace
autocmd!
autocmd BufEnter,WinEnter * call Whitespace()
augroup END
Append those lines to your ~/.vimrc
and start a new Vim session to see the still imperfect magic happen.将这些行附加到您的
~/.vimrc
并开始一个新的 Vim 会话以查看仍然不完美的魔术发生。
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.警告:在
*FuncBody
中*FuncBody
语法组中的某些内容会阻止显示中间点。 I don't know (yet?) how to make that solution more reliable.我不知道(还?)如何使该解决方案更可靠。
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 .
它借鉴了mrucci 的回答和本教程。 Because it uses syntax highlighting, it's persistent:
因为它使用语法高亮,所以它是持久的:
set list listchars=tab:\|\
highlight Whitespace cterm=underline gui=underline ctermbg=NONE guibg=NONE ctermfg=yellow guifg=yellow
autocmd ColorScheme * highlight Whitespace gui=underline ctermbg=NONE guibg=NONE ctermfg=yellow guifg=yellow
match Whitespace / \+/
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).我发现的唯一缺点是此代码段不会调整背景颜色以匹配上下文(例如在评论中)。
all of the answers above try to make spaces visible from within vim.上面的所有答案都试图使 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.如果不能在 vim 中完成,请完全更改您的字体。 I copied the Ubuntu One Mono font and edited it using FontForge.
我复制了 Ubuntu One Mono 字体并使用 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.
请记住更改字体的全名、系列、首选系列、兼容全(在 FontFoge 中,它位于字体信息中的 TTF 名称下),以便将其作为单独的字体使用。 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 "!"
只需编辑空格字符在中间有一个点并将字体保存到 ~/.fonts 现在你可以将它用于你的 gvim 或整个终端......我复制了“!” character, removed the line and moved the dot to the middle.
字符,删除线并将点移到中间。 It took a little more than 5 minutes...
花了5分钟多一点...
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...)注意:更改空格字符(0x20)会导致整个vim屏幕上都有点的不便......(但它会将空格与制表符分开......)
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
只需将以下内容添加到您的
.vimrc
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()
To cover Unicode whitespace characters:覆盖 Unicode 空白字符:
set list
set listchars=tab:│\ ,nbsp:·
highlight StrangeWhitespace guibg=Red ctermbg=Red
" The list is from https://stackoverflow.com/a/37903645 (with `\t`, `\n`, ` `, `\xa0` removed):
call matchadd('StrangeWhitespace', '[\x0b\x0c\r\x1c\x1d\x1e\x1f\x85\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]')
The result:结果:
") │
" (two characters: a long pipe and then an ordinal space; they are gray in colorscheme murphy
)│
”(两个字符:一个长管道,然后是一个序数空间;它们在colorscheme murphy
是灰色的)·
" (one character; it's gray in colorscheme murphy
)·
”(一个字符;在colorscheme murphy
灰色)
" 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:您可以在自 2004 年以来未更新的旧 vim 脚本中找到此功能:
vim-scripts/cream-showinvisibles@vim.org vim-scripts/cream-showinvisibles@vim.org
Thanks to project vim-scripts and vundle you can come back to life this plugin感谢项目vim-scripts和vundle,你可以让这个插件恢复生机
vim-scripts/cream-showinvisibles@github vim-scripts/cream-showinvisibles@github
Even better, my two cents on this is to add a configurable shortcut (instead of predefined F4 )更好的是,我对此的两分钱是添加一个可配置的快捷方式(而不是预定义的F4 )
so add this to ~/.vimrc所以把它添加到~/.vimrc
Plugin 'albfan/cream-invisibles'
let g:creamInvisibleShortCut = "<F5>" "for my F4 goto next error
install plugin on vim在vim上安装插件
:PluginInstall
and there you go你去吧
:match CursorLine /\s\+/
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.避免了“你必须搜索空间才能让它们显示出来”这一点,但 afaict 不能被配置为对空间做非突出的事情。 CursorLine can be any hilighting group and in the default theme it's a plain underline.
CursorLine 可以是任何高亮组,在默认主题中它是一个简单的下划线。
You could use你可以用
:set list
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.
当你看到一片空白时,它真的是一片空白。
highlight search突出显示搜索
:set hlsearch
in .vimrc
that is在
.vimrc
中
and search for space tabs and carriage returns并搜索空格制表符和回车
/ \|\t\|\r
or search for all whitespace characters或搜索所有空白字符
/\s
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)搜索所有非空白字符(未显示空白字符,因此您会看到单词之间的空白字符,但看不到尾随的空白字符)
/\S
to show all trailing white space characters - at the end of the line显示所有尾随空格字符 - 在行尾
/\s$
Keep those hacks in the .vimrc as comments, so in the shell, simply :将这些 hack作为注释保留在 .vimrc 中,因此在 shell 中,只需:
echo '
" how-to see the non-visible while spaces
" :set listchars=eol:¬,tab:>·,trail:~,extends:>,precedes:<,space:␣
" set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
" :set list
" but hei how-to unset the visible tabs ?!
" :set nolist
' >> ~/.vimrc
:se list
:se nolist
:se
is enough, :set
isn't needed. :se
就足够了,不需要:set
。
you can also highlight the spaces (replacing the spaces with a block):您还可以突出显示空格(用块替换空格):
:%s/ /█/g
(before writing undo it) (在写之前撤消它)
Adding this to my .vimrc works for me.将此添加到我的 .vimrc 对我有用。 Just make sure you don't have anything else conflicting..
只要确保你没有任何其他冲突..
autocmd VimEnter * :syn match space /\s/
autocmd VimEnter * :hi space ctermbg=lightgray ctermfg=black guibg=lightgray guifg=black
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.