Switch automatically between normal and insert mode in vim - vim

I am new to vim and have installed oh-my-vim and learnt these keybindings by googling:
Jump forward by word - w
Jump forward by word - b
Jump to end of sentence - A
Close current file without exiting - bd
Undo is u
Execute shell command with ! (bang)
of these typing Shift + a in Normal mode allows me to jump to end of sentence and also goes into insert mode soon as I start typing after it. I tried out Shift + w and Shift + b and
it also shows similar behaviour of going to insert mode as soon as I start typing after I use that keybinding.
What are the equivalents for these keybindings where I am not using a letter?
Jump to start of sentence - 0
Redo is Ctrl + R
Jump to previous line - ``
Hope I am clear in describing. Thanks!

I'm also a beginner and I am getting used to Vim recently.
Well, regarding your question, to switch from Normal to Insert, you could press:
i to insert (the cursor stays, effectively inserting to the left)
a to append (the cursor jumps after the character you were on)
I to insert from the beginning of the line. This is equivalent to pressing 0 in Normal mode, and then pressing i to enter Insert mode.
A to append to the end of the line. This is equivalent to pressing $ in Normal mode to get to the end of the line, and then pressing a to append in Insert Mode.
For Insert to Normal:
press <Esc> to get to Normal mode.
press <Ctrl-[> to get to Normal mode.
press <Ctrl-O> to temporarily enter Normal mode for one command. Upon execution it will automatically return to Insert mode.
Personally, to shift between Normal mode, I use <Ctrl-[> for consecutive commands in Normal mode and <Ctrl-O> if it's a one time deal. <Esc> is too far for my pinky :)

It's not entirely clear what you are asking. Shift+W and Shift+B do not automatically enter insert mode, instead they go back and forth on WORDS (vs words).
If you want to enter insert mode, just hit i. You could hit wi to insert after a word.
You mention 0, which goes to the beginning of a line. You can hit I (shift+I) to insert at the beginning of a line. Note that this inserts after the initial whitespace on the line, it is the same as hitting ^i. If you want to insert at the very beginning of the line, you'll need to use 0i.

Related

How to switch modes when operating on multiple selected lines

I have created a line selection in Vim by Shift+V and followed by jjjj.
I can do something against all lines separately like :normal ^i//, which moves the cursor to the beginning of each line and enter insert mode, then insert two /.
Is it possible to switch back to normal mode after this?
The example in the question is just to demonstrate the problem and I only want to discuss the Vim usage skills.
I tried :normal ^i//<Esc>A// in the hope of adding two / to the end of each line, but it didn't work.
Is this possible?
The :normal command does not interpret special characters. In your last try, all the chars after i would be interpreted as normal text then inserted at the beginning of each selected line: //<Esc>A//.
The Esc character is a special char (actually the ASCII code 27), so you have to ask Vim to insert this char in a different way (because hitting Esc would escape the command line).
In order to do this (either in Command mode or Insert mode), press Ctrl + V then the wanted key, e.g. Esc. This will insert the real <esc> character in you flow, then perform the desired behaviour.
To summarize:
Press Shift + V followed by jjjj...
Type :normal ^i//Ctrl + VEscA//
Hit Return to validate the command

When I search in vim and then press “esc” cursor returns to previous position

Something has seemingly happened to my vim install, and I'm not exactly sure what. I'm a long time vim user (although I don't use it as a main editor).
When I search:
/foo
I want to edit the location that it found, so I press ESC (in preparation for getting into insert mode). vim now jumps BACK to where I started from in the file. E.g. if I was on line 0 of a 3000 line file, I search for a particular string, find it at line 1700, and want to edit it - ESC takes me back to line 0.
What's going on? Did I accidentally set some strange mode? Or did I forget a hotkey combination that I should know?
This is the expected behaviour with the incsearch option on:
Note that the match will be shown, but the cursor will return to its
original position when no match is found and when pressing <Esc>. You
still need to finish the search command with <Enter> to move the
cursor to the match.
If incsearch is not on then the cursor doesn't jump to the first match at all, it doesn't move until you press <Enter>.
you said
so I press ESC (in preparation for getting into insert mode).
you don't need to press ESC before you can get to insert mode, you need to press enter (known in vim as <CR> for carriage return).
so if you wanted to find foo and start inserting text, type
/foo<CR>i
remember that <CR> is a single pressing of the enter button.

How can I move the cursor to the end of the line in insert mode?

Is there an existing command or a configuration for my .vimrc that will make CTRL+right arrow go to the end of the line in insert mode instead of going to the next line? Like in most common editors, that shortcut jumps word by word, but in vim it goes to the next line when the cursor is at the last word in the line.
If there is a different fast way to jump to the end of the line in insert mode, that would also be useful.
In general, the best way to navigate in vim is in normal mode, and not in insert mode. I suggest that instead of finding ways to navigate in insert mode, you use the power of normal mode as intended. There are many ways to do this, but here are a couple of suggestions:
Use CTRL+o while in insert mode to temporarily enter normal mode for the next command, and then $ to go to the end of the line (after which you will be returned to insert mode)
Use ESC to return to normal mode, and then A which both moves the cursor to the end of the line and enters insert mode
If you want to navigate word by word (in normal mode), you can use w to move to the next word, or e to move to the end of the next word.
The Home and End keys are usually interpreted by vim in insert-mode as beginning and end of line (like 0 and $), respectively.
This is documented in the ins-special-special topic, e.g.,
:h ins-special-special

moving text to next line in vi

There is a line in Shell script
f) SCREEN = TRUE
and I want to make it
f)
SCREEN = TRUE
I cannot do this very easily in vi. Obviously in a normal text editor enter command can shift the text after f) to the next line. What is the best way to do it in vi?
Press l to move the cursor right and j to move the cursor down to get the cursor to the S. Then press i to "insert" and hit enter to create a new line. Then hit esc to stop inserting and :wq to save and quit.
There are lots of basic vi command lists on the internet that might help.
I was trying to figure out if there was a way to do this in normal mode without pressing 3 buttons to insert, move the down line with enter and then escape to normal. I believe this is what OP may have been asking for. Not sure why there aren't any built in shortcuts for this.. Anyways I just ended up making a quick mapping in my .vimrc.
nmap <leader>j i<cr><esc>
With your cursor on the line, 0fSi<ENTER><ESC>
(0 Go to beginning of line, fS find 'S', i enter insert mode, <ENTER> insert some kind of newline, <ESC> exit insert mode)

