2

When I use an "alt-gr" character followed by a 'space' character often I miss my input in typing in place of it the "non breaking space character" aka nbsp.

Example with piped commands : mount |<space>tail vs mount |<nbsp>tail The last command will trigger an error ..." tail" command unfindable... (notice the visual space before the tail command's name which is in fact a nbsp character).

So I found a solution which works perfectly in my case, to simply neutralizing the nbsp character :

setxkbmap -option "nbsp:none"

But this changement gains instantly the whole system and for the proof, in LibreOffice Writer if I want to add a nbsp character between two words which must stay grouped, this does not working.

From past, I lost a lot of time to determine what was the cause of a non working command or a script or a service which does not start, because of that damn character.

So I asked to myself if there is a solution to make the neutralisation limited only to the shell (console, terminal, vty, tty, and so) ? In bash or other shell ...

Could you give me what role this damn nbsp character have in shell ? Where and when using it and for what ? What we loose to not using it ?

Another suggestion : instead of neutralize it can we reveal it by a graphic character with color ?

10
  • 1
  • @KamilMaciorowski Thank you. I ever tested it but when I tried to edit a file with editor like vim, I observed the nbsp are not neutralized with this solution (or I do not apply it correctly). This is why I use setxkbmap which neutralize everywhere from shell ... to the GUI application. Obviously I can too parse files after to neutralize all unwanted characters but for me this is a very bad solution. Sep 11, 2021 at 21:38
  • Maybe your terminal emulator can translate. What is it? Sep 12, 2021 at 7:07
  • I am not sure to understand your question. I use many different access to the shell from GUI terminals (PTS like LXTerminal, Terminal Mate, Terminal XFCE, XTerm, and so) to TTY essentialy from Debian and Ubuntu systems with or without Desktop Environment (I often choose XFCE as DE). Sep 12, 2021 at 10:02
  • 1
    BTW vim is a text editor and LibreOffice Writer is a text editor, still you want to disable nbsp in one but not in the other, as if accidental nbsps didn't happen for you in Writer or as if you never needed nbsp in vim. Consider disabling nbsp with setxkbmap like you did; additionally set up another keystroke to type npsp, one that is less likely to be activated accidentally. Then you will use the new keystroke in any editor iff you really need nbsp. Sep 12, 2021 at 10:49

1 Answer 1

2

As Kamil made me realise it the NBSP functionality do will not serve me in nearly 95% of time. So I can disable it and for the 5% remaining, I will have to implement a distinct patch to allow me to insert this character for all the applications which will need.

Foreword : to be sure to speak of the same thing I want to present to you what is for me the difference between a PTS console and a TTY console. PTS are for me the GUI consoles (graphical) that we open from the Desktop Environment and TTY are for me the CLI (command line) that we open with the combination of Ctrl-Alt-Fn (n representing the session number and generally between 1 and 7, 7 reserved by default to the X11 graphical session).

Disable NBSP character from X11 environment

So based on my own searches (see above), I can disable NBSP character from my X11 environment. As X11 is a graphical environment, the disabling must be executed in graphical environment.

So I propose to implement it in an autostart script (for me from XFCE DE) :

user@host:~$ cat <<EOF > $HOME/.config/autostart/setxkbmap_no_nbsp.desktop
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=No NBSP
Comment=Disable NBSP key
Exec=/usr/bin/setxkbmap -option "nbsp:none"
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false
EOF

Redirect NBSP shortkey for Libre Office Writer

Sometimes from Libre Office Writer I need to insert NBSP character, but as it is disabled (see above), I must still find a way to insert this character.

I propose to use the Ctrl-Shift-space to do the replace.

So for the macro (Available here : Tools → Macros → Organize Macros → LOdev Basic → My macros > Standard > Module1 > Edit) :

REM  *****  BASIC  *****

' Based on : https://ask.libreoffice.org/t/special-characters/1089/3
Sub InsertNBSP
 Dim oDoc as variant
  oDoc = ThisComponent
 Dim oCurrentController as variant
  oCurrentController = oDoc.getCurrentController()
 Dim oTextViewCursor as variant
  oTextViewCursor = oCurrentController.getViewCursor()
 Dim oText as variant
 If IsEmpty(oTextViewCursor.Cell) Then
  oText=oTextViewCursor.Text
 Else
  oText=oTextViewCursor.Cell.Text
 End If
 ' NBSP unicode : https://www.compart.com/en/unicode/U+00A0
 oText.insertString(oTextViewCursor,_
  chr(clng("&H00A0&")),false)
End Sub

Nota : notice that the NBSP unicode code is U+00A0 and furthermore in my case the resulting code is written at $HOME/.config/libreoffice/4/user/basic/Standard/Module1.xba

And for the key bindding (Tools → Customize… → Keyboard), we will use Ctrl-Shift-space to bind with the macro's function named InsertNBSP

Nota : in my case the resulting association is written at $HOME/.config/libreoffice/4/user/registrymodifications.xcu


Reveal some characters from vim (included NBSP)

Based on this source I found a way to reveal all important characters in vim (which is my text editor of predilection) colourized in green (in my case). This is useful in TTY console because nothing prevent to type NBSP.

Important: depending on whether we access the shell from PTS or TTY console, we can use the unicode characters (in the first case) or partially (in the second case, but apparently it is possible to make the TTY support the full unicode - not tested).

Prerequisite : before add the following configuration lines, ensure that vim is yet installed

# See more here : https://vimhelp.org/options.txt.html#%27listchars%27
user@host:~$ mkdir .vim
user@host:~$ echo -e "set list\nset listchars=tab:\>\ ,trail:·,nbsp:?" >> $HOME/.vim/vimrc

Notes:

  • tab will revealing all tabs to differentiate with spaces group and they will be replaced by greater than (>)
  • trail will revealing all trailing spaces in the end of a line and they will be replaced by middle dot (·) ; to type this unicode see below
  • nbsp will revealing all NBSP characters which will not be useful in the majority of cases (not to saying never) and they will replaced by question mark (?)
  • although that is not recommended in reason of the fact that will working only from PTS console (unless we manage to integrate full unicode support from TTY) it is possible to use some unicode characters more meaningful or exotic ; for example tab could be composed by ⇥ and nbsp by "☠" (see below)

Use unicode character from PTS console

Important : this method works from my configuration, but I do not know if it works everywhere

To type the tab character (see other codes below) :

  • From shell (source) : ctrl-shift-u 21e5

  • From vim (source) : ctrl-v u 21e5

Unicode characters from PTS :

Disable NBSP character from TTY shell console

The last case not treated is the TTY console. As @Kamil suggested me it is possible to disable the NBSP from it in adding the following lines to the $HOME/.bashrc file :

user@host:~$ echo -e "bind '\"\302\240\":\" \"'" >> $HOME/.bashrc

To conclude

For now we can disable NBSP character from X11 so also PTS bash shell and vim in PTS with setxkbmap -option "nbsp:none" (to make is persistent I implement it in a autostart script located in $HOME/.config/autostart). We can also disable from TTY bash shell with bind '"\302\240":" "' in $HOME/.bashrc. We can be warned to the usage of NBSP (and other problematic characters) with vim in TTY with listchars configuration in $HOME/.vim/vimrc. Finally we can always add some wanted NBSP characters in Libre Office Writer (which depends too of the X11, so NBSP is prevented), in redirecting the NBSP shortkey to another key combination.

For the end word I would say that all different environment cases are not treated but I think what is said here is a good start point to find a way for this other cases.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .