30

After a lot of writing in Q10 on Windows, I got used to the typewriter sound it makes every time you press a key. At least for me it feels great to have this sort of sound feedback.

On Linux on the other hand, I love writing it VIM, because of it's editing features. How could I add this functionality to VIM?

Simply said, I want to play a sound every time I press a key in the insert mode.

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327

5 Answers5

51

Alright, this kinda crazy, but it appears to work. First, grab yourself a typewriter sound in aiff format. Then put that typewriter sound in ~/.vim/support/my_typewriter_sound.aiff. Then add the following to your ~/.vimrc.

function! PlaySound()
  silent! exec '!afplay ~/.vim/support/my_typewriter_sound.aiff &'
endfunction
autocmd CursorMovedI * call PlaySound()

Note that the above function calls out to afplay, which I know works on a Mac and needs to be replaced with play on Linux. On Windows, I have no idea.

So you know what's happening above, we're first creating a function called PlaySound that shells out to afplay. We then setup an autocommand that gets fired anytime the cursor moves in insert mode. When it fires, it calls PlaySound.

Trotter
  • 1,270
  • 11
  • 13
  • Awesome, play on Linux works for wav files too, I just had to modify it to dispose output to /dev/null and it works great! thanks – Jakub Arnold Dec 11 '10 at 19:57
  • 5
    Is why I love vim, because "crazy" and dare all other "freaks" are always ready to give them the answer, and "Vim" in turn serves as a backdrop to all this by having these endless possibilities. – SergioAraujo Dec 12 '10 at 10:51
  • 1
    To take it a step further and center everything on the screen a la Q10/WriteRoom, check out VimRoom: http://projects.mikewest.org/vimroom – Wesley Rice Dec 12 '10 at 21:02
  • Is there a special way to hook up the enter key to play a Different sound? – user518450 Apr 28 '14 at 21:32
  • Not that I know of. It looks like the only thing you can do is know that _something_ was typed in insert mode :/ – Trotter Sep 14 '14 at 22:29
  • 1
    On Neovim, you better use `jobstart`. e.g. `call jobstart('aplay ~/.config/nvim/settings/typewriter.wav')` – Gunar Gessner Jul 12 '16 at 00:10
  • @user518450 bind the keypress to a external script, and have the script do all the heavy lifting. – TankorSmash Jan 09 '18 at 23:38
  • the Atari 8-bit home computers of the 1980s featured keyboard click sounds, with each key press. I loved it back then. I wish I could have the same sound today. However, I think I'd want it for while in command mode, rather than insert mode. – user12711 Aug 06 '19 at 17:22
3

The plugin does exist. You can find it here: https://github.com/osyo-manga/vim-sound

However, you need to add sound effects files yourself, which you can find here: http://www.soundjay.com/typewriter-sounds.html

clearlight
  • 12,255
  • 11
  • 57
  • 75
kompowiec
  • 31
  • 2
1

You should try Qweritckle

It's a typewriter sound emulator for Linux.

Aitjcize
  • 187
  • 1
  • 8
1

Fun idea, and thanks for the answer Trotter. However, using your answer I found with my system (Ubuntu 14.04) that Vim went blank at each keypress with the message:

Press ENTER or type command to continue

Based on discussion on the topic of executing commands silently in Vim at https://vi.stackexchange.com/questions/1942/how-to-execute-shell-commands-silently I tried:

function! PlaySound()
  silent! exec '!play ~/.vim/support/my_typewriter_sound.wav &' | :redraw!
endfunction
autocmd CursorMovedI * call PlaySound()

which cleared the blank screen automatically, but I could see the flicker after each keypress and the sounds were only produced after the last keypress was finished, making for quite an unnatural and epileptic experience. In the same question OliverUv gave the important explanation that Vim executes synchronously, meaning it waits to continue until the execution is completed. He suggests vim-dispatch or Neomake for asynchronous execution, but in the end I went with Do for Vim as it is more geared towards executing arbitrary shell commands rather than specific compilation tasks. Do for Vim utilizes built-in Python support in Vim to run each command on a separate thread (asyncronously). I am very pleased with the results using this plugin as follows:

function! PlaySound()
  :DoQuietly play ~/.vim/support/my_typewriter_sound.wav
endfunction
autocmd CursorMovedI * call PlaySound()

There is no flickering of the screen and the individual keypress sounds overlap for an authentic clattering cascade of clicking.

Community
  • 1
  • 1
neudorf
  • 11
  • 3
1

If you're on Solaris, you can DTrace the kernel keyboard driver with this set of scripts: http://www.brendangregg.com/DTrace/typewriter-0.75.tar.gz

BIAJ
  • 11
  • 1