How to get mouse movement and mouse position in VIM? - vim

I am thinking about a plugin to show and hide nerdtree automatically. The nerdtree is shown when mouse is in the first 5 columns, and hide when the mouse is out of the first 10 columns. However, I cannot find the mouse movement events and mouse position api in the vim. Is there a method to get mouse movement in vim or vim-python?
PS: I mean the mouse movement instead of the cursor movement. To toggle NerdTree by cursor position I think it is not a good idea.

You can hook into mouse clicks via :nnoremap <LeftMouse> ..., but as that is supposed to set the current cursor position, it would be more consistent to also hook into equivalent cursor position changes via the keyboard. But having a NerdTree side panel slide in and out based on the cursor position in the buffer feels very odd... What I'm trying to say is: This is a bad approach for Vim.
Vim (even graphical GVIM) is a text editor with cell addressing. In the terminal, there's no way to get the current mouse position (without clicking); it's not part of the protocol: The mouse didn't exist when terminals were invented; and its support was grafted on much later. Now, GVIM could theoretically implement this, but it chooses to remain as close to the terminal as possible (cp. :help design-not).
alternative approaches
In Vim, everything is key-based, and the different modes make key mappings short, memorable, and often without modifier keys like Ctrl or Alt. I would map either an (unused) function key (<F3>), or combo (<Leader>t):
:nnoremap <Leader>t :NERDTreeToggle<CR>

Related

How to change the mouse cursor to arrow in Iterm2 in VIM

After using MacVim for a long time, I've switched to using VIM in Iterm2 directly. Although, I don't use the mouse often, when I do I miss MacVim's mouse cursor switching to Arrow when in normal mode, and to IBar when in insert mode and to Drag when moving the mouse over a split (to drag it).
Is it possible to configure the mouse cursor to react to the content that VIM presents and if so, how?

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 temporarily make the window I'm working on to be fullscreen in vim?

I use vim, and usually have more than one vertical/horizental window open, usually editing c++ header files alongside cpp files. How can I temporarily make the window I'm working on to be fullscreen, edit what I want, and then exit fullscreen?
By fullscreen I mean to fit vim window only, and not my total display screen.
Ctrl+W_ will maximize a window vertically.
Ctrl+W| will maximize a window horizontally.
So far as I'm aware, there is no way to restore the previous layout after these actions, but Ctrl+W= will resize all windows to equal sizes.
An option could be to pursue the editing in a new tab. The following command opens the active buffer into a new tab allowing you to see the buffer in the hole vim window.
:tab split
And close the tab when you're done:
:tabc
Edit:
You can always use the following command to use tt as a shortcut (or better add it to your .vimrc):
:noremap tt :tab split<CR>
and close is when you're done :
:wq
If I understand what you're asking, I think you'll find the ZoomWin plugin helpful (GitHub). If you've got a bunch of split windows, and you want to temporarily make the current window the only visible one, you can hit <C-w>o. When you want to revert to the previous split state, hit <C-w>o again.
[Edit] Note on key mappings:
The default key mapping for this plugin is <C-w>o, but that conflicts with a default Vim key mapping. By default, that does :only, which makes the current window the only window. If you'd like to retain that functionality, you can remap ZoomWin to another key. I remap it to <C-w>w, because I like to use the :only option as well. Here's my mapping:
nnoremap <silent> <C-w>w :ZoomWin<CR>
Note that this also overrides a default Vim mapping, related to moving to other visible windows (:help CTRL-W_w), but I never used that one anyway.
Use Ctrl w_ to maximize the current window vertically.
These are some useful commands that help work with windows:
:e filename - edit another file
:split filename - split window and load another file
ctrl-w up arrow - move cursor up a window
ctrl-w ctrl-w - move cursor to another window (cycle)
ctrl-w= - make all equal size
10 ctrl-w+ - increase window size by 10 lines
:vsplit file - vertical split
:sview file - same as split, but readonly
:hide - close current window
:only - keep only this window open
:ls - show current buffers
:b 2 - open buffer #2 in this window
I've tried ZoomWin and a few others. The problem is, they all destroy and try to re-create the windows. This is especially problematic with custom plugins like NERDTree, Tagbar and a few others. Icons and fonts are not drawn properly, sizes are messed up etc..
zoomwintab.vim is a simple zoom window plugin that uses vim's tabs feature to zoom into a window inspired by ZoomWin plugin but in a non-destructive manner.
https://github.com/troydm/zoomwintab.vim
I use Tmux, so I mapped it to <leader> z to stay in sync with tmux's <prefix> z
nnoremap <leader>z :ZoomWinTabToggle<CR>
An awesome plugin for toggling windows fullscreen is vim-maximizer.
After it's installed you can simply use <F3> (default shortcut) to toggle fullscreen on the window.
You can also customize the shortcut keys, for example if you wanted to use <C-w> z (similar to tmux shortcut):
nnoremap <silent><C-w>z :MaximizerToggle<CR>
vnoremap <silent><C-w>z :MaximizerToggle<CR>gv
inoremap <silent><C-w>z <C-o>:MaximizerToggle<CR>
Somehow the ZoomWin plugin did not work at all for me, my experience was kind of what arithran says. I couldn't find other plugins so I wrote this:
function! ToggleZoom(zoom)
if exists("t:restore_zoom") && (a:zoom == v:true || t:restore_zoom.win != winnr())
exec t:restore_zoom.cmd
unlet t:restore_zoom
elseif a:zoom
let t:restore_zoom = { 'win': winnr(), 'cmd': winrestcmd() }
exec "normal \<C-W>\|\<C-W>_"
endif
endfunction
augroup restorezoom
au WinEnter * silent! :call ToggleZoom(v:false)
augroup END
nnoremap <silent> <Leader>+ :call ToggleZoom(v:true)<CR>
It creates the effect. You use the mapped key (Leader and + in my case) to toggle between maximized / previous layout. If you change to another split in the same tab, maximization turns off.
Manoj somewhat answered with a lot more useful info, but as a first step this is what works for me:
:hide: Hide the current buffer - with a two-buffer split, this makes the other buffer full screen. Unlike :only, this command does not close unmodified buffers so you can unhide them.
:unhide: Re-create splits for each open buffer
The unhide command does not restore the previous layout, so you will have to manually rearrange your windows if needed ex. using one of these for simple vertical/horizontal splits (this is CTRL-W followed by the uppercase navigation letter - release CTRL-W before entering the letter):
CTRL-W SHIFT-H: Move window to the far left
CTRL-W SHIFT-J: Move window to the very bottom
CTRL-W SHIFT-K: Move window to the very top
CTRL-W SHIFT-L: Move window to the far right
These are the same keys used for navigation, without SHIFT (lowercase navigation letters):
CTRL-W H: Move focus to the left window
CTRL-W J: Move focus to the bottom window
CTRL-W K: Move focus to the top window
CTRL-W L: Move focus to the right window
The H, J, K and K keys alone move the cursor - arrow keys on the keyboard may work as well for navigation.

