Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up vim to automatically change my background color depending on whether CAPS LOCK is on or not?

Tags:

vim

keyboard

I'm an avid vim user and have started to write some SQL code recently. I like to write my SQL statements in CAPS and sometimes forget to switch CapsLock 'off' and I then quickly wreak havoc on my code before I realise what's happening.

I have so far not found any way to tell whether the CapsLock key is on other than looking at my keyboard (which requires me to look away from the screen which I consider a big delay).

Ideally I would like vim to automatically change my background colour whenever CapsLock is 'on' but I'd be willing to settle for some other on-screen indicator of CapsLock status as a compromise.

like image 833
snth Avatar asked Nov 14 '08 09:11

snth


4 Answers

The person who suggested mapping lowercase letters to upper case is on the right track but we need to add some more details. The trick is to map SQL keywords to upper case. Then you can type them in lowercase and vim will convert them to uppercase for you. This is done using abbreviations, not mappings. For example, if you create the abbreviation

:iab ATT American Telephone and Telegraph

then every time you type 'ATT' in your code, Vim will automatically translate it to 'American Telephone and Telegraph' as soon as you hit the space bar. (Try it! It's fun!)

So, create a new file and start entering abbreviations for all the SQL keywords.

iab select SELECT
iab like LIKE
iab where WHERE
...

Since you only want these abbreviations to apply when you're editing SQL source, save this file to ~/.vim/after/ftplugin/sql.vim The commands in this file will be executed when Vim detects it has opened a SQL file.

The file type detection only works if you turn it on so make sure your .vimrc has lines like:

set nocompat
filet detect plugin on

Once you get this working you should save time and have less carpal tunnel from holding down the shift key all the time!

like image 158
Steve K Avatar answered Nov 14 '22 03:11

Steve K


The closest you can get is Kent's answer. Vim cannot see CapsLock (or NumLock, or ScrollLock, or any other modifier key by itself) because the status of these keys is not sent across a terminal.

Note that, in theory, gVim could see these modifiers, but in practice it does not. gVim's keyboard handling is superior to vim's though in many other ways.

like image 34
Zathrus Avatar answered Nov 14 '22 05:11

Zathrus


What I did is bound my caps lock key to the "Compose" key, so my capslock key is effectively missing and I just force myself to use SHIFT instead. :)

You might want to try it, it may sound masochistic, but its better in the long run. Some argue the Caps lock is redundant.

like image 45
Kent Fredric Avatar answered Nov 14 '22 04:11

Kent Fredric


The best I can give you is to have vim toggle the background color whenever the CAPSLOCK key is hit in vim.

Vim can't detect the CAPSLOCK key alone. What I can give you is a mapping so you can use some other key (in this example, F3) to act like the CAPSLOCK key for insert mode, and change the background color when all-caps is on. Hopefully this will give you the functionality you need.

Put the following in your ~/.vimrc or in the appropriate ~/.vim/ftplugin/<filetype>.vim :

    " let the case be toggled in normal mode
    map <expr> <F3> ToggleInsertCase()
    " let the case be toggled in insert mode
    imap <expr> <F3> ToggleInsertCase()

    let toUpper = 0
    func! ToggleInsertCase() 
        let g:toUpper = 1 - g:toUpper
        if (g:toUpper == 1)
            highlight Normal ctermbg=Blue " the background color you want when uppercased
            " convert all the letters to uppercase in insert mode
            let i = char2nr('a')
            while i <= char2nr('z')
                let c = nr2char(i)
                exe 'inoremap' c toupper(c)
                let i = i + 1
            endwhile
        else
            highlight Normal ctermbg=Black " the background color you want normally
            " let letters be as normal in insert mode
            let i = char2nr('a')
            while i <= char2nr('z')
                let c = nr2char(i)
                exe 'iunmap' c 
                let i = i + 1
            endwhile
        endif
        " don't insert anything when this function is called in normal mode
        return ""
    endfunc

This borrows some from Tim Pope's capslock.vim.

If you really want to use the CAPSLOCK key for this, depending on your platform, there's a bunch of free apps that let you remap your CAPSLOCK key to something else, so you could set it up so that when you hit CAPSLOCK, vim (and everything else) got F3. Or whatever. This would interfere with other apps getting the CAPSLOCK key though (unless the key-remapper app is clever), so that could be troublesome.

like image 36
3 revs Avatar answered Nov 14 '22 05:11

3 revs