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>
Related
I have a mapping nnoremap <leader>l i<space><esc> that inserts a space while staying in a normal mode, and the mapping is dot-repeatable. However, the cursor stays where it was and the space "expands" to the right of the cursor.
I want to have a mapping that does the same but moves the cursor along, e.g.
nnoremap <leader>h i<space><esc>l
I love breaking sequences of characters with a space from normal mode and I want to decide at will if the cursor should stay or move (using either h to move the cursor along or l to move the text to the left while leaving the cursor where it is).
None of the tricks with i or a or <C-o> seems to work and this is an expected behavior.
Is there any clever hack to accomplish dot-repeatability using nnoremap only? It should also work if I am at the end of the line.
I can probably re-phrase it like this: I want to have functionality similar to X and x that deletes a single character before or at the cursor, but instead of deleting a character I want to insert a <space> before or at the cursor (not after the cursor as with a). And it should be dot-repeatable and take counts. Preferably a mapping that does not use more than a single line in the .vimrc and does not require any plugins. And I don't want to change the behaviour of <esc> (or i, whichever is responsible for moving the cursor when one leaves the insert mode) that moves a cursor one character to the left.
Edit 1:
Dot-repeatable nnoremap <leader>l i<space><esc> does this:
<leader>l...
AAAAAAAAAA[B]BBBBBBBB
AAAAAAAAAA[ ]BBBBBBBBB
AAAAAAAAAA[ ] BBBBBBBBB
AAAAAAAAAA[ ] BBBBBBBBBB
And I want a dot-repeatable <leader>h that would do this:
<leader>h...
AAAAAAAAAA[B]BBBBBBBB
AAAAAAAAAA [B]BBBBBBBB
AAAAAAAAAA [B]BBBBBBBB
AAAAAAAAAA [B]BBBBBBBBB
Edit 2:
The workaround mentioned in reply by #romainl does the trick:
function! s:insspace(...)
if a:0
" perform operation
execute 'normal' v:count1.'i '."\<esc>".'l'
else
" set up
let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$')
return "g#\<space>"
endif
endfunction
nnoremap <silent><expr> <leader>h <sid>insspace()
Can someone explain how it works? I am a beginner in vim...
Given the string below and assuming the cursor is on the -:
lorem-ipsum
^
There are various easy and repeatable ways to insert a space after the - and leave the cursor on the space:
a <Esc>
s<C-r>" <Esc>
lorem- ipsum
^
The two macros above…
are "dot-repeatable", because there is no motion involved,
leave the cursor on the space because, after an insertion, Vim places the cursor on the last inserted character.
But this is precisely the latter behaviour that prevents us from doing the same in the other direction without a motion either before or after the insertion.
Without motion, the operation is "dot-repeatable" but the cursor is left on the -:
i <Esc>
a <Esc>
s <C-r>"<Esc>
lorem -ipsum
^
With motion, the cursor is left on the space but the operation is not "dot-repeatable":
i <Esc><Left>
<Left>a <Esc>
s <C-r>"<Esc><Left>
lorem -ipsum
^
The workaround is a bit contrived but nifty.
Maybe I did not understand your question well enough, but for me this does the trick:
:nnoremap <leader>h i<space><esc>w
Rationale: Insert space, after <ESC> go one left, so the cursor stays on the space, use w to get to the next word boundary.
Is that what you want? I can now do 5\h and it stays at the current position while inserting spaces to the left.
I want a movement to get from point 1 to points 2 and 3:
FunctionCall(a, b, c, AnotherFunc(a, b))
^ <--- ^ ------------> ^
3 1 2
Ideally it would work with editing operations like d, y etc
Now I count closing parentheses visually and use <n>f), but it's very annoying.
Vim does have facilities to know about blocks, i.e. i(, a(, but I want 'a half' of such a command.
How can I achieve this?
What I usually do is using a suitable commands from various-motions.
For example, for your particular case ]) followed by % will help.
They can be combined with operations like d, y and c.
More details using :help various-motions
You are right that the text objects have knowledge about both sides of the block. If you briefly go into visual mode, your cursor will end up on the end of the range (va(<Esc>) or the beginning (va(o<Esc>) when using the v_o command to switch to the "other" side of the selection.
Unfortunately, because this involves multiple steps, it only works for navigation, but not as an atomic {motion} to be used with d, y, etc. You could write a custom motion for those, though. For example:
onoremap ,) :normal! va(<C-v><Esc><CR>
onoremap ,( :normal! va(o<C-v><Esc><CR>
These then work like motions, e.g. gU,) uppercases the text from the cursor to the closing parenthesis.
The normal mode command % will take you to the next opening paren (and while there, toggling between the opening and closing parentheses). So sadly it would jump to the innermost opening paren instead of place 2 or 3.
Couple that with a F( and you've got something remotely neat that doesn't involve counting. So in place 1, F(% would take you to place 3 and a subsequent % would jump you to place 2.
You can do the following:
nnoremap <silent> ,) :normal! va(<c-v><esc><cr>
nnoremap <silent> ,( :normal! va(o<c-v><esc><cr>
onoremap ,) :<c-u>normal! mava(o`a<cr>
onoremap ,( :<c-u>normal! mava(`a<cr>
Note that for the motion version (onoremap), it consumes the a mark, which can be a problem; so it could be improved.
In vim, if there're many lines in a file, I can use hjkl to navigate, I can just press j to jump to the next line.
However, if I am processing a very long line of text, I use set wrap to wrap it into many lines. It is still one single line, but visually looks like many line to a vim user.
In such a case, if one line of text is wrapped into many lines, it is possible to jump between wrapped fake-lines instead of pressing h or l to jump by characters or pressing w to jump by words?
The motion for that is gj. (Or g<any other direction>.)
gj or *gj* *g<Down>*
g<Down> [count] display lines downward. |exclusive| motion.
Differs from 'j' when lines wrap, and when used with
an operator, because it's not linewise.
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.
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.