How to go back (ctrl+z) in vi/vim - 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...

Related

Mapping 'Ctrl + O' in vim doesn't work

I freshly installed Ubuntu 16.04 and then I installed vim. Soon I realized that Ctrl + O which I regularly use in vim to jump to the last place, does not work! After some search, I got the following commands:
:nnoremap <M-Left> <C-O>
and
:nnoremap <X1Mouse> <C-O>
I tried putting both these in my .vimrc but they don't give me the desired effect. I am really annoyed by this since this is one of the most used things that I use. What am I missing?
Edit: I reinstalled vim from scratch, also deleted the ~/.vimrc file. After deleting the file, I checked for /usr/share/vim/vimrc; it was there. Then I installed vim and tested for Ctrl+o; it works till I close the file. However, after I open it back, it forgets all the history.
Well, the actual answer as mentioned in the comments is your vim config files didn't have proper permissions on them. In addition to that, the key mappings you used in your question are invalid according to the docs:
:help X1Mouse
The X1 and X2 buttons refer to the extra buttons found on mice.
The 'Microsoft Explorer' mouse has these buttons available to the right thumb.
Currently X1 and X2 only work on Win32 environments.
And
:h M-Left
Alt-Left Move cursor to the beginning of the previous word
M = alt.
Put this in your vimrc. Enable the mouse first:
set mouse=a
map <LeftMouse> <c-o>
Although Vim uses inefficient gluing of movements together if you want to get to a far away arbitrary place on the screen, which the mouse solves well and much faster than Vim :)

Effective way to put ; at the end of line

Sorry for a noob question, but i find it struggling to just put a ";" at the end of line after writing a function. For example, I am coding in C and many time i need to write things like:
f(a);
what i usually type is (from normal mode, using bracket autopair-like feature):
if(a<ESC><SHIFT-a>;
and it need changing mode twice! Comparing to normal editor (sublime):
f(a<right>;
does anyone have more efficient way do do those typing? thanks for any help.
I think you have some "auto-close" plugin installed.
I have that kind of plugin too, and I don't press arrow keys either, since I don't have them on my keyboard. I have this:
" moving cursor out of (right of ) autoClosed brackets
inoremap <c-l> <esc>%%a
So with your example: it would be (assume already in INSERT mode)
f(a<ctrl-l>;
Thus, your fingers never leave the home row.
If you're a vim user, you can hit Shift-a.
Shift-a takes you from normal mode to insert mode, and starts your cursor at the end of the line.
(If you want to be an efficient vim user, you should remap esc to something like caps-lock.)
Comparing to normal editor (sublime):
f(a<right>;
Well… that's exactly how you would do it in Vim if you use Delimitmate or some other "autoclosing" plugin. Why do you insist on making things more complicated than they are?

VIM keybinding to jump back to initial position after indenting the whole file

I have created a keybinding that should indent a whole file.
My first solution looked like this:
map <F4> gg=G
The problem is that after pressing F4, the cursor jumped to the first line of the file. So I have tried to improve my solution with the feature of markers, look like this:
map <F4> mzgg=G'z<CR>
I expected this would have resolved my problem, but the command do the same as the first. When I try to jump to the z marker manually vim told me "marker not set".
After changing the keybinding, I have or course restarted vim! I am using the GVIM 7.3 on a WIN 7 machine.
Thank you in advance for your Help!
Edit:
After trying to get my keybinding working by tipping it directly to vim commandline. I find out that the keybinding was working quite nice. I think problem is that I create a session some times ago (with mksession) and if you load a session I think vim ignores the vimrc-file. Is this assumption right?
Solution:
In these thread I find a soultion to make mksession save less options.
Another lightweight approach: set the ` mark, format the buffer and jump back to the mark afterwards.
:nnoremap <key> m`gg=G``
I would recommend the use of CTRLo and CTRLi which allow to go respectively backward and forward in the jump list. See :help jumps.
The following mapping
map <F4> gg=G2<C-o>
works. (It jumps back two times in the jump list)
But generally, the jump list is a great way to navigate in a file, this is likely the shortcuts that use the most in my daily use. It is also works if you jump to a tag to go back to your original location.
You might also want to use nnoremap rather than map, this way it will only work in normal mode, and you could potentially reuse F4 in combination in another key binding without having recursive mappings.
so
nnoremap <F4> gg=G2<C-o>

Key mapping confusion in VIM,How to make vim works in a smart way?

I want to bind Ctrl-w with :q command in vim That is to say, I want to use Ctrl-w to close the current window of vim,
So I add this to my ~/.vimrc :
nmap <C-W> :q<cr>
When I use this to close a window that has something unsaved,this command cannot work.
So I want to make it works in a smart way: When the content is saved, just close the window. When the content remains unsaved, ask me whether to close the window directly like this command
:0,$s/a/b/gc
I don't know if this is clear enough for you, but thanks in advance.
There's the :confirm command for that. Just put it in front of :quit:
:nmap <C-W> :confirm q<cr>
The simplest thing to do would be to set confirm in your vimrc - this will prompt you before closing without saving (and a few other things, too; see :help confirm).
Otherwise, you could write a little vim script which uses &modified and confirm() (see :help confirm()).

How do I switch between command and insert mode in 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.

Resources