Linux 写信时如何让VIM播放打字机的声音?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4418364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:16:41  来源:igfitidea点击:

How can I make VIM play typewriter sound when I write a letter?

linuxvimvim-plugin

提问by Jakub Arnold

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.

Windows上的Q10 中进行了大量写作之后,我已经习惯了每次按一个键时它发出的打字机声音。至少对我来说,拥有这种声音反馈感觉很棒。

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?

另一方面,在 Linux 上,我喜欢用 VIM 编写它,因为它具有编辑功能。我如何将这个功能添加到 VIM?

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

简单地说,我想在插入模式下每次按下一个键时播放一个声音。

采纳答案by Trotter

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.

好吧,这有点疯狂,但它似乎有效。首先,给自己一个 aiff 格式的打字机声音。然后把打字机的声音放进去~/.vim/support/my_typewriter_sound.aiff。然后将以下内容添加到您的~/.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 playon Linux. On Windows, I have no idea.

请注意,上面的函数调用了afplay,我知道它在 Mac 上工作,play在 Linux 上需要替换。在 Windows 上,我不知道。

So you know what's happening above, we're first creating a function called PlaySoundthat 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.

所以你知道上面发生了什么,我们首先创建一个名为PlaySoundshell的函数afplay。然后我们设置了一个自动命令,它会在光标在插入模式下移动时被触发。当它触发时,它会调用PlaySound

回答by BIAJ

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

如果您使用的是 Solaris,则可以使用以下脚本集对内核键盘驱动程序进行 DTrace:http: //www.brendangregg.com/DTrace/typewriter-0.75.tar.gz

回答by Aitjcize

You should try Qweritckle

你应该试试Qweritckle

It's a typewriter sound emulator for Linux.

它是 Linux 的打字机声音模拟器。

回答by kompowiec

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

该插件确实存在。你可以在这里找到它: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

但是,您需要自己添加音效文件,您可以在这里找到:http: //www.soundjay.com/typewriter-sounds.html

回答by neudorf

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:

有趣的想法,感谢Trotter 的回答。但是,使用您的答案,我在我的系统 (Ubuntu 14.04) 中发现 Vim 在每次按键时都变为空白并显示以下消息:

Press ENTER or type command to continue

按 ENTER 或输入命令继续

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

基于在https://vi.stackexchange.com/questions/1942/how-to-execute-shell-commands-silently上关于在 Vim 中静默执行命令的主题的讨论,我尝试了:

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 Vimas 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:

它会自动清除空白屏幕,但我可以看到每次按键后闪烁,并且声音是在最后一次按键完成后才产生的,这是一种非常不自然和癫痫的体验。在同一个问题中,OliverUv 给出了重要的解释,即 Vim 是同步执行的,这意味着它会等待直到执行完成才继续。他建议使用 vim-dispatch 或 Neomake 进行异步执行,但最后我选择了Do for Vim,因为它更适合执行任意 shell 命令而不是特定的编译任务。Do for Vim 利用 Vim 中的内置 Python 支持在单独的线程(异步)上运行每个命令。我对使用此插件的结果非常满意,如下所示:

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.

屏幕没有闪烁,各个按键声音重叠,发出真实的咔嗒咔嗒声。