Is it possible to map the leader to Space and then the local leader to Space+l (l is the lowercase L) in Vim 8?
Related
The vim documention on how to set the default leader key ( :h mapleader) provides one simple example:
:let mapleader = ","
I want to map it to backspace and tried a bunch of options, this being the first one:
:let mapleader="<BS>"
But nothing seemed to work.
To make it work I had to use:
:let mapleader="\<BS>"
Another option is to not override the default \ leader key, but map backspace to it, effectively ending up with two simultaneous leader keys:
map <BS> <Leader>
Is there a motion for capturing text in between / or \? I know there is motions for other symbols ---
ci" - Capture 'text' inside "text"
vi( - Visual capture int var inside foo(int var)
di[ - Delete word [word] into []
The only workaround I can find is by using Vim Surround, and using it to change surrounding \ into " (cs\"), and then working from there. However, not only is it that kind of tedious, but the plugin only supports backslashes, not forward.
You could write your own text-object for this pretty easily.
onoremap <silent> i/ :<C-U>normal! T/vt/<CR> " inside /
onoremap <silent> a/ :<C-U>normal! F/vf/<CR> " around /
For it to work with visual mode :
xnoremap <silent> i/ :<C-U>normal! T/vt/<CR> " inside /
xnoremap <silent> a/ :<C-U>normal! F/vf/<CR> " around /
Similarly you could also do for \
Edit: Added info comments.
Here's one way to do it with a mapping:
nnoremap H mal??e+1<Enter>mb//<Enter>y`b`a
This mapping will yank everything between two occurrences of the last search pattern.
Place it in your .vimrc file to make it permanent.
Usage for your question:
search for forward slash: /\/ (note the backslash escape character)
position cursor between two slashes (or on second slash)
press H
Everything between the last / and the next / will get yanked.
Explanation:
nnoremap H mal??e+1<Enter>mb//<Enter>y`b`a
- nnoremap H map H in normal mode, ignoring other mappings
- ma place a mark (named a) at the current cursor location
- l move the cursor one char to the right
- ??e+1<Enter> move to 1 character after END of prev occurrence of last search pattern
- mb place a mark (named b) at the current cursor location
- //<Enter> go to the beginning of the next occurrence of the last search pattern
- y`b yank to the location of the mark named x (note: ` = "back tick")
- `a go to the mark named `a`
Example input:
This is a *funky* string
search for *
position cursor between two asterisks (or on 2nd asterisk)
press H
The word funky will be in the yank buffer.
You can use words as delimeters!
Example input:
<br>
Capture all this text.
<br>
search for <br>
press H in normal mode when between <br>s (or on 2nd <br>)
You can use regexes, too!
Example input:
<p>
Capture this paragraph.
</p>
search for <.\?p> (or <\/\{,1}p> to be more correct)
press H in normal mode when inside the paragraph (or on closing <p> tag)
...
A better approach might be to use a register to remember a delimiter, so you can use this mapping quickly and/or repeatedly. In other words, you could store / or \ or <\?p> in a register and use that to quickly capture text between your stored delimiter.
there is no built-in text object with slash. However there are plugins support customized text-object, like:
targets
A recently proposed Vim patch adds a text object for arbitrary matched pairs:
https://groups.google.com/d/topic/vim_dev/pZxLAAXxk0M/discussion
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.
I notice that I use the goto match bracket key a ton in vim and it is really inconvenient to have it bound to the % key. Since I don't use visual mode that often (and if I do I'm manually transitioned to it when I use my mouse) I would like to map this command to my v key.
As a side question does anyone have their ^ (go to beginning of line) and $ (go to end of line) mapped to different more convenient keys?
I guess the main problem that I'm having is that I use ^ % $ very frequently and I would like them to be mapped to more convenient keys so that I can move around more efficiently.
Thanks
What's the deal? Use
map v %
In this way you can also map ^ and $ keys. For example, I prefer to map ^ to the Home key:
nmap <Home> ^
imap <Home> <Esc>I
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.