How do I switch between command and insert mode in Vim? - vim

I just started using Vim as an IDE. I was using it as a test editor for a while now, so I didn't have to go to command mode very often. But, since I program in Java, I have to go to command mode to make the file, compile/run it... etc.
The problem is: I need a good way to switch between the two modes.
I looked online and it says that the <Esc> key is supposed to do that, but that doesn't work for me (maybe it's not for gVim? I don't know why.)
I have to press CTRLO every time to go to command mode; the escape key works from that mode... it brings me back to insert mode. But is there a better, or easier, way of switching between command mode and insert mode?

Pressing ESC quits from insert mode to normal mode, where you can press : to type in a command.
Press i again to back to insert mode, and you are good to go.
I'm not a Vim guru, so someone else can be more experienced and give you other options.

Looks like your Vim is launched in easy mode. See :help easy.
This happens when Vim is invoked with the -y argument or as evim, or maybe you have a :set insertmode somewhere in your .vimrc configuration. Find the source and disable it; temporarily this can be also done via Ctrl + O :set noim Enter.

This has been mentioned in other questions, but ctrl + [ is an equivalent to ESC on all keyboards.

Using jj
In my case, the .vimrc (or in gVim it is in _vimrc) setting below.
inoremap jj <Esc> """ jj key is <Esc> setting

Coming from emacs I've found that I like ctrl + keys to do stuff, and in vim I've found that both [ctrl + C] and [alt + backspace] will enter Normal mode from insert mode. You might try and see if any of those works out for you.

For me, the problem was that I was in recording mode. To exit from recording mode press q. Then Esc worked as expected for me.

There is also one more solution for that kind of problem, which is rather rare, I think, and you may experience it, if you are using vim on OS X Sierra. Actually, it's a problem with Esc button — not with vim. For example, I wasnt able to exit fullscreen video on youtube using Esc, but I lived with that for a few months until I had experienced the same problem with vim.
I found this solution. If you are lazy enough to follow external link, switching off Siri and killing the process in Activity Monitor helped.

You can use Alt+H,J,K,L to move cursor in insert mode.

Related

Binding F1 to ESC in VIM

Evening,
I got a Lenovo T400 laptop, and the F1 is in a stupid place, and keep hitting it instead of hitting Esc to get change modes with VIM.
I did some googling, and tried adding
map <F1> <Esc>
imap <F1> <Esc>
to my ~/.vimrc file, but to no avail...
If I can't do this with a vimrc, is there a way to change it at the linux/system level?
Using Linux Mint 14 MATE
--
Answer:
The terminal application has it's on keyboard shortcuts defined. Under Edit -> Keyboard Shortcuts you can disable the help. Then the map/imap above will work fine.
You can also edit the keyboard settings in the main system keyboard preferences. Open that up and go to Layouts -> Options and play with the Caps Lock key behaviour. You can disable it, or even bind it to ESC.
It may be that your keyboard isn't producing the keycode that Vim translates to <F1>. In Vim, go into insert mode, and type Ctrl-v. Then press F1. The string that you just inserted is the string that needs to go on the left-hand side of your map command. It might be <x-F1> or something similar. You can do this to insert the key name directly in your .vimrc file.
Remember to restart Vim after editing your .vimrc file.
Regardless of whether you get this working, you might like to swap your Esc and Caps Lock keys, which is quite a popular thing to do among Vim users. You can't do that within Vim, you need to edit your X configuration to achieve it. The Vim tips wiki tells you how.

How to go back (ctrl+z) in vi/vim

In normal text editors [with all due respect to Vim] there is a shortcut Ctrl+Z when you have done something nasty and want to return to the previous version of the text. Like BACK button in Word. I wonder how can you achieve this behaviour in Vim.
You can use the u button to undo the last modification. (And Ctrl+R to redo it).
Read more about it at: http://vim.wikia.com/wiki/Undo_and_Redo
The answer, u, (and many others) is in $ vimtutor.
Just in normal mode press:
u - undo,
Ctrl + r - redo changes which were undone (undo the undos).
Undo and Redo
Here is a trick though. You can map the Ctrl+Z keys.
This can be achieved by editing the .vimrc file. Add the following lines in the '.vimrc` file.
nnoremap <c-z> :u<CR> " Avoid using this**
inoremap <c-z> <c-o>:u<CR>
This may not the a preferred way, but can be used.
** Ctrl+Z is used in Linux to suspend the ongoing program/process.
On a mac you can also use command Z and that will go undo. I'm not sure why, but sometimes it stops, and if your like me and vimtutor is on the bottom of that long list of things you need to learn, than u can just close the window and reopen it and should work fine.
I had the same problem right now and i solved it. You must not need it anymore so I write for others:
if you use gvim on windows, you just add this in your _vimrc:
$VIMRUNTIME/mswin.vim behave mswin
else just use imap...

how to remap <C-;>

I want to use the following code to quickly go from insert mode -> command mode. The problem is it just doesnt seem to be registering my keys. I've tried with control (nothing happens) and i've also tried with command (D) and it just says spellcheck not activated.
" Quick command mode from insert
imap <C-;> <esc>:
How do i go about doing this? is there an easier vim way that I'm not aware of?
Certain Ctrl chords can't be mapped, including Ctrl-;.
This is mentioned in this FAQ, see also the Vim FAQ:
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?
First make sure that the key is passed to Vim. In insert mode, press Ctrl-V
followed by the desired key. You should see the keycode corresponding to
the key. If you do see the keycode, then you can create a mapping for the
key using the following command:
:map <C-V><xxx> <your_command_to_be_mapped>
For more information, read
:help map-keys-fails
:help :map-special-keys
:help key-codes
The tip about trying to print the character using Ctrl-V is good to remember if you run into this problem with another key combo.
I tried it, however it doesn't seem to work, also as pb2q said, it just can't be mapped. But there are other ways to escape using a Ctrl key combination.
For example, you can also escape insert mode with the following key presses:
Ctrl-[
Ctrl-c
On OS X (I think Lion and above), you are able to map alt - ; using this method, alt - ; on OS X outputs ç, which you can map.
But at a MacBook, I prefer to use PCKeyboardHack to map caps lock to esc. Or at Windows, use a tool I've created myself for that or even Ctrl2Cap.
EDIT
oh sorry, I thought you wanted to switch to normal mode, that's why I talked about caps lock mappings.

Return to normal mode without leaving the home row

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.

How to Emulate TextMate's Command-Return Feature in Vim

TextMate has a feature where pressing Command-Return at any point inserts a new line below the current line placing your cursor at the beginning of the new line. This works much (exactly?) like pressing 'o' while in command mode in Vim.
Is there a way to emulate this TextMate feature while in insert mode in Vim? The same question phrased differently: Can I make Cmd-Return in insert mode do what 'o' does in command mode?
Untested, since I don't use a Mac, tried with Ctrl-Return (<C-CR>). Ctrl-O executes a single command in normal mode.
:inoremap <D-CR> <C-O>o
Updating this page to say that Jörn's answer worked for me, too, although it's worth noting that in Snow Leopard or higher, you have to disable the Full Screen menu option, because that's also mapped to Cmd-Enter by default. So here's what I have in my ~/.gvimrc:
macmenu Window.Toggle\ Full\ Screen\ Mode key=<nop>
inoremap <D-CR> <C-O>o

Resources