Map 'f' to PageUp in vi - vim

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.

Related

Remapping display line navigation in vim

I'm currently setting up a .vimrc and remapping my home-row (ie navigation keys) to wormkan. So for example I have
nnoremap n j
nnoremap j n
which works all well and good, but I'm having issues with figuring out how to use display line navigation with this setup.
Basically, if I want to move down one display line on a qwerty layout this would be achieved with gj - so now I want this to happen with gn.
However, gn currently still maps to visual search and gj is still associated with the display line navigation. I thought that operator-pending mode would help here (ie adding onoremap n j and vice versa) but this doesn't have the desired result. Any ideas on what I have to do to get this functioning?
By reading: :help gn
*gn* *v_gn*
gn Search forward for the last used search pattern, like
with `n`, and
start Visual mode to select the match.
If the cursor is on the match, visually selects it.
If an operator is pending, operates on the match.
E.g., "dgn" deletes the text of the next match.
If Visual mode is active, extends the selection
until the end of the next match.
You realize that gn is the motion command. That's why mapping :onoremap n j did not work as expected. What you need instead is :noremap gj gn.

VimTutor arrow keys v/s hjkl

VimTutor says in its first lesson:
NOTE: The cursor keys should also work. But using hjkl you will be
able to
move around much faster, once you get used to it. Really!
However, I find no difference at all between them. Is there really a difference between using hjkl v/s the arrow keys?
You don't have to move your hand from the touch type position to use hjkl, whereas you do if you use the arrow keys.
The thing is that you start out using the "hjkl" key just as a slightly better way to move your cursor, but the truth is that they are actually motions.
h motions one character left
j motions one line down
k motions one line up
l motions one character right
So for example, with the delete operator d: dl deletes the current character, dh deletes the previous character, dj deletes the current line and a line below it, and dk deletes the current line and a line above it. The same is of course true for the y, c, V, gU and any operator.
Another example are split windows. You can create a couple of windows using Control-w+s and Control-w+v, then use your trusty hjkl to move around between your windows, Control-w+h moves a window left, Control-w+j moves a window down, etcetera.
So it's not just that they are an improvement over the arrow keys. As your understanding of Vim grows, you'll learn that the hjkl keys can be combined with other commands in many ways, and you will be happy you can use them.
In fact, using h j k l keys makes a good practice to avoid to much of right hand movimentation on your keyboard when you have to reach the arrow keys but, there are more efficient ways to navigate through a text file using vim:
f <char> : Go to the next occurrence of a character<char> on the current line. Cursor is placed above the character;
t <char> : Go to the next occurrence of a character<char> on the current line. Cursor is placed right before the character;
T and F: Backward your character search on this line, however T will put the cursor right after the character(or before if you think backwards ;) ), and F will put it above of it.
/ <char> <Enter>: Go the next occurrence of a character independent of line. After you do this one time, pressing n or N goes to the next and previous occurrence.
? <char> <Enter>: Go to the next occurrence of a character independent of line, backwards. n will search next occurrence(backwards) and N will go to the previous.
{ and } : Navigate on paragraphs. Paragraphs are basically blocks divided by an empty line. See :help paragraph for further details.
( and ) : Navigate back and forward on Sentences(:help sentence). A sentence is a group of words ended by . ! or ? followed by a space, tab or endline.
e : next word, place the cursor at the end of the word
w : next word, place the cursor at the start of the word
b : back 1 word, place the cursor at the start of the word
ge : back 1 word, place the cursor at the end of the word
E W B and gE : Same as e w b ge, but with WORD. See :help word for further details.
If you want to start by getting the first good habits of using h j k l or other movements and avoid the arrow keys, pleace the following lines on your .vimrc file inside your home to disable them:
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>

How to jump down X amount of lines, over and over

