How to have ESC also cancel CAPS if CAPS is enabled?

3

1

Is there any way to configure the keyboard so that pressing ESC will also cancel the Capslock feature, if Capslock is enabled? I specifically use Kubuntu Linux with ESC and Caps keys switched (VIM user) but my coding SQL statements code practices dictate the use of CAPS for SQL keywords. I would prefer a general Linux / X solution but a KDE-specific solution is alright as well.

Thank you!

dotancohen

Posted 2012-10-26T12:20:54.347

Reputation: 9 798

Answers

3

An X-Windows specific solution would be to use xbindkeys

  1. Install xbindkeys from your distro's repository.
  2. Create a config file .xbindkeysrc in your home directory
  3. Run xbindkeys -k from the terminal. This should open a GUI window. With that window focused press the key you wish to bind.
  4. Copy the code snippet relating to that key from the terminal
  5. Paste it into your .xbindkeysrc
  6. Replace the "command schema" with a command to run when you press that key.

In your case you would need to create a script to turn caps lock off and then generate a real escape key event (using crikey to send XTest signals perhaps)

You can check whether your caps-lock is on using xset q | grep "LED mask"

jsj

Posted 2012-10-26T12:20:54.347

Reputation: 422

Thank you trideceth, that is a great answer. I don't see from casual googling that there exist either an if flow control in xbindkeys, nor a "Disable Capslock" code that would have no effect if Capslock is not set. I'll keep looking and update if I find either of those. – dotancohen – 2012-10-26T13:41:29.097

@dotancohen you would have to delegate to a bash script to do that – jsj – 2012-10-26T13:42:14.037

Thanks, I see your edit that even mentions how to do that. I'm fiddling with it now, thank you! – dotancohen – 2012-10-26T13:49:27.590

1

What about a dumb, short-sighted and brutal Vim-specific solution?

:s/\vfalse|null|true|access|add|as|asc|begin|by|check|cluster|column|compress|connect|current|cursor|decimal|default|desc|else|elsif|end|exception|exclusive|file|for|from|function|group|having|identified|if|immediate|increment|index|initial|into|is|level|loop|maxextents|mode|modify|nocompress|nowait|of|offline|on|online|start|successful|synonym|table|then|to|trigger|uid|unique|user|validate|values|view|whenever|where|with|option|order|pctfree|privileges|procedure|public|resource|return|row|rowlabel|rownum|rows|session|share|size|smallint|type|using|not|and|or|in|any|some|all|between|exists|like|escape|union|intersect|minus|prior|distinct|sysdate|out|alter|analyze|audit|comment|commit|create|delete|drop|execute|explain|grant|insert|lock|noaudit|rename|revoke|rollback|savepoint|select|set|truncate|update|boolean|char|character|date|float|integer|long|mlslabel|number|raw|rowid|varchar|varchar2|varray/\U\0/g

romainl

Posted 2012-10-26T12:20:54.347

Reputation: 19 227

That might work! It's not finding anything as posted, I'll google how to get the regex to work and I'll post back. Thanks, Romain! – dotancohen – 2012-10-26T13:35:07.950

Forgot the \v at the beginning of the search pattern. It works, now. But it is very brutal and very dumb. xbindkeys sounds better. – romainl – 2012-10-26T14:01:14.117

Yes, it is too brutal and I've had to trim it down quite a bit. I'm now trying to get it to only match whole words but VIM regex is a bit different than PCRE. However, it is a great tool to have for the common one-liner SQL keywords such as INSERT, INTO, TABLE, WHERE, SELECT, DELETE, FROM, LIMIT, ORDER, BY, ASC, DESC, SET, UPDATE. As soon as I've got this whole-word issue sorted out I'm mapping it to leader-s. Thanks! – dotancohen – 2012-10-26T14:20:54.367

The following form is perfect: :s/\v<where>|<select>|<delete>|<from>/\U\0/g. Thanks! As is obvious, I will select trideceth's solution as it does answer the question and it much broader is scope. But I do love this trick and I see this becoming a common tool in my box. Thank you Romain! – dotancohen – 2012-10-26T14:36:23.653

De nada. Regular expressions are at the heart of Vim. It's sometimes a bit tricky to come up with the right pattern but once you have it it's so simple to create a mapping for later reuse… I ♥ Vim. – romainl – 2012-10-26T14:43:41.037

Changing that to a mapping was an exercise in experimentation. Here is the format, for those will will follow in my footsteps: noremap <leader>s <Esc>:s/\v<select>\|<from>/\U\0/g. Only the pipes need backslashes, in contrast to all the info that I was finding on Google which suggested that the \, < , and > characters should all be escaped. I wonder if that is due to my environment or other settings, though I don't see anything relevant. – dotancohen – 2012-10-26T14:46:20.817

If I understand your comment, you are refering to using <foo> vs \<foo\>. Am I right? That's because \v changes the meaning of a few characters in patterns. It's called "very magic" and you can read about it in :h \v. It's often used to reduce the number of backslashes. – romainl – 2012-10-26T14:53:24.930

Great, thanks. I had intended to google that later once I get some other issues taken care of. – dotancohen – 2012-10-26T15:15:40.750