Return to normal mode without leaving the home row - vim

I've been trying out vim, and the emphasis on speed and accessibility makes sense for the veteran programmer. I've previously used emacs, and currently use a combination of nano, and stuff like gedit or geany.
I'm confused by the need to constantly switch modes, and that returning from insert mode to normal mode requires leaving the home row to press Esc. I've read that previously, this key was Tab on some systems where vim was first used, which makes a lot more sense, but not on current systems where tab is expected to participate in smart indenting when coding.
Returning to normal mode is an operation that you need to perform all the time. Is there some alternative key mapping that makes this quicker, or mappings that I simply don't know about that do this?

Ctrl+C also return you to normal mode

You can also use ^[. If you've mapped your caps lock key to control (highly recommended), this becomes a rather easy keystroke.

I personally use the Capslock key as the second Esc key, so it is very comfortable. You can do it by adding the following lines to your .xmodmap file:
remove Lock = Caps_Lock
keysym Caps_Lock = Escape

You can map whatever you like to escape:
imap kj <Esc>
I haven't yet run across a situation where I need to type kj next to each other, besides when I talk about this mapping. I've seen other people use jk or jj, but kj works best for me.

I personally use ii to get out of insert mode while staying on the home row, but it is only a matter of personal preference:
inoremap ii <Esc>
i to go in insert and ii to go out. This is easy for your finger to remember. (I tend to still use <Esc> though)

I personally use Left Control as escape and Caps Lock as Left Control. It is not vim mapping, but some xmodmap+setxkbmap magic:
! ~/.Xmodmap
! Control_R
keycode 37 = Escape
and ctrl:nocaps in X keyboard options. You may have different keycode though.

Use Alt/Meta In a Terminal
If you use Vim in a terminal, simply press alt/meta+normal_mode_key. Most terminals send an escape character followed by the normal_mode_key that you pressed, removing the need to press escape yourself.
The terminals konsole and gnome terminal send the escape by default when you press alt/meta+normal_mode_key. For Xterm you can ctrl+click and select the option "Meta sends escape" or "Alt sends escape".
Thus in insert mode pressing alt+h alt+j alt+k alt+l all take you to normal mode and move in the expected direction. You can hold down alt when moving even while in normal mode since the additional ESC that is sent does no harm.
The advantage of using this scheme is that you can use the alt/meta+key combination with any normal mode key, e.g.
Alt+o opens a new line below the one you are currently editing,
Alt+A appends to the end of the current line,
Alt+p pastes at the current insert location.
Alt+k moves up
Thus using vim via a terminal gives you these short cut powers on any stock standard system without the need to edit each systems vim mappings.

Related

Insert mode default keys in vim

The following items are useful to me in editing text, and I was wondering if vim had something for this built out of the box (though I didn't see it on the https://vimhelp.org/index.txt.html#index.txt page), or I had to create mappings for it:
Forward-delete a character. This is X in normal mode.
Forward-delete all text to the right of the cursor on the line. This is the inverse of ctrl-u.
Are either of these mappings available? And if not, are there 'standard' mappings for this that are common (for example, how it might be done in another unix program).
Note that this is the keyboard I have -- there is only one delete key (which acts like a normal backspace key) and there is no backspace key:
Note: for forward-delete, I am currently mapping ctrl-d as:
"Ctrl-d to forward-delete when in insert or command mode
noremap! <C-d> <Delete>
However, this interferes with the tab in insert mode (which I don't use) and the help-options in command mode (which I do use!) so I may have to modify this later, or hopefully someone suggests a better solution.
though I didn't see it on the https://vimhelp.org/index.txt.html#index.txt page
If you can't find it in the documentation, then it doesn't exist.
You can use fn+delete for "Forward-delete a character".
"Forward-delete all text to the right of the cursor on the line" is ctrl+k in MacOS, but Vim has its own use for that combo, :help i_ctrl-k so it is up to you to create a mapping for it.
Something like:
inoremap <key> <C-o>ld$

Mapping Alt-j and Alt-k in vim [duplicate]

I'm running Vim on a gnome terminal. But the alt key mappings are not working. For example:
:imap <A-i> <Esc>
It works fine in GVim. But when I run the same command with Vim in the gnome terminal it does nothing.
I'm using Windows 7, The problem is with the terminal, right?
The problem
There are two ways for a terminal emulator to send an Alt key (usually called a Meta key as actual terminals didn't have Alt). It can either send 8 bit characters and set the high bit when Alt is used, or it can use escape sequences, sending Alt-a as <Esc>a. Vim expects to see the 8 bit encoding rather than the escape sequence.
Some terminal emulators such as xterm can be set to use either mode, but Gnome terminal doesn't offer any such setting. To be honest in these days of Unicode editing, the 8-bit encoding is not such a good idea anyway. But escape sequences are not problem free either; they offer no way of distinguishing between <Esc>j meaning Alt-j vs pressing Esc followed by j.
In earlier terminal use, typing Escj was another way to send a Meta on a keyboard without a Meta key, but this doesn't fit well with vi's use of Esc to leave insert mode.
The solution
It is possible to work around this by configuring vim to map the escape sequences to their Alt combinations.
Add this to your .vimrc:
let c='a'
while c <= 'z'
exec "set <A-".c.">=\e".c
exec "imap \e".c." <A-".c.">"
let c = nr2char(1+char2nr(c))
endw
set timeout ttimeoutlen=50
Alt-letter will now be recognised by vi in a terminal as well as by gvim. The timeout settings are used to work around the ambiguity with escape sequences. Esc and j sent within 50ms will be mapped to <A-j>, greater than 50ms will count as separate keys. That should be enough time to distinguish between Meta encoding and hitting two keys.
If you don't like having timout set, which times out for other mapped key sequences (after a second by default), then you can use ttimeout instead. ttimeout applies only to key codes and not other mappings.
set ttimeout ttimeoutlen=50
For Gnome-terminal, use the following instead:
imap ^[i <Esc>
^[i should be typed by pressing Ctrl-v Alt-i
Attention: You need to yank and put in Vim when you want to copy it elsewhere. If you just copy the mapping in an editor like gedit, the mapping will probably be broken.
EDIT here is an example which makes Alt-k add an empty line above the cursor, and Alt-j add an empty line after the current line.
" Alt-j/k to add a blank line
if has('gui_running')
" the following two lines do not work in vim, but work in Gvim
nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>
else
" these two work in vim
" shrtcut with alt key: press Ctrl-v then Alt-k
" ATTENTION: the following two lines should not be
" edited under other editors like gedit. ^[k and ^[j will be broken!
nnoremap ^[k :set paste<CR>m`O<Esc>``:set nopaste<CR>
nnoremap ^[j :set paste<CR>m`o<Esc>``:set nopaste<CR>
endif
Try
<m-i>
Or, if typing alti inserts a character (like in my case, it inserts a carret: ˆ) just map to that character:
:inoremap ˆ <esc>
Be careful, because this one wouldn't work (at least in my system, MacOS 10.6). The caret waits for a letter, because it's not exactly a caret, it is a circumflex.
It may be that the shortcuts are actually from the Gnome Desktop. Try looking at the Gnome Keyboard Shortcuts tool (System menu, Preferences, Keyboard Shortcuts), which lets you view and modify the shortcuts defined on Gnome Desktop. If the key combination is assigned to a function on Gnome Desktop, then remove it and then that key combo should filter down to Vim properly.
Or you may be right that it is a problem of the terminal. Not all terminals support all key combos. Your problem may be the one described in the Vim help docs at :h map-alt-keys. The docs provide a workaround, but not a very good one.
The same thing happens to me. I searched on Google with "gnome terminal alt key", and found that someone asked almost the same question: "How to disable the alt-hotkey behavior on gnome terminal?" in the first link found. (The second link is just this question)
So, maybe you can try that:
Edit > Keyboard Shortcuts, and uncheck "Enable menu access keys"
Take a look at section 1.10 of http://vimdoc.sourceforge.net/htmldoc/map.html. It seems to indicate that gnome-terminal automatically escapes the Alt modifier, so that it doesn't switch the byte sent in the way that Vim is expecting. The document seems to indicate that there isn't really a way around this except for using a different terminal (such as xterm).
This is certainly frustrating because so far as I can tell Linux machines are also incapable of using the D (Mac's Command or Linux's Super) bindings, so at least as far as the terminal goes, we are limited to Shift and Ctrl modifiers, which is frustrating if we want to ensure that we can use all the commands we use in Gvim on terminal Vim (at least without switching terminals, towards which I'm perhaps overly stubborn - gnome-terminal is just so much prettier). I've been looking for a way around this but have been unable to find anything.

Press pageup/pagedown after press esc when use vim in tmux

When I Press pageup/pagedown after press esc when use vim in tmux, it will lower-case/upper-case the three characters after current cursor, is there any way to fix it?
thanks.
This is a bit of a non-answer, but don't!
Using arrow keys and special keys like Home, End, etc. is generally discouraged. In addition to the speed loss of moving your hands off the home row, there are often side effects like the ones you're seeing now.
Instead, use Ctrlf and Ctrlb to go forward and backward a page (same effect as Page Down/PageUp).
Alternatively, you can use Ctrlu and Ctrld to go up and down with smaller jumps.
Specifically, the page-up key will send a sequence of characters, not a single character, to the input stream, which must then be interpeted. That sequence might be something like "^[[5~" (escape [ 5 ~), which means vim sees the ~ and performs its case-changing behavior.
You may remap case-changing key or just disable it:
if &term =~ '^screen'
" disable case-changing command to fix esc-esc-pagedown misbehavior.
map ~ <Nop>
endif

How to remap keyboard keys to avoid RSI in VIM

I hope this does not come across as an attempt to spark conversation purely for the sake of doing so.
I use vim a great deal (5-10 hours a day) and I noticed my left wrist is the first to start aching. The standard keyboard layout (see image below) is almost guaranteed to give you arthritis.
Currently, I have remapped
Ctrl to Shift
Shift to Caps Lock
Caps Lock to Ctrl
This helps with common commands such as Ctrlf or Shift2 (#), but I still have to twist my wrist to get at those combinations. Is there anyway to remap the keys so as to avoid this twisting on the left wrist (maybe use the Tab key)? I just find it odd that for a text editor designed specifically for programmers, it would make heavy use of these out of the way keys. I have considered remapping to the old keyboard layout (see below image), but I don't want to learn that if it is going to have the same fundamental problems as my current remapping.
Note: I am not a doctor, just another typist that suffered from wrist pain.
In my experience, just mapping Caps to Ctrl (or swapping the two if you must have a Caps) and using Ctrl+[ instead of Esc make a big difference. Ctrl+[ does not require an additional mapping in vim as it is equivalent to Esc. If you can teach yourself this small change you reduce your movement by a great deal when you consider how often you use Esc in vim.
The Caps to Ctrl mapping can't be done inside vim and will have to be made system-wide, however, having Ctrl in the more convenient location will also help in other applications that use Ctrl combinations. There are different methods of remapping keys depending on which OS you're using. A few common options are:
xmodmap (GNU/Linux)
KeyRemap4Macbook (OSX)
KeyTweak (Windows)
While this will probably improve your situation you really should consider seeing a doctor and/or purchasing a more ergonomically designed keyboard if your pain persists. I own an older version of the Kinesis Advantage that I picked up used on Ebay for about $100 (USD) and I couldn't be happier. The keys can be programmed directly on the keyboard so no software is needed to swap keys or create your own keyboard macros for frequently used key strokes.
Edit: I see in your post you also mention combinations like Ctrl+f and Shift+2 requiring you to twist your wrist to press. It sounds to me like you're only using the Ctrl and Shift keys on the left side of your keyboard. You should really be using both sets of these keys, using whichever one is on the opposite hand of the key you're modifying. That would negate this wrist movement.
Use caps as ctrl systemwide. (How often do you cut/copy/paste?)
This also makes ctrl[ work in vim.
Alongside I use the vim 'arpeggio' plugin.
Arpeggio lets you define vim bindings that are activated when two keys are simultanously pressed.
jk pressed serves me as ESC.
Also the 'surround.vim' plugin might be interesting for you.
Eliminated all my wrist pains.
post scriptum:
Use 'vundle'!
Its the best vim plugin manager by far, and you will wonder how you could ever do without, especially if you use a customized vim on several machines. Don't be scared if you have trouble setting it up initially, take your time.
I switched to a Kinesis Advantage keyboard, and remapped Caps Lock to Esc. This effectively removed any wrist pain I felt as a vim user.Once again not a doctor, but looking at how my hands move, I don't see my wrist twisting even when I hit Shift.
You could probably do some great remappings to the thumb keys to further minimise tension, but to be honest I've never felt the need.
Not a $0 solution, I'm afraid, but it was very effective for me.
I mapped both Escape (single tap) and Ctrl (hold) to CapsLock:
xmodmap -e "remove Lock = Caps_Lock"
xmodmap -e "remove Control = Control_L"
xmodmap -e "keysym Control_L = Caps_Lock"
xmodmap -e "keysym Caps_Lock = Control_L"
xmodmap -e "add Control = Control_L"
xcape -t 1000 -e "Control_L=Escape" # Fire nothing if 1000ms hold time out
Probably I'm messing things up here, but xmodmap isn't the best option to modify mappings anyway, so I will change that to pure xkb config.
I have also swapped : with ; and ` with ~, since I use : and ~ more often:
xmodmap -e "keycode 47 = colon semicolon"
xmodmap -e "keycode 49 = asciitilde grave"
I also use Compose Key to be able to type ūmlauts et c.
In vim, the most useful change I have ever made is mapping of space to leader. All kinds of leader-key combinations will make your life easier.
I couldn't get used to H and L for _ and $, since it doesn't seem to work with surround.vim and some other tricks people advise.
Just don't use arrows!
I'd make this part a comment to Randy's answer on Esc if I could, but it demands a little more space. There's one important use case where you can't just remap Windows keys: Windows 7 without admin rights. It's common enough in an enterprise environment; at the very least, it describes me and that keyboard in the pic. keymapper's repo has decent info on this issue:
Important note for Windows 7 Users
Windows 7 does not support per-user key mappings. Until the next release of Key Mapper, you will have to manually switch to 'Boot' mappings from the Mappings menu, reboot to change the mappings, and you will need to have Administrative access to your computer to set key mappings that will work in Windows 7.
The same is true for any Microsoft tools, KeyTweak, SharpKey, etc.
So you'll have to use something like AutoHotkey. I personally prefer mapping Caps Lock directly to Esc for Vim, and I've had issues just mapping it to Ctrl:
#IfWinActive ahk_class Vim
*CapsLock::Esc
#IfWinActive
Another alternative I like is inoremap jj <Esc>. Unless you plan to write about a hajj or something it works pretty well.
More to your point
In any case, have you considered doing the above for easy escaping and then remapping some of the hard-to-reach insert mode commands to Alt (hit it with your thumb) and then using mappings to your leader?
let mapleader=","
nnoremap <leader>f <C-F>
I would suggest remapping
Caps lock to left Control
Left Control to Escape
Escape to Caps lock
. If in linux first can be done by using setxkbmap -option '...,ctrl:nocaps' or changing XkbOptions in /etc/X11/xorg.conf. Second requires using of xmodmap. After some X server update I failed to get the third working, previously used xmodmap as well.
Using dvorak layout (maybe even programming) that is optimized for touch typing and mappings like ,s->()<Left> might also help.
Note: I am not a doctor as well. I am touch typist, but unlike #Randy Morris I never suffered from the wrist pain, just moved keys to the most convenient locations.
I always press the left ctrl key by curling up my pinky and pressing with the first knuckle. I find this requires less hand movement.
Update
I don't find Shift uncomfortable, so I can't help you with
that. However Ctrl does bother me, and even with my
knuckle technique I prefer to avoid it where possible. So for
insert mode (and command-line mode) I use a leading ;
instead of Ctrl. E.g.
:map! ;w <C-W>
:map! ;a <C-A>
:map! ;r <C-R>
:ino ;t <C-T>
:ino ;d <C-D>
You get the idea.
In normal mode I use v and V instead of Ctrl+F and
Ctrl+B (I only use visual blockwise mode), though
I imagine most people would prefer to map different keys,
such as <Space> and - for paging (- isn't really
needed). Instead of F you could search forwards with f,
and then hit ,.
I've never really seen much point in Ctrl+D or Ctrl+U.
However I do find z., z<Enter>, and z- useful for
scrolling the line with the cursor to the middle, top, and
bottom of the window respectively. H, M and L are also
useful for moving the cursor around the screen. But in
general I prefer to just use / with 'incsearch' set for
navigating about.
There's no right or wrong way to do this, these are just my personal preferences, but I hope it gives you one or
two ideas. Try experimenting and see what works for you.
I've developed a vim module to do some simple remappings for exactly this issue:
https://github.com/vim-scripts/vim.ergonomic
The biggest things it does is make it easier to type bracket sets. It also makes it much easier to get into command mode (jj->ESC)
It runs on the principle that you hardly ever used the J key while editing, but it is right there in the home row... so let's abuse it!
from the documentation:
Remapping commonly used commands
key combo mapping purpose
; : removes the need to hit Shift to use commands
jj <ESC> can exit edit mode quickly and easily
Jj j can type 'j' when you need to
JJ J can type 'J' when you need to
Making it easier to insert various kinds of braces:
key combo mapping purpose
JU ()<left> easier to create parenthesis
JI []<left> easier to create square brackets
JM {}<left> eaiser to create curly braces
J< <><left> easier to create carrot braces
Make it easier to move small distances (especially useful with braces commands):
key combo mapping purpose
JL <left> move left in insert mode
JH <right> move right in insert mode
Removing any need for enter, backspace or delete:
key combo mapping purpose
JB <esc>ldbi delete the previous word
JW <esc>dwi delete the next word
JO <esc>oi insert line and go to it

Alt key shortcuts not working on gnome terminal with Vim

I'm running Vim on a gnome terminal. But the alt key mappings are not working. For example:
:imap <A-i> <Esc>
It works fine in GVim. But when I run the same command with Vim in the gnome terminal it does nothing.
I'm using Windows 7, The problem is with the terminal, right?
The problem
There are two ways for a terminal emulator to send an Alt key (usually called a Meta key as actual terminals didn't have Alt). It can either send 8 bit characters and set the high bit when Alt is used, or it can use escape sequences, sending Alt-a as <Esc>a. Vim expects to see the 8 bit encoding rather than the escape sequence.
Some terminal emulators such as xterm can be set to use either mode, but Gnome terminal doesn't offer any such setting. To be honest in these days of Unicode editing, the 8-bit encoding is not such a good idea anyway. But escape sequences are not problem free either; they offer no way of distinguishing between <Esc>j meaning Alt-j vs pressing Esc followed by j.
In earlier terminal use, typing Escj was another way to send a Meta on a keyboard without a Meta key, but this doesn't fit well with vi's use of Esc to leave insert mode.
The solution
It is possible to work around this by configuring vim to map the escape sequences to their Alt combinations.
Add this to your .vimrc:
let c='a'
while c <= 'z'
exec "set <A-".c.">=\e".c
exec "imap \e".c." <A-".c.">"
let c = nr2char(1+char2nr(c))
endw
set timeout ttimeoutlen=50
Alt-letter will now be recognised by vi in a terminal as well as by gvim. The timeout settings are used to work around the ambiguity with escape sequences. Esc and j sent within 50ms will be mapped to <A-j>, greater than 50ms will count as separate keys. That should be enough time to distinguish between Meta encoding and hitting two keys.
If you don't like having timout set, which times out for other mapped key sequences (after a second by default), then you can use ttimeout instead. ttimeout applies only to key codes and not other mappings.
set ttimeout ttimeoutlen=50
For Gnome-terminal, use the following instead:
imap ^[i <Esc>
^[i should be typed by pressing Ctrl-v Alt-i
Attention: You need to yank and put in Vim when you want to copy it elsewhere. If you just copy the mapping in an editor like gedit, the mapping will probably be broken.
EDIT here is an example which makes Alt-k add an empty line above the cursor, and Alt-j add an empty line after the current line.
" Alt-j/k to add a blank line
if has('gui_running')
" the following two lines do not work in vim, but work in Gvim
nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>
else
" these two work in vim
" shrtcut with alt key: press Ctrl-v then Alt-k
" ATTENTION: the following two lines should not be
" edited under other editors like gedit. ^[k and ^[j will be broken!
nnoremap ^[k :set paste<CR>m`O<Esc>``:set nopaste<CR>
nnoremap ^[j :set paste<CR>m`o<Esc>``:set nopaste<CR>
endif
Try
<m-i>
Or, if typing alti inserts a character (like in my case, it inserts a carret: ˆ) just map to that character:
:inoremap ˆ <esc>
Be careful, because this one wouldn't work (at least in my system, MacOS 10.6). The caret waits for a letter, because it's not exactly a caret, it is a circumflex.
It may be that the shortcuts are actually from the Gnome Desktop. Try looking at the Gnome Keyboard Shortcuts tool (System menu, Preferences, Keyboard Shortcuts), which lets you view and modify the shortcuts defined on Gnome Desktop. If the key combination is assigned to a function on Gnome Desktop, then remove it and then that key combo should filter down to Vim properly.
Or you may be right that it is a problem of the terminal. Not all terminals support all key combos. Your problem may be the one described in the Vim help docs at :h map-alt-keys. The docs provide a workaround, but not a very good one.
The same thing happens to me. I searched on Google with "gnome terminal alt key", and found that someone asked almost the same question: "How to disable the alt-hotkey behavior on gnome terminal?" in the first link found. (The second link is just this question)
So, maybe you can try that:
Edit > Keyboard Shortcuts, and uncheck "Enable menu access keys"
Take a look at section 1.10 of http://vimdoc.sourceforge.net/htmldoc/map.html. It seems to indicate that gnome-terminal automatically escapes the Alt modifier, so that it doesn't switch the byte sent in the way that Vim is expecting. The document seems to indicate that there isn't really a way around this except for using a different terminal (such as xterm).
This is certainly frustrating because so far as I can tell Linux machines are also incapable of using the D (Mac's Command or Linux's Super) bindings, so at least as far as the terminal goes, we are limited to Shift and Ctrl modifiers, which is frustrating if we want to ensure that we can use all the commands we use in Gvim on terminal Vim (at least without switching terminals, towards which I'm perhaps overly stubborn - gnome-terminal is just so much prettier). I've been looking for a way around this but have been unable to find anything.

Resources