Cursor positioning when entering insert mode

When I switch to command mode in Vim, the cursor seems to move one character back when it's at the end of or on a word, and when I go to the end-of-line with $, it does not go to the actual end of line but one character before the end of the last word, and l ("el") does not move it forward and I have to use the arrow key to get there.
I haven't been able to find documentation of this behavior, but this seems strange to me. What's the reasoning behind this (for my own curiosity), and how can I get around it (or deal with it)?
it is a little more clear if you use gvim, where the cursor changes.
insert mode in gvim has the cursor as an I-beam, since the next letter you type will be inserted after the |. normal mode has the block cursor, because the next thing you type may just effect the letter that is currently highlighted (like if you use x, s, etc). So insert mode is actually adding text, but normal mode is modifying text in some way.
So in normal mode, jumping to the end of the line really just means the last character, since that is the last thing that is possible to be modified. in insert mode, the cursor goes passed the last character, since it is possible to add things afterwards.
One thing to keep in mind is that you can control which side of the block you end up on going from normal mode to insert mode
([] means that the block cursor is over that h)
Let's say you have t[h]is text
if you pressed i at this point, the cursor would look like this (in gvim)
(| being the insert mode cursor)
Let's say you have t|his text
if you pressed a instead of i, it would look like this
Let's say you have th|is text
Another thing to keep in mind (as pavanlimo mentioned), from normal mode you can go to insert mode with your cursor just before the first character of the line, or just after the last character, with shift-I or shift-A.
I'm not quite sure of the reasoning behind it, but you can work around it by pressing:
Shift + a
You might be interested in the option virtualedit and the following value:
set virtualedit=onemore
With this option you can move the cursor one character over the end of the line and then press i to insert mode after the last character.
This solves the issue in a way but personally I find this behavior a bit odd. Only in a few cases you encounter the problem so it might be worth ignoring it ;-)
That's because all commands you use affect the letter the cursor is over. If wouldn't make sense to press x (delete 1 letter) behind the actual letter.
There's actually no need to move the cursor in command mode behind the last letter, if you want to e.g. append you can press a which puts the cursor behind the letter in insertion mode.
It is implementation-dependent on whether the cursor can move past the end of the line (at least it is an option in my editor's VIM emulation mode). You can press a to enter insert mode and start editing after the current character in this situation (rather than i).
pressing i will enter the insert mode before the cursor
a after the cursor
I before the first non empty character in the line
A at the end of the line.
So, use A to quickly start typing at the end of the line.
I suggest
:set virtualedit=onemore
:inoremap <Esc> <Esc>`^

Resources