-1

I believe this article should help me find the control sequence for backward/forward word deletion.

My understanding is that the control sequence would start with \u001b[ since that is "the control sequence introducer" (I got this from this other answer), but don't really know what the full string should be.

Context (as per the other SO question): My goal is to issue the control sequence for deleting words in the VSCode terminal from a keyboard shortcut (keybinding)

2
  • 1
    Would using readline or libedit, or zle not be an option in your case rather than reinvent the wheel? Nov 27, 2019 at 13:38
  • Thanks @StéphaneChazelas - I just found a way of doing this from VSCode that I believe piggybacks on those libraries (I'm in zsh so I suppose zle). There is a commandID in VSCode called workbench.action.terminal.deleteWordRight that I can map to a keybinding. In other words, I don't need to use workbench.action.terminal.sendSequence to send an XTerm Control sequence for this particular operation. Thanks to both StéphaneChazelas and stolenmoment for your help! Nov 27, 2019 at 16:49

2 Answers 2

2

There's no such thing, you can see the ctlseq document doesn't have any single mention of the word word. There's also the question of what background color to use to erase and whether to shift what's to the right of the word to the left or not. You may have to consider words that are wrapped at the right margin of the terminal.

Some of your options are:

  • send as many BS characters as there are cells in the word you want to erase, followed by as many SPC (beware it uses the current background colour) followed by as many BS characters again (to move the cursor back again):

    $ printf 'foo bar\b\b\b   \b\b\b+\n'
    foo +
    

    that one should work on virtually all terminals.

  • same but use tput cub x (where x is the number of cells) to move the cursor back by that amount instead of using a sequence of BS. And you can use tput ech x to erase those cells (with the current background color or not depending on the terminal)

    $ printf 'foo bar'; tput cub 3; tput ech 3; printf '+\n'
    foo +
    
  • instead of tput ech x, you can use tput el to erase to the end of line, or tput dch x to delete x cells (and shift what's after that to the left).

(tput here is the shell interface to the terminfo database, the idea being to avoid hard coding escape sequences, use the equivalent for your language).

Looking at the backward-kill-word widgets of some shells (using script for instance to log their output), I see readline uses BS for motion and dch to delete, while zsh uses a combination of BS and cub and rewrite. tcsh seems to be doing some absolute column positioning (hba) and use dch to delete, ksh93 sends a CR and rewrites the whole line.

1
  • 1
    It can get even more complex than that. A smart use of termcap/terminfo can adjust between control sequences according to how far the cursor has to move backwards. I have not investigated how smart the various shell line editors are in this regard, but I've certainly implemented several full-screen programs this way.
    – JdeBP
    Nov 27, 2019 at 16:36
2

I've never seen such a sequence. If you think about it, deleting a word is quite complex, more complex than other operations, and I don't think that any other operation cares about what's on the screen in any way. For instance, what terminates a word? '_'? '-'? '.'?

4
  • 1
    Hmm, see this. Not sure if one can still build a control sequence for it though. Nov 26, 2019 at 19:32
  • That's what you type to erase a word, not what gets sent to edit the screen. The software in the application prints backspace-space-backspace (in simple cases, like at the end of the line) or something more complicated in the middle of a line, like reprinting the rest of the line a few places to the left of its original location. Nov 26, 2019 at 22:36
  • Thanks but when I bind a keystroke sequence (e.g. Alt+D) to backward-kill-word in .inputrc, backward-kill-word is not what I type, but a command right? Also, from what I understand, that command is available in many shells, e.g. bash and zsh, i.e. it has a clear definition of what terminates a word (answering your question of what terminates a word) and that definition is what I am referring to) Nov 26, 2019 at 23:43
  • 1
    Yes, your keystroke is interpreted as a command by the software that's reading your typing, but that's not a command that the terminal emulator that's rendering the pixels on your screen would understand. The OP is clearly asking about sending an escape sequence to the screen to erase a word. Nov 27, 2019 at 21:14

You must log in to answer this question.

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