show illegal whitespace (such as unbreakable space) in source code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I sometimes type (by accident) shift-space, inserting unbreakable space into code. With c++, I get this:



error: stray ‘302’ in program
error: stray ‘240’ in program


while in other formats (like RestructuredText), I am hunting mysterious errors of syntax without any visible cause.



Is there a way to



  • always highlight unbreakable spaces (and perhaps other type of "illegal" whitespace, i.e. any whitespace besides space and tab)

  • map shift-space to space, so that these characters are not inserted in the first place?

Thanks!










share|improve this question







New contributor




eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    1
    down vote

    favorite












    I sometimes type (by accident) shift-space, inserting unbreakable space into code. With c++, I get this:



    error: stray ‘302’ in program
    error: stray ‘240’ in program


    while in other formats (like RestructuredText), I am hunting mysterious errors of syntax without any visible cause.



    Is there a way to



    • always highlight unbreakable spaces (and perhaps other type of "illegal" whitespace, i.e. any whitespace besides space and tab)

    • map shift-space to space, so that these characters are not inserted in the first place?

    Thanks!










    share|improve this question







    New contributor




    eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I sometimes type (by accident) shift-space, inserting unbreakable space into code. With c++, I get this:



      error: stray ‘302’ in program
      error: stray ‘240’ in program


      while in other formats (like RestructuredText), I am hunting mysterious errors of syntax without any visible cause.



      Is there a way to



      • always highlight unbreakable spaces (and perhaps other type of "illegal" whitespace, i.e. any whitespace besides space and tab)

      • map shift-space to space, so that these characters are not inserted in the first place?

      Thanks!










      share|improve this question







      New contributor




      eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I sometimes type (by accident) shift-space, inserting unbreakable space into code. With c++, I get this:



      error: stray ‘302’ in program
      error: stray ‘240’ in program


      while in other formats (like RestructuredText), I am hunting mysterious errors of syntax without any visible cause.



      Is there a way to



      • always highlight unbreakable spaces (and perhaps other type of "illegal" whitespace, i.e. any whitespace besides space and tab)

      • map shift-space to space, so that these characters are not inserted in the first place?

      Thanks!







      key-bindings highlight unicode






      share|improve this question







      New contributor




      eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      eudoxos

      1083




      1083




      New contributor




      eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      eudoxos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I do have this in my custom vimrc file, which highlights git conflict markers as well as special whitespace characters.



          " highlight VCS conflict markers
          " highlight strange Whitespace
          aug CustomHighlighting
          au!
          au WinEnter * if !exists("w:custom_hi1") | let w:custom_hi1 = matchadd('ErrorMsg', '^(<|=|>)7([^=].+)?$') | endif
          au WinEnter * if !exists("w:custom_hi2") | let w:custom_hi2 = matchadd('ErrorMsg', '[x0bx0cu00a0u1680u180eu2000-u200au2028u202fu205fu3000ufeff]') | endif
          aug END


          kudos to Benjamin Haskell via the vim_use mailinglist






          share|improve this answer



























            up vote
            2
            down vote













            You can use



            :syntax match ErrorMsg " "


            while inserting an unbreakable space between the "'s.



            To automatically load the rule, you can simply add



            au VimEnter,BufWinEnter * syn match ErrorMsg " "


            to your .vimrc.



            If you want to customize the highlighting, create a custom rule before the syn match, i.e.



            highlight UnbreakableSpace ctermbg=red guibg=red
            au VimEnter,BufWinEnter * syn match UnbreakableSpace " "





            share|improve this answer



























              up vote
              2
              down vote













              I don't think you can use the <S-Space> notation in a mapping* but I think you should be able to map it by pressing Shift+Space when typing in the mapping where I've written [lhs] below:



              :inoremap [lhs] <Space>


              Of course, when reading your .vimrc, this will be a bit confusing, so you might be better off using an :execute command and Vim's notation for adding Unicode characters to a string:



              execute "inoremap u00A0 <Space>"


              (N.B. I'm not entirely sure which character you are inserting when you press Shift+Space: you may need to adjust the Unicode value accordingly.)



              * I think that the keystrokes are probably converted in your terminal and never make it to Vim. So Vim only sees the unbreakable space: it doesn't know you pressed Shift+Space.






              share|improve this answer






















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "599"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );






                eudoxos is a new contributor. Be nice, and check out our Code of Conduct.









                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f17694%2fshow-illegal-whitespace-such-as-unbreakable-space-in-source-code%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                I do have this in my custom vimrc file, which highlights git conflict markers as well as special whitespace characters.



                " highlight VCS conflict markers
                " highlight strange Whitespace
                aug CustomHighlighting
                au!
                au WinEnter * if !exists("w:custom_hi1") | let w:custom_hi1 = matchadd('ErrorMsg', '^(<|=|>)7([^=].+)?$') | endif
                au WinEnter * if !exists("w:custom_hi2") | let w:custom_hi2 = matchadd('ErrorMsg', '[x0bx0cu00a0u1680u180eu2000-u200au2028u202fu205fu3000ufeff]') | endif
                aug END


                kudos to Benjamin Haskell via the vim_use mailinglist






                share|improve this answer
























                  up vote
                  2
                  down vote



                  accepted










                  I do have this in my custom vimrc file, which highlights git conflict markers as well as special whitespace characters.



                  " highlight VCS conflict markers
                  " highlight strange Whitespace
                  aug CustomHighlighting
                  au!
                  au WinEnter * if !exists("w:custom_hi1") | let w:custom_hi1 = matchadd('ErrorMsg', '^(<|=|>)7([^=].+)?$') | endif
                  au WinEnter * if !exists("w:custom_hi2") | let w:custom_hi2 = matchadd('ErrorMsg', '[x0bx0cu00a0u1680u180eu2000-u200au2028u202fu205fu3000ufeff]') | endif
                  aug END


                  kudos to Benjamin Haskell via the vim_use mailinglist






                  share|improve this answer






















                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    I do have this in my custom vimrc file, which highlights git conflict markers as well as special whitespace characters.



                    " highlight VCS conflict markers
                    " highlight strange Whitespace
                    aug CustomHighlighting
                    au!
                    au WinEnter * if !exists("w:custom_hi1") | let w:custom_hi1 = matchadd('ErrorMsg', '^(<|=|>)7([^=].+)?$') | endif
                    au WinEnter * if !exists("w:custom_hi2") | let w:custom_hi2 = matchadd('ErrorMsg', '[x0bx0cu00a0u1680u180eu2000-u200au2028u202fu205fu3000ufeff]') | endif
                    aug END


                    kudos to Benjamin Haskell via the vim_use mailinglist






                    share|improve this answer












                    I do have this in my custom vimrc file, which highlights git conflict markers as well as special whitespace characters.



                    " highlight VCS conflict markers
                    " highlight strange Whitespace
                    aug CustomHighlighting
                    au!
                    au WinEnter * if !exists("w:custom_hi1") | let w:custom_hi1 = matchadd('ErrorMsg', '^(<|=|>)7([^=].+)?$') | endif
                    au WinEnter * if !exists("w:custom_hi2") | let w:custom_hi2 = matchadd('ErrorMsg', '[x0bx0cu00a0u1680u180eu2000-u200au2028u202fu205fu3000ufeff]') | endif
                    aug END


                    kudos to Benjamin Haskell via the vim_use mailinglist







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 3 hours ago









                    Christian Brabandt

                    14.5k2443




                    14.5k2443




















                        up vote
                        2
                        down vote













                        You can use



                        :syntax match ErrorMsg " "


                        while inserting an unbreakable space between the "'s.



                        To automatically load the rule, you can simply add



                        au VimEnter,BufWinEnter * syn match ErrorMsg " "


                        to your .vimrc.



                        If you want to customize the highlighting, create a custom rule before the syn match, i.e.



                        highlight UnbreakableSpace ctermbg=red guibg=red
                        au VimEnter,BufWinEnter * syn match UnbreakableSpace " "





                        share|improve this answer
























                          up vote
                          2
                          down vote













                          You can use



                          :syntax match ErrorMsg " "


                          while inserting an unbreakable space between the "'s.



                          To automatically load the rule, you can simply add



                          au VimEnter,BufWinEnter * syn match ErrorMsg " "


                          to your .vimrc.



                          If you want to customize the highlighting, create a custom rule before the syn match, i.e.



                          highlight UnbreakableSpace ctermbg=red guibg=red
                          au VimEnter,BufWinEnter * syn match UnbreakableSpace " "





                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            You can use



                            :syntax match ErrorMsg " "


                            while inserting an unbreakable space between the "'s.



                            To automatically load the rule, you can simply add



                            au VimEnter,BufWinEnter * syn match ErrorMsg " "


                            to your .vimrc.



                            If you want to customize the highlighting, create a custom rule before the syn match, i.e.



                            highlight UnbreakableSpace ctermbg=red guibg=red
                            au VimEnter,BufWinEnter * syn match UnbreakableSpace " "





                            share|improve this answer












                            You can use



                            :syntax match ErrorMsg " "


                            while inserting an unbreakable space between the "'s.



                            To automatically load the rule, you can simply add



                            au VimEnter,BufWinEnter * syn match ErrorMsg " "


                            to your .vimrc.



                            If you want to customize the highlighting, create a custom rule before the syn match, i.e.



                            highlight UnbreakableSpace ctermbg=red guibg=red
                            au VimEnter,BufWinEnter * syn match UnbreakableSpace " "






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 3 hours ago









                            Mr Shunz

                            1862




                            1862




















                                up vote
                                2
                                down vote













                                I don't think you can use the <S-Space> notation in a mapping* but I think you should be able to map it by pressing Shift+Space when typing in the mapping where I've written [lhs] below:



                                :inoremap [lhs] <Space>


                                Of course, when reading your .vimrc, this will be a bit confusing, so you might be better off using an :execute command and Vim's notation for adding Unicode characters to a string:



                                execute "inoremap u00A0 <Space>"


                                (N.B. I'm not entirely sure which character you are inserting when you press Shift+Space: you may need to adjust the Unicode value accordingly.)



                                * I think that the keystrokes are probably converted in your terminal and never make it to Vim. So Vim only sees the unbreakable space: it doesn't know you pressed Shift+Space.






                                share|improve this answer


























                                  up vote
                                  2
                                  down vote













                                  I don't think you can use the <S-Space> notation in a mapping* but I think you should be able to map it by pressing Shift+Space when typing in the mapping where I've written [lhs] below:



                                  :inoremap [lhs] <Space>


                                  Of course, when reading your .vimrc, this will be a bit confusing, so you might be better off using an :execute command and Vim's notation for adding Unicode characters to a string:



                                  execute "inoremap u00A0 <Space>"


                                  (N.B. I'm not entirely sure which character you are inserting when you press Shift+Space: you may need to adjust the Unicode value accordingly.)



                                  * I think that the keystrokes are probably converted in your terminal and never make it to Vim. So Vim only sees the unbreakable space: it doesn't know you pressed Shift+Space.






                                  share|improve this answer
























                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    I don't think you can use the <S-Space> notation in a mapping* but I think you should be able to map it by pressing Shift+Space when typing in the mapping where I've written [lhs] below:



                                    :inoremap [lhs] <Space>


                                    Of course, when reading your .vimrc, this will be a bit confusing, so you might be better off using an :execute command and Vim's notation for adding Unicode characters to a string:



                                    execute "inoremap u00A0 <Space>"


                                    (N.B. I'm not entirely sure which character you are inserting when you press Shift+Space: you may need to adjust the Unicode value accordingly.)



                                    * I think that the keystrokes are probably converted in your terminal and never make it to Vim. So Vim only sees the unbreakable space: it doesn't know you pressed Shift+Space.






                                    share|improve this answer














                                    I don't think you can use the <S-Space> notation in a mapping* but I think you should be able to map it by pressing Shift+Space when typing in the mapping where I've written [lhs] below:



                                    :inoremap [lhs] <Space>


                                    Of course, when reading your .vimrc, this will be a bit confusing, so you might be better off using an :execute command and Vim's notation for adding Unicode characters to a string:



                                    execute "inoremap u00A0 <Space>"


                                    (N.B. I'm not entirely sure which character you are inserting when you press Shift+Space: you may need to adjust the Unicode value accordingly.)



                                    * I think that the keystrokes are probably converted in your terminal and never make it to Vim. So Vim only sees the unbreakable space: it doesn't know you pressed Shift+Space.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 3 hours ago

























                                    answered 3 hours ago









                                    Rich

                                    13.8k11763




                                    13.8k11763




















                                        eudoxos is a new contributor. Be nice, and check out our Code of Conduct.









                                         

                                        draft saved


                                        draft discarded


















                                        eudoxos is a new contributor. Be nice, and check out our Code of Conduct.












                                        eudoxos is a new contributor. Be nice, and check out our Code of Conduct.











                                        eudoxos is a new contributor. Be nice, and check out our Code of Conduct.













                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f17694%2fshow-illegal-whitespace-such-as-unbreakable-space-in-source-code%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        List of Gilmore Girls characters

                                        One-line joke