Disable Macvim scrolloff when selecting with the mouse

I have set a high value for scrolloff, which works really well for me when I am moving around with the keyboard. However, any time I try to select something with the mouse in an off-center line (so that I can copy some text), that line jumps to the center, preventing me from selecting that text. Is there a way to keep the scrolloff setting but prevent a mouse click from moving the cursor to that line?
I don't believe that there is a way to use an autocmd to set scrolloff=0 when using the mouse, but you could map a key to toggle between scrolloff=0 and scrolloff=50(or whatever value you are using) and just use that mapping before selecting text:
map <silent> <Leader>m :exec &scrolloff==0? "set scrolloff=50" : "set scrolloff=0"<CR>

Vim/MacVim: when I scroll with mouse, the text cursor moves too!

I've been getting used to Vim/MacVim for the last few weeks. One of main problems I seem to be having is when I scroll around using the mouse (especially when I'm trying to select large portions of text) the text insertion cursor moves too and doesn't stay where it was (like in TextMate for example). This means I've selected a large piece of text, when I scroll back up to review my selection the cursor will move which messes with the selection that I've made.
I do realise I should get used to text selection with visual mode, and I am one bit of a time, but sometimes it's the best tool to use the mouse.
Is there a way of fixing this behaviour?
:help scrolling tells you:
These commands move the contents of
the window. If the cursor position is
moved off of the window, the cursor is
moved onto the window (with
'scrolloff' screen lines around it).
So basically I would say that it is not possible to leave the cursor where it was when you are scrolling. The cursor is always visible in your window, and therefore your visual selection will extend.
Probably you would like to xnoremap <ScrollWheelUp> <esc><ScrollWheelUp> and same for ScrollWheelDown. Use then gv to restore your selection.
I made a screencast about Vim's changelist and jumplist which addresses the issue of Vim's cursor always being on screen. The changelist maintains a list of all of the places in your document where you have made an edit. You can move back and forward through the list with the commands g; and g, respectively. Or if you want to put your cursor back on the last place where you made an edit and go into insert mode, just press gi.
As Alois Cochard pointed out, the o key is very useful when you are in visual mode. It moves the cursor between the start and the beginning of your selection. So if your selection is larger than your screen, it will move you from one end to the other.

Resources