better way to always highlight NS?

13 views
Skip to first unread message

Anton Sharonov

unread,
Jul 10, 2022, 8:46:22 AM7/10/22
to vim...@googlegroups.com
Hi all,

I am trying to always highlight non-breakable spaces in all my
buffers, regardless of the filetype.

One way I found so far is:

autocmd BufReadPost *
\ some |
\ other |
\ stuff |
\ exec "3mat Todo /\xc2\xa0/"


This has 2 small disadvantages for me:

- makes all a bit slower (probably unavoidable)?
- consumes one of available :mat :2mat :3mat commands

Are there some better ways perhaps?

--
With best regards, Anton

PS: By the way, even ":set list" do not show them so those NS are
really nasty if you don't expect them )))

BPJ

unread,
Jul 10, 2022, 1:08:16 PM7/10/22
to vim_use


Den sön 10 juli 2022 14:47Anton Sharonov <anton.s...@gmail.com> skrev:
Hi all,

I am trying to always highlight non-breakable spaces in all my
buffers, regardless of the filetype.

One way I found so far is:

  autocmd BufReadPost *
    \ some  |
    \ other |
    \ stuff |
    \ exec "3mat Todo /\xc2\xa0/"


This has 2 small disadvantages for me:

- makes all a bit slower (probably unavoidable)?
- consumes one of available :mat :2mat :3mat commands

Maybe :h matchadd() ?

And why not use /\%u00a0/ if you use UTF-8 encoding anyway?

:h 'encoding'  :h 'fileencoding'


Are there some better ways perhaps?

--
With best regards, Anton

PS: By the way, even ":set list" do not show them so those NS are
really nasty if you don't expect them )))

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/YsrKELzdt1LFm/FV%40DESKTOP-75K4M1M.

M

unread,
Jul 10, 2022, 1:22:40 PM7/10/22
to vim...@googlegroups.com
вс, 10 июл. 2022 г. в 15:46, Anton Sharonov <anton.s...@gmail.com>:
--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/YsrKELzdt1LFm/FV%40DESKTOP-75K4M1M.

Recent Vim has support for NBSP. like `:set list listchars=nbsp:?`

Regards,
Matvey

Anton Sharonov

unread,
Jul 10, 2022, 2:16:18 PM7/10/22
to Richard Hartmann


Anton Sharonov <anton.s...@gmail.com> schrieb am So., 10. Juli 2022, 14:46:
Hi all,

I am trying to always highlight non-breakable spaces in all my
buffers, regardless of the filetype.

One way I found so far is:

  autocmd BufReadPost *
    \ some  |
    \ other |
    \ stuff |
    \ exec "3mat Todo /\xc2\xa0/"


Quick WARNING for anybody reading it: WARNING (just learned), only :mat and :2mat are free, please NEVER use :3mat. (see documentation, read it careful).

Anton

Anton Sharonov

unread,
Jul 10, 2022, 4:42:24 PM7/10/22
to vim...@googlegroups.com
On Sun, Jul 10, 2022 at 07:07:58PM +0200, BPJ wrote:
> Den sön 10 juli 2022 14:47Anton Sharonov <anton.s...@gmail.com> skrev:
>
> > Hi all,
> >
> > I am trying to always highlight non-breakable spaces in all my
> > buffers, regardless of the filetype.
> >
> > One way I found so far is:
> >
> > autocmd BufReadPost *
> > \ some |
> > \ other |
> > \ stuff |

----------------vvv WARNING, never use this (see documentation)!
> > \ exec "3mat Todo /\xc2\xa0/"
----------------^^^

> >
> >
> > This has 2 small disadvantages for me:
> >
> > - makes all a bit slower (probably unavoidable)?
> > - consumes one of available :mat :2mat :3mat commands
> >
>
> Maybe :h matchadd() ?
>
> And why not use /\%u00a0/ if you use UTF-8 encoding anyway?
>
> :h 'encoding' :h 'fileencoding'

Hi BPJ,

Thanks for pointing to matchadd().

The best thing what i can manage:

~~~
" ************************************
" Update or create match with given id
" Otherwise if the buffer is
" re-read plane matchadd() will complain that id already taken.
" Always matchdelete() before is not an option because very first
" time it complain that id not defined yet
" ************************************
function! MatchUpdate (group, pattern, prio, id)
for l:m in getmatches()
if get(l:m, "id") == a:id
call matchdelete(a:id)
break
endif
endfor
call matchadd(a:group, a:pattern, a:prio, a:id)
endfunction

autocmd BufReadPost *
\ call MatchUpdate("Todo", "\\%u00a0", 99, 2001)
~~~

Basically it works but:

- if the buffer is :vsplit or :split highlighting is lost in one
of the splits and come back again only after :e in this split.

- same story in the :vnew or :new buffer, highlighting appears
only after ":sav something" with subsequent ":e".

To make it more robust have to change to:

~~~
autocmd BufWinEnter,WinNew *
\ call MatchUpdate("Todo", "\\%u00a0", 99, 2001)
~~~

This seems to cover all corner cases so far...

--
Anton
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CADAJKhBeKQTd5qFHO9OG3QhZZszR89KfgQGFdUZ6v%2BwYuAPpmg%40mail.gmail.com.

Anton Sharonov

unread,
Jul 10, 2022, 4:57:58 PM7/10/22
to vim...@googlegroups.com
On Sun, Jul 10, 2022 at 08:22:33PM +0300, M wrote:
> вс, 10 июл. 2022 г. в 15:46, Anton Sharonov <anton.s...@gmail.com>:
>
> > Hi all,
> >
> > I am trying to always highlight non-breakable spaces in all my
> > buffers, regardless of the filetype.


> > PS: By the way, even ":set list" do not show them so those NS are
> > really nasty if you don't expect them )))
>
> Recent Vim has support for NBSP. like `:set list listchars=nbsp:?`

Hi Matvey,

Ah, that's nice! How could I forgot that everything can be
configured in Vim ))). Decided to go with "white square" instead
of plane "?" in my vimrc:

" show non-breakable spaces with white square (UTF16 0x25a1) when ":set list"
exec "set listchars=nbsp:\u25a1"

--
Anton

>
> Regards,
> Matvey
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAFsTB%2BJ1O8Z4TrSAt9tRjyOhy-1C_xYOGEeQ%3DbT%2B4QGzepX1hQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages