Can VIM remember the mode when switching between tabs? - vim

I originally noticed this in MacVim, but turns out this happens in the terminal as well. If you have two tabs open and enter the Insert mode in one, then switch to the other tab (by clicking on it in GUI, for example), the Insert mode is still active. This wreaks havoc when I'm editing something and want to quickly switch to the other tab to search/look up/etc, because the Normal mode keystrokes get inserted into the buffer.
Any workarounds for this?

One way using autocommands:
au TabEnter * stopinsert
Add this line to your .vimrc file and it should work as you wish.

Related

How to open new tabs instead of splits by default in VIM?

I have installed rust.vim and it has the :Crun command, which opens a split when called. Is it possible to open a new tab instead of a split? If it is possible, how to do it? (I know that it is possible to call C-w T to switch to tab mode, but I'd like to have this automated)
Oh, I found out: autocmd BufNew * wincmd T

In vim, mouse clicks and scrolls are seen as keystrokes and messing up my text

I am having issues with my vim setup. If I click, or if I scroll with the mouse inside vim, I get a strange behavior. Those actions sometimes change my mode to insert and copy or paste things from the register, or insert random characters.
I do not know when the problem started since I do not use my mouse too often inside vim. However, sometimes I click or scroll on my window, and these commands are messing up my document.
I am using arch with i3 and uxrvt. I would also share my .vimrc file, but I am new here, so I do not know if I should just copy and paste it.
I noticed that if I open vim with xterm instead of uxrvt, the mouse clicks and scrolls refresh the cursor and place it at the middle of the screen and to the left.
I have set the option set mouse=a in my .vimrc
Please help me :)
Ok, I figured it out what was the issue. At some point, I mapped this command: nnoremap <esc> :noh<return><esc> to disable the highlighted results of a search after pressing <esc>
When reading :h set ttymouse, I realized that the mouse clicks and scrolls return <esc> and some other characters to the editor. This, together with my remapping of <esc> was messing up the return values of my mouse.
I fixed it by removing my remapping of <esc>, but I would like to use that mapping without messing my mouse :(

How can I explain this behaviour in Vim?

Vim is so awesome. For example, you have a file, called 'test0.html', stored in a folder.
In the same folder, you store the folder 'test' with the files test1.html, and test2.html.
Then you put in test0.html the following content:
include('test/test1.html');
include('test/test2.html');
In vim, you put the cursor on the filenames. You open the files under the corsor with the keys gf. Thats why Vim is so awesome.
I would like to open in a new tab. That's possible with the keys gF.
But what if you want to stay in the same file, but open the file in a background tab, like Chrome does?
So I'm mapping the key.
noremap gf <c-w>gF<c-PageDown>
So, when my cursor is on test1.html, it open with the key gf in a background tab. Wonderful, now I'm a satisfied man.
Then I want to open test2.html under cursor.
Vim jumps to the tab of test1.html, instead stay on test0.html
When I tried to debug this weird behaviour, by only mapping gf to gF, and then do manual CTRL+pagedown, I get the expected behaviour.
My guess is that Vim is much faster with executing the command before he opens the new tab (gF), and so I get to the last tab from the first tab.
Am I correct in my guess explaination?
<c-PageDown> or more commonly used gT will got to the previous tab. <c-w>gF on the other hand will open the file under the cursor in a new tab. That tab will be last tab. So doing a gT will not always make you go back to the previous tab.
You can change your mapping to go back to the previous tab like so:
nnoremap gf :execute "normal! \<lt>c-w>gF" . tabpagenr() . "gt"<cr>
However I would personally suggest you avoid using tabs in such a manner and use buffers instead.
noremap gf :tabe<cfile><CR><c-PageUp>
This is even better. When the file doesn't exist, Vim will create a new one.

Is it possible to restore windows status after maximum window?

I always have this problem. When a windows seems too small to view code, i will type :only the maximum this window and hide other windows. But when the editing is finished i quit want to restore the previous status of windows. Exactly the same before i maximum one window. It there any plugin to do this job? Or it's build-in in vim?
EDIT: I found a plugin called ZoomWin can actually do this job. But 0 can't been map to :ZoomWin in my vim. Still don't know why. The help file says i can use 0 to call Zoomvim just after i installing this plugin.
You could probably write a script using mkview and loadview if you wanted to keep this all in one tab, however, this is the exact thing that vim's tabs were made for. I suggest using the following mapping to map \0 to open the current buffer in a new tab. To close the tab just do :q as you would normally do and you'll go back to your previous tab which contains the window layout you want.
:nnoremap <leader>0 :tabedit %<cr>
Note that this mapping uses <leader> so if you've changed your mapleader then the sequence will not be \0.

mvim closing last tab want not to close window

I am just getting start with mvim coming from e-texteditor on Windows. One little annoyance (for me) is that when I use the cmd+w to close tabs, if the last tab is closed the window of mvim is also closed, is there any chance that I can keep that window open with one empty tab in it?
In your ~/.gvimrc file (not your .vimrc; and create one if it doesn't exist already), add:
macm File.Close key=<nop>
nnoremap <silent> <D-w> <Esc>:bd<CR>
The first line unmaps the <D-w> menu binding so you can remap it. The second maps <D-w> to close each vim-window one at a time, which will close the tab if it is the last vim-window in the tab, and will leave the OS window open on the last remaining tabpage. The caveat is that it will also cycle through all hidden buffers in that last window, closing each in turn (though you could use <D-S-w> at this point to close the OS window at once). c.f. :help :bd. If you don't desire this behavior, you could use this instead:
nnoremap <silent> <D-w> <Esc>:tabclose<CR>
Which is more strictly what you were asking for. c.f. :help :tabclose
Duplicate the mapping replacing nnoremap with inoremap if you want it to work in insert mode, too.
You might want to look at this script. It does something similar to what you have mentioned. Using Araxia's mapping for Command-W you can replace the scripts commands to Cmd-w.

Resources