I'm using 10j to jump down 10 lines, but I want to easily jump 10 lines over and over. I don't want to have to perform the jump with a macro qv10jq#v##..
I wish there was a method for repeating down keys like motion has f then ; to continually jump (, to go back) to the next character(s).
is there anything shorter than my macro?
Instead of 10j, you can run:
:+10
Then you can repeat the last ex-mode command with #:.
Here's repmo.vim - a plugin to do what you want. It maps ; to repeat the last motion command given with a count.
The solution to this gave me the idea to use noremap to map 10j (or any other number) and 10k to my up and down arrows. I don't know if anyone would be interested in something obscure like this but figured I would comment.
added to .vimrc:
noremap <Up> 5k
noremap <Down> 5j
There is no plugin or edit to .vimrc here, but I've found this simple and low tech method works pretty well because it requires no control keys and you can keep both hands stationary while scrolling up or down in any increment of lines or order (e.g. down, down, up):
Let's say you want to move down in increments of 44 lines at a time.
44j (of course)
Now just leave your left index finger above the "4" key and repeat this to continue scrolling down in increments of 44 lines. Although it's 3 keystrokes, you can do this very quickly as long as you stick to numbers like 22, 33, etc.
Now what is nice about this is that you can quickly reverse direction with no hand movement by just hitting "k" instead of "j", e.g.
44j
44j
44j (oops, too far, lets go back now...)
44k
Also, you can start with a higher number like 55 (for speed scrolling) and then drop to 22 or 11 to home in on your target. Unfortunately numbers like 77 don't work as well b/c you want to do the number with your left hand, although you could still do higher numbers like 77 with your left hand, it's just that you have strayed from standard touch typing hand position at that point.
Try ctrl+f to move a whole page down and ctrl+b to move a whole page back. Not necessarily 10 lines though.
Taken from this site: http://www.thegeekstuff.com/2009/03/8-essential-vim-editor-navigation-fundamentals/
I found that mapping 10char jumps to arrow keys work perfectly for navigating. ( ^d, ^u, ^f, & ^b are too big of jumps for my liking). Just paste this into your .vimrc file :)
noremap <Up> 10k
noremap <Down> 10j
noremap <Left> 10h
noremap <Right> 10l
Alternatively, you can map the custom jumps to replacing any of these: ^d, ^u, ^f, & ^b, such as:
map <C-d> 10j
map <C-u> 10k
There's a great solution in this answer:.
I found this to have some excellent info:
:help scroll-cursor
The thread also references:
:help motion.txt
You can configure how many lines to move at a time by for example: 15<C-d>
Then subsequent <C-d> or <C-u> strokes will move by the same amount of lines

"Minimizing" vertical VIM window splits

I use horizontal and vertical window splits in religiously in VIM and up until recently, I enjoyed the comfort of two commands to effectively hide (or minimize) my horizontal splits. I set them up adding the following lines to my .vimrc file:
set winminheight=0
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
Hitting Control-j or Control-k navigates through horizontal splits by going up or down. What I'd like to accomplish is the same thing for vertical splits by showing or hiding the left or right split using Control-Shift-h and Control-Shift-l; h moving to the left, l moving to the right. I have tried the following with little to no success:
set winminwidth=0
map <S-C-L> 500<C-W>h<C-W>_
map <S-C-H> 500<C-W>l<C-W>_
The action would be similar to utilizing Control-w-< and Control-w->, only moving the vertical split completely to the left or write, not just one line at a time.
Any ideas on how to accomplish this? Thanks.
First up, you won't be able to use <S-C- (shift + control) in your code (see below). But you can use the 'mapleader' as your "shift" and then use the <C-h> and <C-l> like you want to. Like this:
set winminwidth=0
nmap <Leader><C-h> <C-W>h500<C-W>>
nmap <Leader><C-l> <C-W>l500<C-W>>
The common leader keys in vim are comma and back-slash:
:let mapleader = ","
But you'll find that this gets annoying to require 3 keystrokes for this, so you might as well just drop the control key stroke. This way (if your leader is comma) you can just press ",h" and ",l" to go to the splits to your left and right:
set winminwidth=0
nmap <Leader>h <C-W>h500<C-W>>
nmap <Leader>l <C-W>l500<C-W>>
" (FTW) :D
...
A guy named Tony Chapman answers why you can't use control + shift:
Vim maps its Ctrl+printable_key
combinations according to ASCII. This
means that "Ctrl+lowercase letter" is
the same as the corresponding
"Ctrl+uppercase letter" and that
Ctrl+<key> (where <key> is a printable
key) is only defined when <key> is in
the range 0x40-0x5F, a lowercase
letter, or a question mark. It also
means that Ctrl-[ is the same as Esc,
Ctrl-M is the same as Enter, Ctrl-I is
the same as Tab.
So yes, Ctrl-s and Ctrl-S (i.e. Ctrl-s
and Ctrl-Shift-s) are the same to
Vim. This is by design and is not
going to change.
Try
set winminwidth=0
map <S-C-L> <C-W>h<C-W>|
map <S-C-H> <C-W>l<C-W>|
This doesn't move a window completely to the left or right (that's <C-W>H and <C-W>L), it just moves the cursor to the left (or right) window and maximizes that window horizontally.
See :help CTRL-W_bar for more.
Crl-w 1 |
will minimize current window in Vim.

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