It seems like this article by Steve Losh has been making the rounds, and in short, it led me to remap Caps Lock to F19, and at the system level, to remap F19 to Shift-Command-Ctrl-Alt, thus giving me an entire namespace for my own keyboard shortcuts.
The problem is I enjoyed using Caps Lock as my escape key in Vim.
I tried :map <M-C-D-S> <esc>, but upon sourcing, hitting caps lock doesn't do anything. I'm guessing it's because Vim expects an actual key to be pressed with the modifiers.
My question is: is it possible to have Vim read Shift-Cmd-Ctrl-Alt as Escape?
No, you can't map four modifiers. Not even three, Not even two. And, well… not even one.
"Modifiers" are called "modifiers" for a reason: they are used with normal keys to "modify" their meaning. The Shift, Alt, Cmd and Ctrl keys don't do anything on their own and Vim rightfully does nothing but wait for further keypresses when you hit them.
Additionnaly, Vim's input mechanism is a bit "old school", most probably to keep it consistent over the multiple platforms it may run on. It won't register more than one modifier with a normal key. That means that, if the terminal doesn't eat them up (which it does), you can only do <C-…> or <S-…>. The Alt key is not really a working solution because of how many terminal emulators deal with it and the Cmd key is Mac-only and only works in MacVim anyway.
At that point, if you are ready to mash four keys together to emulate the function of only one key (which in itself is rather silly), you might as well simply use <C-c> which, AFAIK, works everywhere. Your left hand will thank you.
My original question suffers as an XY Problem.
You want to do X, and you think Y is the best way of doing so. Instead of asking about X, you ask about Y.
I wanted to Vim to read Caps Lock as Esc (while still maintaining the other crazy system-level modifications), so I asked about functionality that Vim doesn't have.
Turns out, while the answers provided above were great, on point, and informative, I just don't like being told 'no.' And I was searching for answers in the wrong problem space.
One of the applications discussed in the OP's link, KeyRemap4MacBook, is highly extensible. The documentation leaves much to be desired, but I was able to piece together a solution in line with my original question. This diverges sharply from the original topic of "map stuff in vim," and for that I apologize, but I hope to leave this in posterity for the one poor soul that needs this.
Simply put, the previous article recommends adding this to KR4MB's 'private.xml' file:
<item>
<name>Remap Caps Lock to Hyper</name>
<appendix>OS X doesn't have a Hyper. This maps Left Control to Control + Shift + Option + Command.</appendix>
<identifier>caps_lock_to_hyper</identifier>
<autogen>
--KeyToKey--
KeyCode::F19,
KeyCode::COMMAND_L,
ModifierFlag::OPTION_L | ModifierFlag::SHIFT_L | ModifierFlag::CONTROL_L
</autogen>
</item>
(modified slightly for Caps Lock)
This can instead be changed to:
<item>
<name>Caps Lock to Hyper OR Escape</name>
<appendix>Caps Lock should be remapped to F19 (80) in PCKBH</appendix>
<appendix>This remaps F19 to "Hyper" aka Cmd-Shift-Ctrl-Alt...</appendix>
<appendix>...but, when pressed alone, F19 sends only escape</appendix>
<identifier>remap.hyper2hyper_escape</identifier>
<autogen>
--KeyOverlaidModifier--
KeyCode::F19,
KeyCode::COMMAND_L,
ModifierFlag::OPTION_L | ModifierFlag::SHIFT_L | ModifierFlag::CONTROL_L,
KeyCode::ESCAPE
</autogen>
</item>
This tells the system that whenever Caps Lock is pressed alone, it's actually Esc, but if it's pressed in conjunction with any other key, it's F19.
Related
I am enjoying hardmode and have definitely seen improvement. However the one item I am dealing with is selecting, moving, copying only two lines at the time. Current line +1 or -1.
Before hardmode the way I would select three lines of code in visual mode would be with the motion:
V2j
Since HardMode disables the "j" key what would be a good substitute to such move?
About HardMode:
Hard Mode is a plugin which disables the arrow keys, the hjkl keys,
the page up/down keys, and a handful of other keys which allow one to
rely on character-wise navigation. The philosophy behind Hard Mode is
that you'll never master Vim's advanced motion and search
functionality if you can fall back on the anti-pattern of fumbling
around your code with the arrow keys.
https://github.com/wikitopian/hardmode
For me, HardMode is all about changing your mindset about how you move in vim. Really getting comfortable with text objects, searching etc.
In this case, you can just use 3V (3 <S-v>) to select 3 lines.
I'd urge you to learn some ex commands while you work in HardMode. Like use
:8,15d " To delete lines from line no. 8 through 15
:8,15co . "To copy range of lines 8 through 15 to current cursor position.
You can also use
:.+3 " To move down
:.-3 " To move up 3 lines
but then you'll be totally missing the point. Just use HardMode for what it's meant to be. Which is learn a few things in a constrained situation.
I think I see the point of hardmode now, and you got an answer for how to select multiple lines in visual mode. That answer is correct, but maybe you don't need to select at all? You mentioned copying, or moving, a few lines. For that, try using counts with your yank/delete commands. Example, to copy 5 lines:
5yy
To delete 3 lines:
3dd
I'm trying to make a transition to emacs (using evil mode/vim keybindings) and I'm having a hard time feeling more efficient/productive than if I just used the mouse. Here is an example of a situation where I find myself really slow:
for i in range(self.allData.shape[0]):
self.correctSequence = self.displayNumbers(i, self.allData)
self.userSequence = self.numberEntry()
self.allData.set_value(i, 'userSequence', ''.join(self.userSequence))
if len(self.correctSequence) != len(self.userSequence):
self.allData.set_value(i, 'correct', 0)
else:
if list(reversed(self.correctSequence)) == self.userSequence:
self.allData.set_value(i, 'correct', 1)
else:
self.allData.set_value(i, 'correct', 0)
It would be very common for me to have to change the first 4 instances of self.allData to something else (self.testData, for example), leaving the last 2 untouched.
Normally this wouldnt be too bad with a mouse. For example, I could replace the first allData with testData, copy it, use the mouse to the next 3 occurences and just hit CTRL-V for each one. Or better yet, just use multiple cursors in sublime/atom and replace all 4 in one go
I use spacesmacs in emacs with vim keybindings. So, in emacs I find myself having to do something like the following:
SPC-SPC a (avy jump to words beginning with a)
cw testData
Repeat those 2 steps once for each word I want to replace
This seems really inefficient and I'm wondering: am I just using an inefficient method? Is there a faster way to do what I want?
It seems that even if I managed to complete those steps really fast (4 times), theres still A LOT more typing one would have to do, and I fail to see how this would be faster than just reaching for the mouse. Yes, one could make the argument that I'm losing time by constantly reaching for the mouse, but in my mind I'm saving typing time by reaching for the mouse because I can just hit CTRL-V a few times to achieve what I want. Where exactly are the vim speed gains in a situation like this?
If you just want to replace, you can use query-replace, and replace the word one by one.
You can use replace-string too, but remember to limit replacement to part of the buffer, activate the region around that part.
Anyway, these commands could prevent you from finding the word by your eyes, moving cursor by mouse and moving your hand back to keybaord. And they could avoid probable overlook too. At least I don't want to leave my hands from the keyboard when typing. :)
I'm not sure how "vim-like" Spacemacs is, but you could do it like this in Vim:
/all<CR>
cgntest<Esc>
.
.
.
or:
/all<CR>
cetestData<Esc>
n.
n.
n.
or:
:,$s/allD/testD/gc<CR>
Maybe one of these methods works in Spacemacs too?
In addition to the usual (and generally the best) answer, query-replace (see #songyuanyao's answer), you can use the secondary selection to advantage to selectively paste the same thing at various places. Unlike the region, the secondary selection is unrelated to the cursor position (aka point), so it is always available regardless of where the cursor is.
And unlike query-replacing, you can paste it on demand, instead of having to answer for each matching occurrence. If you use delete-selection mode then just select some text to replace and paste the secondary selection to replace it.
You can set and paste the secondary selection using the mouse - see Secondary Selection on the Emacs Wiki, and see the Emacs manual, node Secondary Selection.
If you use library second-sel.el then you can use the secondary selection from the keyboard as well, and get a lot more use out of it.
I'm just getting into using Vim. The way I learned to type I keep my fingers anchored over the "j,k,l,;" keys instead of the "h,j,k,l" keys. I'd like to change the key board short cuts to essentially move over the left,down,up,right functionality from the "h,j,k,l" keys to the "j,k,l,;" keys. Can anyone help me out?
The recent book Practical Vim by Drew Neil has an interesting discussion on this topic:
You’re wasting keystrokes if you press the h key more
than two times in a row. When it comes to moving horizontally, you can get
around quicker using word-wise or character search motions.
...
I’m pleased that the ; key rests comfortably beneath my little finger.
[Given that ; is the "find next" key for the f search command.]
I've been using Vim for many years and have never really thought about it. A friend of mine asked why that is, noting that in our culture, left would usually map to up while right would map to down, making the Vim keys backwards.
I understand that they are on the home row, meaning that you do not have to move your fingers anywhere to hit them, but that's a different point altogether.
Why were these keys given their present purposes? Is there some documentation on the decision as well?
The answer is in the Wikipedia entry for vi. Bill Joy, who wrote the visual mode of ex - which ended up being Vim's precursor vi - used a Lear Siegler ADM-3A terminal on which the HJKL keys mapped to left, down, up, right - and it’s been that way ever since.
Here's the keyboard layout:
A couple of other points of note on the ADM-3A layout:
Left of the Q: the escape key - somewhat handier than where it is on keyboards today, hence a good choice for switching between normal and insert modes.
Top right: the 'Home' key doubles as the tilde (~), which subsequently became shorthand for a Unix user's home directory.
vimtutor provides the mnemonic that 'J' looks vaguely like a downward-pointing arrow, though that may or may not be the original reason why it was chosen.
This is a total guess, but: The Ctrl+J character is the "line feed" character, which on a traditional TTY moves down one line, providing a mnemonic. K was right next to it on a Qwerty keyboard, under the second most commonly used finger on the right hand when in the home position.
Simply to supplement all the answers, here is the photo of the ADM-3A keyboard (the exact keyboard on which Vim has been created).
P.S: I wish they had used W, A, S, D
From a user experience perspective, after you open a file, you always move down first and moving down is usually a more frequent operation. Since from left to right is the natural direction for most of us, it make sense to associate the task you perform first or more frequently with the key on the left. You can try to switch the 2 keys and try to tell which way is better. For me, "J" for down is more natural and comfortable.
It explains it if you run vimtutor in the terminal. It says:
The h key is at the left and moves left.
The l key is at the right and moves right.
The j key looks like a down arrow.
I always thought it was because on the Dvorak layout, j and k are also next to each other. There are not many keys that have this property on both Dvorak and A/QWERTY/Z.
Bash uses readline, and readline can delete the word to the right of the cursor with "kill-word".
The problem is in recognizing the keypress of control-delete. When I press them in bash, "5~" is output on the screen. I could just bind for this, but it would mean that one day I need to type "5~", and it deletes a word to the right instead! So I'd much rather discover the correct control sequence.
I have googled, and quite a few resources discuss the "delete" key, but none that I've found discuss "control-delete" key. I've experimented with many variations, but nothing works.
The worst is the hours I've spent on this tedious, mindless grind, when it really should be a non-problem.
EDIT: It's through X, so maybe there's a solution with xev and xmodmap
On my machine, pressing Ctrl-V, Ctrl-Delete outputs this:
^[[3;5~
The ^[ escape character can be replaced with \e, so you can then use bind like this for bash (in your ~/.bashrc for example):
bind '"\e[3;5~":kill-word'
Or, you can add the following to your ~/.inputrc so Ctrl-Delete does kill-word in any program that uses readline:
"\e[3;5~": kill-word
This will bind only the Ctrl-Delete key, you don't have to worry about what will happen if you need to type 5~.
What you see is not the whole truth. It's probably <ESC>5~ or something like that. Try Ctrl-V Ctrl-Delete. The Ctrl-V means "do not interpret the next thing".
So binding <ESC>5~ that should be pretty safe.
Alt+D deletes one word to the right of the cursor
Ctrl+W deletes one word to the left of the cursor
(both are based on Emacs, I believe)
If you type ^Q^V (that's Control-Q followed by Control-V, releasing the Control key between them is fine), and then press Control-Delete, do you get the output you mentioned? I just tried it, and at least using Putty I don't get a response at all. Perhaps the behvior is different on an actual Linux console, though.
For other keys, readline prints a longer sequence, often including a special "command sequence introduction" character, which is hard to type by mistake. Try it, and see if you get a longer sequence with the ^Q^V command (which is, btw, called quoted-insert).
For example, if I press ^Q^V and then Delete (without control held down), readline prints ^[[3~. This tells me I can bind stuff to the Delete key by saying \e[[3~. It seems highely likely that the CSI character is present for you, but you're not seeing it since you're not asking readline to quote the input properly.
Ctrl-W deletes words.
Ctrl-u deletes lines.
They're based on Emacs (M-w and M-u).