Skip to content

Instantly share code, notes, and snippets.

@andscoop
Created February 17, 2018 14:10
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andscoop/874ff042a28ca5aaa06426a58aacaf83 to your computer and use it in GitHub Desktop.
Save andscoop/874ff042a28ca5aaa06426a58aacaf83 to your computer and use it in GitHub Desktop.
fix nice-editor (ne) backspace issue
KEY 0x7f BS
KEY 0x115 DC
@andscoop
Copy link
Author

Remaps the delete key on mac keyboards so that backspace performs as expected.

lives in ~/.ne/.keys

@churchers
Copy link

Thanks!
Seemed to work perfectly for me on FreeBSD

@carlsmith
Copy link

This also fixes ne when running in Termux (a shell and environment that makes Android feel like a proper Linux (with or without root).

You can use pkg install ne to get the editor, and that automatically installs the .ne directory in your Termux home directory.

With Hackers Keyboard in portrait mode, you can comfortably edit text on your phone with ne.

@vigna
Copy link

vigna commented Aug 18, 2018

But this in principle won't work in other systems. ASCII 127 is delete, not backspace...

Copy link

ghost commented Oct 15, 2018

Seems not to work when ne is run as root. Any suggestions?

EDIT: Oops, duh. This needs to be installed for the root user as well.

@ShikiSuen
Copy link

mkdir ~/.ne
echo "KEY 0x7f BS" >> ~/.ne/.keys
echo "KEY 0x115 DC" >> ~/.ne/.keys

@craigbarnes
Copy link

craigbarnes commented Oct 19, 2019

But this in principle won't work in other systems. ASCII 127 is delete, not backspace...

@vigna I just checked the ncurses terminfo.src file. The list of terminals that send 0x7F for kdch1 is almost entirely composed of obsolete hardware terminals that no one uses (or even emulates) any more.

On the other hand, the list of terminals that send 0x7F for kbs (both according to terminfo and in practice) includes quite a few terminal emulators still in current use, including linux, st, mlterm-256color, konsole-256color, iTerm.app, iTerm2.app, etc. Many terminals that set TERM=xterm-256color also send 0x7F for Backspace in practice (despite the terminfo entry using kbs=^H), including most libvte-based terminals, the Termux terminal etc.

The historical naming of the key code seems somewhat less important than out-of-the-box compatibility with terminals people actually use.

@utoddl
Copy link

utoddl commented Oct 20, 2019

You have eloquently described a long-standing bug with ncurses, not ne. I hardly think the solution is for every application to ignore the terminfo information for this one case rather than to fix the broken terminal definitions.

@craigbarnes
Copy link

craigbarnes commented Oct 20, 2019

I hardly think the solution is for every application to ignore the terminfo information

ncurses may be partly to blame, but there's a solution that fixes the problem without many practical downsides. Since ^H and ^? are both control keys, you can just treat them like other control keys (i.e. not as "special" keys) and then just bind both to erase by default. I don't know if that's compatible with the ne key binding model but it's what I do in dte. In theory, it could cause the inverse of the current problem, but the terminals where that could happen all seem like relics.

@vigna
Copy link

vigna commented Oct 20, 2019

Well, in the last 26 (!) years we tried to Do The Right Thing. At this point, I think we should continue to Do The Right Thing. Maybe it's too much to hope that terminal implementors and terminfo database maintainers fix this in the right way, but for the time being it seems to be a very minor nuisance.

@utoddl
Copy link

utoddl commented Oct 21, 2019

@craigbarnes If I'm reading your code correctly (dte/terminfo.c), you're using "kdch1" from terminfo as Delete, but that code doesn't even attempt to get anything from terminfo for backspace, just assuming it's either ^H or ^?. I can see several reasons for taking that approach: (1) it appears to work pretty much everywhere, (2) a lot of otherwise useful terminfo files are just dead wrong wrt backspace anyway, and (3) everybody else does it because of 1 and 2. I'd really like ne to "just work", but I'd like it to be because the terminfos are right, not because we're intentionally ignoring it. For a key as fundamental as backspace, I can't understand how so many terminfo files can be so wrong, nor why it seems impossible to get them fixed.

Ignoring short term patches and workarounds for the moment, I'm asking you -- as you've clearly thought about the problem and have some skin in the game so to speak with your own editor which has to deal with this -- what would have to happen to get the backspace key definition fixed in some significant portion of the terminfo definitions where it's currently broken? I really want to know.

I can imagine a simple interactive program that asks you to press different keys and it tells you where your terminfo is right/wrong, maybe even generating either a patch or new terminfo definition based on the results. I don't know if that would help, though, because you'd have to get someone to run it who's in a position to do something about the problems it might turn up.

@craigbarnes
Copy link

craigbarnes commented Oct 21, 2019

For a key as fundamental as backspace, I can't understand how so many terminfo files can be so wrong, nor why it seems impossible to get them fixed.

Yeah, it's unfortunate that it's ended up that way, but I can understand how it happened. No terminal developer wants to wait until every distro picks up their custom terminfo entry, so they just abuse the xterm one. There are countless other problems caused by this, but fortunately text editors don't typically run into them.

... what would have to happen to get the backspace key definition fixed in some significant portion of the terminfo definitions where it's currently broken?

Probably for every terminal emulator that claims to be xterm to start using the same key code. Or at least the ones people still use.

I can imagine a simple interactive program that asks you to press different keys and it tells you where your terminfo is right/wrong

The xterm/ncurses maintainer has something like that at https://invisible-island.net/ncurses/tack.html.

I don't know if that would help, though, because you'd have to get someone to run it who's in a position to do something about the problems it might turn up.

I'm at the stage where I just assume that anything not part of the basic common subset of features will be completely messed up by at least one terminal. Unfortunately, people actually use such terminals -- so the best thing you can do is stay within that small set of features and just take a pragmatic approach to things like the backspace issue.

@ortonomy
Copy link

@andscoop -- you wonderfuk bastard. this has been annoying the hell out of me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment