Eliminating lag when remapping 'd' key in VIM - vim

I use a Dvorak keyboard layout, and so I've made a few adjustments to the default VIM mappings-- one change I've made is to remap the right-side home row keys to
noremap d h
noremap h j
noremap t k
noremap n l
So that movement keys are conveniently positioned, as they would be for a QWERTY user. h, t, and n do their job fine and are very responsive. d, however, lags for a moment before moving left the way its supposed to. I think this is because there are key-sequence commands that start with d (like dd for delete line), so VIM is waiting to receive the second stroke in the sequence before executing the command for just a single 'd'. I've remapped dd:
noremap dd hh
But this isn't doing the trick. Yet commands for things like d3w or d$ (text objects after d) aren't working (after remapping d), so they couldn't be causing the issue. Anyone know how to get rid of the lag? BTW, even after disabling all plugins I have the same issues. I also have no other mappings with d in my vimrc.

AFAIK, d, c and their "operator pending" friends are not really mappings. This means that you can't :unmap d. The delay you experience can't really be avoided without side effects, I believe.
You might want to play with :h timeout and :h timeoutlen.
For what it's worth, the alternative layouts/Vim story is… complicated. And not settled at all.

Related

How to set default registers in vim?

I want to set the default register of d, x, and c to the black hole and use some mapping for use other register, like <leader>d "*d
Of course I can remap like
noremap d "_d
but then I'd have to remap dd to "_dd and when I select something and press d vim waits a second because it could be dd, or just d. Other thing I could do is: noremap _ "_ but I dont want to tell vim that I want to use the black hole register each time I want to really delete something.
That behaviour is hardcoded and you can't do anything to change it except creating lots of brittle mappings.
You will be better off embracing the versatility of register rather than fighting it.

vim - Ignore timoutlen for certain key-mapping macros

I have made two simple nmaps in Vim that moves the cursor 10 times right and left by pressing Alt-z and Alt-Z
nmap <ESC>z 10l
nmap <ESC>Z 10h
It works perfect.
Now I would like to add two similar nmaps that delete 10 times
nmap d<ESC>z 10x
nmap d<ESC>Z 10X
This works almost fine, and follows the vim logic of operation followed by cursor move.
Unfortunately it times out if I don't press Altz or AltZ quickly after d.
I find this odd, because I have not mapped anything to just d. And if I press w or W after d, I can wait as long as I want.
I know there's a special thing about built-in maps such ad dw and dW.
To my question: Is there a way I can get my own nmaps to accept long delays between the key-presses?
You're right that by default, 'timeoutlen' (default 1 s) applies to mapped keys. I find this useful, but if you really want your started mappings to wait indefinitely, you have to define the map on the first key only, then query and handle the remaining keys yourself.
function! MapOrDefault()
let c = nr2char(getchar())
return c == "\<A-z>" ? '10x' : 'd' . c
endfunction
nnoremap <expr> d MapOrDefault()
Note: Though it's technically equivalent, I'd recommend to prefer the Vim key-notation (<A-z>) instead of the cryptic <Esc>z.
Note: You should use :noremap; it makes the mapping immune to remapping and recursion.

Vim and assigning function to a key

Hi!
For about 3 last weeks, I have been using Vim. I must say, I love the navigation keys on the home row!
A week ago I switched to Dvorak, and the new location of the movement keys, simply doesn't let me use Vim. It's terrble.
So, I wonder, is there a way to keep movement keys in Dvorak, in the same location as in qwerty?
I don't want to remap keys, as that would be super-confusing. One way I can think of is to assign the function i.e go up one line from key k to t.
So I could have my movement keys in dvorak(d h t n) layed out as in qwerty (h j k l)
Is it possible? How can I do that?
I don't want to remap keys, as that would be super-confusing
You probably want to remap keys, but just in Vim. However the fine people over at vim.wikia.com doesn't think this is such a good idea.
One alternative, as mentioned in the linked article, is to use dvorak in insert mode, and qwerty-position of binds in normal mode, i.e. w = ", i = c etc.
set
langmap='q,\,w,.e,pr,yt,fy,gu,ci,ro,lp,/[,=],aa,os,ed,uf,ig,dh,hj,tk,nl,s\;,-',\;z,qx,jc,kv,xb,bn,mm,w\,,v.,z/,[-,]=,\"Q,E,PR,YT,FY,GU,CI,RO,LP,?{,+},AA,OS,ED,UF,IG,DH,HJ,TK,NL,S:,_\",:Z,QX,JC,KV,XB,BN,MM,W<,V>,Z?
-
One way I can think of is to assign the function i.e "go up one line" from key k to t, so I could have my movement keys in dvorak(d h t n) layed out as in qwerty (h j k l)
Is it possible? How can I do that?
Remember that doing such a thing could break other keys, I can't really tell as it's hard without actually trying/comparing each key. You could however remap all of those keys to custom positions if you'd want to using nnoremap etc.
A basic remapping would be just to remap the home row like this.
nnoremap d <Left>
nnoremap h <Down>
nnoremap t <Up>
nnoremap n <Right>
All in all I don't think it's such a good idea to combine two keyboard layouts, so just remapping home row is probably the safest, and then learn that e etc. has custom positions compared to qwerty.
These problems is why I never bothered to switch to Dvorak og Colemak, because I'd have to relearn all those binds that I have in my muscle memory, for a new vim user it might not be such a big issue though, so you're lucky that way!

Map 'f' to PageUp in vi

I can map 'jj' to
imap jj <Esc>
and I can even map letters to tab navigation
map tj :tabprevious<CR>
map tk :tabnext<CR>
But I can't map g to page up (even though spacebar acts as page down)
map <Space> <PageDown>
map g <PageUp>
According to this "When you try to map multiple key sequences, you won't be able to start them with lower or upper case letters ("Too dangerous to map that"), but the punctuation and control characters are fair game." Can anyone confirm this?
If so, how does one assign a function to an unmapped key like 'g'
This isn't answering your question, but I thought it may be helpful to the problem you are having with your RSI. It maps the spacebar to toggle between fast and slow move modes. Normally pressing j or k will scroll down one line. Pressing space will turn on fast move mode, where pressing j or k will scroll down/up 10 lines. Press space again to go back to normal. This will only work in vim, not just plain vi (most "vi" programs are just symlinks to vim anyway though).
It works in both normal and visual edit modes.
To use it, put this code somewhere in your ~/.vimrc file:
map <Space> :call ToggleFastMoveMode()<CR>
vmap <Space> :call ToggleFastMoveMode()<CR>gv
let g:fastMoveMode = 0
function! ToggleFastMoveMode()
let g:fastMoveMode = 1 - g:fastMoveMode
if (g:fastMoveMode == 0)
noremap j j
vnoremap j j
noremap k k
vnoremap k k
else
noremap j 10j
vnoremap j 10j
noremap k 10k
vnoremap k 10k
endif
endfunction
(Edit - original answer suggested native Ctrl-f and Ctrl-b, but answer was updated as the goal here is to avoid using Ctrl and Shift)
A few points to add
Leaving the issue of choosing the right character to you, assuming we chose X for now.
I can think of two reasons why map X <PageUp> isn't working for you.
Your version of vi may not support PageUp/PageDown. If this is the issue then try instead to map to vi's page jumping (B for back, accompanied by for forward) eg. map X <C-b>.
Another other option is that it doesn't work 'as expected'. In vi PageUp/PageDown act on the 'viewport' not the cursor. So if you'r looking at the top of the file, but the cursor is not at the top or won't do anything. PageDown won't 'work' if your cursor is two lines from the bottom either.
To address this you could combine the 'move viewport up' <C-b> and the 'move cursor to the top of viewport' H eg. map X <C-b>H (The opposite being map X <C-f>L). Or specifying the number of lines to jump yourself map X 30k (Op. map X 30j).
Then the issue of choosing the right character to overwrite. Vi has a lot of native commands, so many in fact that only a handful of characters don't do something natively.
So if your goal is to avoid RSI, then of course overwrite something. But make sure to overwrite something that isn't too useful for you personally.
Natively:
f searches for a given symbol on the line you are currnetly on (can be very useful, but not critical I guess)
g on it's own does nothing, but gg moves cursor to top of file. Choosing g may cause issus as vim (not the original vi) will interpret two quick keypresses as go to top of file instead of do two PageUp's.

Vim Dvorak keybindings (rebindings :)

Although I played with it before, I'm finally starting to use Dvorak (Simplified) regularly. I've been in a steady relationship with Vim for several years now, and I'm trying to figure out the best way to remap the key bindings to suit my newfound Dvorak skills.
How do you remap Vim's key bindings to best work with Dvorak?
Explanations encouraged!
I use one of the more common recommended keybindings:
Dvorak it!
no d h
no h j
no t k
no n l
no s :
no S :
no j d
no l n
no L N
Added benefits
no - $
no _ ^
no N <C-w><C-w>
no T <C-w><C-r>
no H 8<Down>
no T 8<Up>
no D <C-w><C-r>
Movement keys stay in the same location. Other changes:
Delete 'd' -> Junk 'j'
Next 'n' -> Look 'l'
Previous 'N' -> Look Back 'L'
There were also some changes for familiarity, 's'/'S' can be used to access command mode (the old location of the :, which still works).
Added Benefits
End of line '$' -also- '-'
Beginning of line '^' -also- '_'
Move up 8 'T'
Move down 8 'H'
Next window <C-w><C-w> -also- 'N'
Swap windows <C-w><C-r> -also- 'D'
-Adam
I don't find that I need to remap the keys for Dvorak -- I very quickly got used to using the default keybindings when I switched layouts.
As a bonus, it means that I don't have to remember two different key combinations when I switch between Dvorak and Qwerty. The difference in keyboard layout is enough that I'm not expecting keys to be in the same location.
A little late, but I use the following:
" dvorak remap
noremap h h
noremap t j
noremap n k
noremap s l
noremap l n
noremap L N
" easy access to beginning and end of line
noremap - $
noremap _ ^
This basically does the following:
left-down-up-right are all under the default finger positions on the home row (i.e. not moved over by one as in the default QWERTY Vim mappings)
l/L is used for next/previous search result
use -/_ to reach the end/beginning of a line
This seems to work for me...
I simply use standard qwerty for commands and dvorak for insert mode
Here is how to set it up
My rebindings:
noremap h h
noremap t j
noremap n k
noremap s l
noremap j t
noremap l n
noremap k s
noremap J T
noremap L N
noremap K S
noremap T J
noremap N L
noremap S K
Notes:
In qwert, vi has to use 'h', because vi doesn't want to use ';' a non-letter. But
in dvroak, we have 's', so why not take this advantage?
vi uses Caps for relative actions. This is a good design philosophy. So I try to
conform this.
Meanings:
n (Next) -> l (Left) -- "What's left?" resembles "What's next?"
s (Substitute) -> k (Kill then insert)
t (jump Till) -> j (Jump till)
N, S, T are similar.
J (Join lines) -> T (make lines Together)
K (Keyword) -> S (Subject)
L[count] (Line count) -> N (line Number)
B.T.W. L itself goes to the last line, and N is the last letter of fin.
(Thanks for tenzu to point out this.)
P.S. I have used these rebindings for a while. Now I does not use it in vim. I just use the default ones.
Vim ships with an extensive Dvorak script, but unfortunately it’s not directly source-able, since the file includes a few lines of instructions and another script that undoes its effects. To read it, issue the following command:
:e $VIMRUNTIME/macros/dvorak
You can use this to have Vim use Dvorak only in insert mode:
:set keymap=dvorak
This way all of the commands are still in QWERTY, but everything you type will be in Dvorak.
Caveats: Well, almost everything. Insert mode, search mode, and replace mode will all be dvorak, but ex commands will not. This means that you don't have to relearn :wq, but you will need type :%s/foo/bar/gc in QWERTY.
This won't help if you only want to move certain commands, but I found that in my head, "move forward one word" was bound to "move left ring finger up," rather than "ask the typing department where the letter 'w' is and then press it," which made this method much easier for me.

Resources