mcbetz
mcbetz

Reputation: 2399

How to disable backspace and delete keys in Vim?

I'd like to use Vim for distraction-free writing instead of PyRoom/TextRoom. This post, which mentions VimRoom, could already show me how to get most settings.

I only miss how to enable TextRoom style flow mode:

How can I disable backspace and delete keys (not shortcuts like d$)?

Upvotes: 11

Views: 2677

Answers (2)

Kaz
Kaz

Reputation: 58637

You can flow without vim, by making a shell scripts that reads from the tty in raw mode and does not allow most control characters, including backspace and DEL, and echoes things twice: once directly to the terminal, and once to standard output so you can redirect your work to a file.

#!/bin/sh

saved_tty=$(stty -g < /dev/tty)

bail()
{
  stty $saved_tty < /dev/tty
  exit $?
}

trap bail EXIT INT

stty raw -echo < /dev/tty

while true ; do
  ch=$(dd bs=1 count=1 < /dev/tty 2> /dev/null)
  code=$(printf "%d" "'$ch")
  if [ $code -eq 4 ] ; then
    printf "\r\n" > /dev/tty
    printf "\n"
    break
  elif [ $code -eq 13 -o $code -eq 10 ] ; then
    printf "\r\n" > /dev/tty
    printf "\n"
  elif [ $code -ge 32 -a $code -lt 127 ] ; then
    printf "%s" "$ch" > /dev/tty
    printf "%s" "$ch"
  fi
done

Save this as flow, chmod a+x flow and just:

$ flow > file.txt
Backspace and del not allowed
here just type and type.
Then hit Ctrl-D when you're done; no Enter
required.

$ flow
ffoorrggoott  ttoo  rreeddiirreecctt!!

If you require a blank screen, clear first or add screen clearing to the script.

Upvotes: -1

Ingo Karkat
Ingo Karkat

Reputation: 172758

You can disable individual keys by mapping them to the special <Nop> target:

:inoremap <BS> <Nop>
:inoremap <Del> <Nop>

(Assuming you only want them disabled in insert mode.)

Upvotes: 21

Related Questions