Disable Macvim scrolloff when selecting with the mouse - vim

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>

Related

don't use scrolloff=5 for mouse clicks in (neo)vim

I use (neo)vim with mouse=a and scrolloff=5. Clicking on one of the top or bottom five lines with the mouse causes the screen to jump (so that the new cursor position is more centered), which distracts me. Is there any way to disable "so=5" for mouse-clicking only?
I use (neo)vim in different terminal emulators on linux.
You can disable the scrolloff value using a click mapping, refer :help <LeftMouse> However, you'll have to re-enable the scroll setting afterwards as re-enabling the setting in the mapping would again cause the view to jump up/down.
This will disable the scrolloff setting and the view will remain unchanged:
nnoremap <LeftMouse> :let &so=0<cr><LeftMouse>
With the above, the view will not jump, but you'll have to re-enable so
We could re-enable it in the same mapping, but that would mean, you'll see the same behavior of view jumping up or down as so is applied immediately after it's set.
nnoremap <LeftMouse> :let temp=&so<cr>:let &so=0<cr><LeftMouse>:let &so=temp<cr>
So, there isn't an ideal solution to what yo want (at least I know of). You could either disable so completely and use something like zz or get used to the behavior of so on mouse click.

How to get mouse movement and mouse position in 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>

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 :(

vimscript: add functionality to mouse click with key mapping

I use vim in xterms on Arch linux. Wanting to automatically set marks for subsequent command ranges I wrote this mapping
map <LeftMouse> mp:let g:oc=g:nc<cr>:let g:nc=getpos('.')<cr>:call setpos("'o", g:oc)<cr>:call cursor(g:nc[1], g:nc[2])<cr>
but now the mouse no longer places the cursor at the clicked location. How can I keep standard mouse function, and add to it rather than replacing it?
It seems its not possible to modify LeftMouse, but you can achieve the effect of it using LeftRelease. So my mapping does what I want as
nmap <LeftRelease> mp:let g:oc=g:nc<cr>:let g:nc=getpos('.')<cr>:call setpos("'o", g:oc)<cr>
and the standard function of is unchanged.
You can :set mouse=a for using visual select mode. (This is not what you wanted but it is a trick).
Then, click on text, you will see that it is getting selected. Then, you can release it.
A mapping like
:map gv ma
will set the last recently selected text as mark a.
Other way:
Instead of :set mouse=a, you can press v and then select a letter or a word, depending upon your convenience and then ma for marking it as mark a.
A short mapping for it will be
:map vly ma

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.

Resources