How to Make Vim Play Typewriter Sound When I Write a Letter

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

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.

Typewriter sounds for emacs

First you must establish some way to play sound:

    (defun play-typewriter-sound ()
(let ((data-directory "~/Dowloads/Sounds"))
(play-sound `(sound :file "key1.wav"))))

...doesn't work on Mac OSX Emacs for example since it's not compiled with sound support. There are workarounds though, see for example http://www.emacswiki.org/emacs/ErcSound

  • Then, you can use advice on any Emacsen

    (defadvice self-insert-command (after play-a-sound activate)
    (play-typewriter-sound))

    You could also advise newline-and-indent.

  • On Emacs24 you now have post-self-insert-hook

    (add-hook 'post-self-insert-hook 'play-typewriter-sound)
  • If you don't like defadvice you can use post-command-hook and check the name of this-command there:

    (add-hook 'post-command-hook #'play-typewriter-sound-maybe)

    (defun play-typewriter-sound-maybe ()
    (if (eq this-command 'self-insert-command)
    (play-typewriter-sound)))

Writing whole alphabet in Vim

Using set nrformats+=alpha:

ia<Esc>qqylp<C-a>q24@q

Step by step:

ia<Esc>      " Start with 'a'
qqylp<C-a>q " @q will duplicate the last character and increment it
24@q " Append c..z

gVIM won't let me type a certain character

Finally I've found the solution. So for everyone who happen to use gVIM on a Hungarian system with the auto-pairs plugin, comment out line 196 in auto-pairs.vim so you can type the letter á as you would expect.

I have auto-pairs plugin version 1.1.1 and in my file the line to be changed reads:

execute 'inoremap <buffer> <silent> <M-a> <END>

Traversing text in Insert mode

While it may make sense that you should be able to use the h j k l keys to traverse the editor in insert mode, but that is actually not the way Vim is intended to be used! There are many commands that Vim provides to make editing faster and easier.

The right way is to press Esc, go where you want to do a small correction, fix it, go back and keep editing. It is effective because Vim has much more movements than usual character forward/backward/up/down. After you learn more of them, this will happen to be more productive.

Here are a couple of use-cases:

  • You accidentally typed "accifentally". No problem, the sequence EscFfrdA will correct the mistake and bring you back to where you were editing. The Ff movement will move your cursor backwards to the first encountered "f" character. Compare that with Ctrl+DeldEnd, which does virtually the same in a casual editor, but takes more keystrokes and makes you move your hand out of the alphanumeric area of the keyboard.
  • You accidentally typed "you accidentally typed", but want to correct it to "you intentionally typed". Then Esc2bcw will erase the word you want to fix and bring you to insert mode, so you can immediately retype it. To get back to editing, just press A instead of End, so you don't have to move your hand to reach the End key.
  • You accidentally typed "mouse" instead of "mice". No problem - the good old Ctrl+w will delete the previous word without leaving insert mode. And it happens to be much faster to erase a small word than to fix errors within it. I'm so used to it that I had closed the browser page when I was typing this message...!
  • Repetition count is largely underused. Before making a movement, you can type a number; and the movement will be repeated this number of times. For example, 15h will bring your cursor 15 characters back and 4j will move your cursor 4 lines down. Start using them and you'll get used to it soon. If you made a mistake ten characters back from your cursor, you'll find out that pressing the key 10 times is much slower than the iterative approach to moving the cursor. So you can instead quickly type the keys 12h (as a rough of guess how many characters back that you need to move your cursor), and immediately move forward twice with ll to quickly correct the error.

But, if you still want to do small text traversals without leaving insert mode, follow rson's advice and use Ctrl+O. Taking the first example that I mentioned above, Ctrl+OFf will move you to a previous "f" character and leave you in insert mode.

How to convert visual selection from unicode to the corresponding character in vim command?

Any hint is appreciated.

Here are a whole lot of them…

  1. :help i_ctrl-v is about insert mode and ranges matter in command-line mode so :help command-mode is totally irrelevant.

  2. When they work on text, Ex commands only work on lines, not arbitrary text. This makes ranges like '<,'> irrelevant in this case.

  3. After carefully reading :help i_ctrl-v_digit, linked from :help i_ctrl-v, we can conclude that it is supposed to be used:

    • with a lowercase u,
    • without the +,
    • without worrying about the case of the value.

    So both of these should be correct:

    <C-v>u00a9
    <C-v>u00A9
  4. But your input is U+00A9 so, even if you somehow manage to "capture" that U+00A9, you won't be able to use it as-is: it must be sanitized first. I would go with a substitution but, depending on how you want to use that value in the end, there are probably dozens of methods:

    substitute('U+00A9', '\(\a\)+\(.*\)', '\L\1\2', '')

    Explanation:

    • \(\a\) captures an alphabetic character.
    • + matches a literal +.
    • \(.*\) captures the rest.
    • \L lowercases everything that comes after it.
    • \1\2 reuses the two capture groups above.
  5. From there, we can imagine a substitution-based method. Assuming "And I want to generate the following next to it" means that you want to obtain:

    U+00A9©

    you could do:

    v<motion>
    y
    :call feedkeys("'>a\<C-v>" . substitute(@", '\(\a\)+\(.*\)', '\L\1\2', '') . "\<Esc>")<CR>

    Explanation:

    • v<motion> visually selects the text covered by <motion>.
    • y yanks it to the "unnamed register" @".
    • :help feedkeys() is used as low-level way to send a complex series of characters to Vim's input queue. It allows us to build the macro programatically before executing it.
    • '> moves the cursor to the end of the visual selection.
    • a starts insert mode after the cursor.
    • <C-v> + the output of the substitution inserts the appropriate character.

    That snippet begs for being turned into a mapping, though.

Enclosing in parentheses with Vim

Surround.vim should do the trick. If you want to repeat that with '.', see here.

How to start using vim with a non-english keyboard

You can remap all vim defaults bindings with the map function.
For further information, you can read this tutorial

How can I get vim to render Arabic text correctly?

Unicode bidirectionality (bidi) offers the user the ability to view
both right-to-left as well as left-to-right text properly at the same time
within the same window. Vim, due to simplicity, does not offer bidi. Instead, it provides commands to make switching between the two directions easier.

" switch to right-left mode (arabic)
:set rl
" switch to left-right mode (english)
:set norl

However, if your terminal supports bidi, you can tell Vim that the terminal is in charge of text direction with the termbidi setting:

set termbidi

If termbidi is set, changing the arabic setting will now only affect your keyboard mapping and the delcombine, which allows you to delete overlapping characters (fathah + letter) individually.



Related Topics



Leave a reply



Submit