Scroll the other window in VIM Split - vim

How do I scroll the other window in :vsplit in VIM? I've looked everywhere, but all websites are talking about how to sync both windows to scroll together. I want to look at one file, edit it, while scrolling the other.

I'm fairly (ok, quite) certain there's nothing built in to do this, not least of all because it only makes sense when there are exactly two windows. For that specific case, however, you can use a mapping. Put these in your ~/.vimrc:
nmap <a-j> <c-w>w<c-e><c-w>w
nmap <a-k> <c-w>w<c-y><c-w>w
This will make Alt+J scroll down and Alt+K up, you can change those as you like. If you want it to work with more windows, you'll have to write a script.

" SCROLLING FOR OTHER SPLIT WINDOWS (JUST 2 WINDOWS)
nmap <M-j> <c-w>w<c-e><c-w>wh " Scroll down one line other pane
nmap <M-k> <c-w>w<c-y><c-w>wh " Scroll up one line other pane
nmap <M-d> <c-w>w<c-d><c-w>wh " Scroll down half screen other pane
nmap <M-u> <c-w>w<c-u><c-w>wh " Scroll up half screen other pane
nmap <M-f> <c-w>w<c-f><c-w>wh " Scroll down one screen other pane
nmap <M-b> <c-w>w<c-b><c-w>wh " Scroll up one screen other pane
<M> is Meta, Opt, or Alt key. When I tested the shortcut, my cursor in the current window go forward 1 character. So I ended mapping the bindings with h. But these work whenever you don't reach the first or last line of your other pane or the cursor will just switch to your current pane then start scrolling.

Related

vim scrolls wrong window

I am using vim 7.4 under Ubuntu Linux.
When I split window by using ":sp hello.c",
if I click the upper window, then the mouse wheel scrolls the upper window.
But if I click the lower window, then depending on which part of the lower window I click, it scrolls either the upper window or the lower window.
Specifically, if I click inside the upper half of the lower window, it scrolls the upper window; if I click inside the lower half of the lower window, it scrolls the lower window.
How can I make it work correctly?
Thanks.
Here is my setting:
ambiwidth=double helplang=ko nomodeline ruler syntax=php ttymouse=xterm2
filetype=php history=50 mouse=a scroll=25 ttyfast
backspace=indent,eol,start
fileencoding=utf-8
fileencodings=ucs-bom,utf-8,default,latin1
printoptions=paper:a4
runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/share/vim/vimfiles/after,/var/lib/vi
m/addons/after,~/.vim/after
suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
I was able to get around this problem, by adding the following lines to my .vimrc file:
noremap <ScrollWheelUp> 4<C-Y>
noremap <ScrollWheelDown> 4<C-E>
noremap <S-ScrollWheelUp> <C-B>
noremap <S-ScrollWheelDown> <C-F>
from the following document:
https://superuser.com/questions/351972/how-can-i-change-the-scroll-wheel-behavior-in-vim-so-that-it-scrolls-instead-of

vim - show one file in several files consistently

I.e. is it possible in vim to edit large file in several simultaneously opened tabs in the following way: first part of text (that fill all vertical space), second - in the second pane, and so on.
if it is not possible in vim, maybe it's feature is implemented in other editors?
Synchronizes two vim panes
Lets say your window is 20 rows, then
:vsplit // splits window into left and right panes
^w^w // focuses right pane
20^e // scrolls right pane down 20 rows
:windo set scrollbind // syncronizes both panes
^w means press and hold CTRL and then press w. Likewise for ^e
:windo means :set scrollbind in all open panes
Implement it in a function
Wraps the above commands in a function and binds it to [
function! SyncScroll()
vsplit
execute "normal! \<c-w>\<c-w>" . winheight(0) . "\<c-e>"
windo set scrollbind
endfunction
nnoremap [ :call SyncScroll()<cr>
You can add the above function and mapping to your vimrc with :e $MYVIMRC and reload it with :source $MYVIMRC
ref:
vim
docs - scrollbinding
wiki - scrollbinding
I think you're looking for the MPage plugin. You probably want to install the latest version from DrChip's page.

How to change the default position of quickfix window in Vim?

Setup: MacVim with MiniBufExplorer plugin window spanning the entire top and Taglist plugin window on the right.
Due to the fact that I keep my Taglist on the right, whenever I open the quickfix window, it is positioned on the far right, below the Taglist window (with the same width as the Taglist window)
Is it possible to change the default opening position logic, so that the quickfix window will open below my main code window (down and to the left) or maybe span the entire width at the bottom of the Vim viewport?
While it is not possible to change the default split-window behavior
of the :copen command, one can approach the issue in two ways.
1. Use the commands that directly alter window splitting directions
(see :help :vertical and below until the “Closing a window” paragraph).
For instance, consider the commands:
:botright copen
or
:botright cwindow
to make the quickfix window open as the bottommost one, or even:
:vertical topleft cwindow
to open it to the top left of the current window.
These commands can be shortened to :bo cope, :bo cw, and :vert to cw, respectively. Also, one can, of course, create a short mapping or
a custom command for their quick invocation.
2. Alternatively, move the quickfix window to the bottom of the window
layout using an auto-command:
:autocmd FileType qf wincmd J
This trigger takes advantage of the fact that the quickfix window can
be easily distinguished by its file-type, qf. The wincmd J command
is equivalent to the
[Ctrl+W,
Shift+J]
shortcut sequence instructing Vim to move the current window to the
very bottom of the screen (see :help :wincmd and :help ^WJ).
By default, Vim opens the new window above the current one for horizontal splits, and to the left of the current one for vertical splits (:help opening-window). You can customize this behavior like most other things in Vim:
make the new window appear below the current window.
:set splitbelow
make the new window appear on the right.
:set splitright

What is the best way to do smooth scrolling in Vim?

The main scrolling commands in Vim are:
Ctrl-B and Ctrl-F, as well as PageUp and PageDown scroll by full page
Ctrl-U and Ctrl-D scroll half a page by default
Ctrl-Y and Ctrl-E scroll one line
I lose visual context every time for the former two, so I have developed the bad habit of hitting the latter (Ctrl-Y and Ctrl-E) repetitively.
Since there is currently no first party support for smooth scrolling, what are the least objectionable workarounds/plugins?
I use both Vim and GVim depending on the task, and am happy to customize them separately if there is no one really good hack that works for both. The mouse scroll wheel works nicely in GVim, but I'm looking for keyboard based solutions.
Update: I have now pushed this code, refactored somewhat according to the guidelines at :help write-plugin, to a Github repo.
Using the Keyboard
Here is what I have in my .vimrc:
function SmoothScroll(up)
if a:up
let scrollaction="^Y"
else
let scrollaction="^E"
endif
exec "normal " . scrollaction
redraw
let counter=1
while counter<&scroll
let counter+=1
sleep 10m
redraw
exec "normal " . scrollaction
endwhile
endfunction
nnoremap <C-U> :call SmoothScroll(1)<Enter>
nnoremap <C-D> :call SmoothScroll(0)<Enter>
inoremap <C-U> <Esc>:call SmoothScroll(1)<Enter>i
inoremap <C-D> <Esc>:call SmoothScroll(0)<Enter>i
Features:
Scroll on the base of the Vim scroll option.
Customizable scrolling speed (adjust time argument of the sleep command; I use ten milliseconds). Note: just like slowing down the frame rate on a video, if you slow down the smooth scroll too much it will be jerky scroll, not smooth scroll. But whatever works best for you.
Works in normal or insert mode.
Note: all you copy-and-pasters, remember that the ^ character indicates a control character; copy-paste will produce invalid results and these must be entered manually!
^Y – CTRL-V then CTRL-Y
^E – CTRL-V then CTRL-E
However, the <C-U> and <Enter> style syntaxes are literally typed as those characters; the map command intelligently converts them to control characters.
Using the Mouse
The question mentions that scrolling with the mouse works well in GVim, but a keyboard solution is desired. This implies to me that the asker may be interested in a mouse solution if it works in regular terminal Vim.
For me, turning mouse support on allows smooth scrolling through the mouse wheel. Also, for me, smooth scrolling is most important when I am looking around (i.e. in normal mode), not when I am editing (in insert mode), and if I am not actively editing, the need for my hands to stay on the keyboard at all times is removed, so this works well.
On the basis of this question, though, it would seem that some people have to do some more manual setup beyond simply turning the mouse on (I just use set mouse=n):
My .vimrc has the following lines
set mouse=a
map <ScrollWheelUp> <C-Y>
map <ScrollWheelDown> <C-E>
There is a simple remap hack in vim's tips.txt:
Smooth scrolling *scroll-smooth*
If you like the scrolling to go a bit smoother, you can use these mappings:
:map <C-U> <C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y>
:map <C-D> <C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E>
Shameless plug, but I created a plugin here that you can use to easily adjust the distance, speed, and duration of the scrolling animation: https://github.com/terryma/vim-smooth-scroll
A Recent Plugin
I have posted something similar here, but basically there is a great plugin that we can use now for scrolling, called terryma/vim-smooth-scroll. It provides a very nice and smooth scrolling.
The install is quite easy:
1 I use Vundle so I simply appended this in .vimrc.bundles :
Bundle terryma/vim-smooth-scroll
In the latest version of Vundle available today, 29th April of 2016, you can put this in your .vimrc:
Plugin 'terryma/vim-smooth-scroll'
2 As stated in the doc, you can set up 3 arguments: distance, duration and speed.
I am using this in my .vimrc file:
noremap <silent> <c-b> :call smooth_scroll#up(&scroll*2, 10, 4)<CR>
noremap <silent> <c-f> :call smooth_scroll#down(&scroll*2, 10, 4)<CR>
What I do is I set the keyboard repeat to very fast, about 120 chars / second, and the delay small.
Then I map to 4j and to 4k
I navigate up and down source code using j and k which moves the cursor up and down nice and quick, pretty smooth.
But here's the good part, and this works on Linux, not Windows.
For a number of years now, X11's keyboard input works in such a way that when you press and hold j it obviously starts putting out j characters. But when you then keep holding down j and then also press the ctrl key, X11 starts putting out c-j characters without you having to re-press the j key. Then when you let go of the ctrl key and still keep on pressing j, X11 continues to put j's again.
So j makes the cursor start moving nice and smooth downwards, and you can periodically hit ctrl without letting go of j to give it a boost, a jolt.
Also, I do what Devin does, and I set scrolloffset to 5.
Lastly, I swap ctrl and cap lock. The default position of the ctrl key is completely retarded (no offense intended). It makes you have to rotate your left hand. I almost never use caps lock, so I swap them. Then my left pink finger can reach the ctrl key without any yoga moves.
These things have worked for me for years. I only use vim, never gvim.
This isn't exactly smooth scrolling, but it's how I handle not losing context when jumping pages.
set so=7
'scrolloff' 'so' number (default 0)
global
{not in Vi}
Minimal number of screen lines to keep above and below the cursor.
This will make some context visible around where you are working. If
you set it to a very large value (999) the cursor line will always be
in the middle of the window (except at the start or end of the file or
when long lines wrap).
For scrolling horizontally see 'sidescrolloff'.
NOTE: This option is set to 0 when 'compatible' is set.
The Plugin cskeeters/vim-smooth-scroll supports smooth scrolling and requires no configuration. It supports to support smooth scrolling with zt, zz, and zb. It's a fork of terryma's plugin. Some of the open pull requests have been applied.
This combines many of these answers, and this is what I use.
noremap <expr> <C-u> repeat("\<C-y> :sleep 10m<CR>", winheight('%')/2)
noremap <expr> <C-d> repeat("\<C-e> :sleep 10m<CR>", winheight('%')/2)
N <CR-E>
N <CR-Y>
...where 'N' is the number of single lines you want to scroll.
Not smooth in literal sense, but you keep the keyboard.
This may be controversial for hardcore users, but... the best way to smooth scroll in Vim is... mouse wheel.
I just found this plugin called "accelerated-smooth-scroll" ("Vim plugin for accelerated smooth scroll (mapping to <C-D>/<C-U>, <C-F>/<C-B>)") which can be for example used through Vundle by putting this line in your .vimrc:
Plugin 'yonchu/accelerated-smooth-scroll'
Then by restarting Vim and running the :PluginInstall command, then again restart Vim and use the <C-D> (Ctrl+D) and <C-O> (Ctrl+O) commands normally.
Sadly, this plugin also moves the cursor instead of what I wanted: to just scroll the screen like the <C-E> and <C-Y> commands.
I slightly modified #Keith Pinson's code so that ctrl-f and ctrl-b can be mapped too:
function SmoothScroll(scroll_direction, n_scroll)
let n_scroll = a:n_scroll
if a:scroll_direction == 1
let scrollaction=""
else
let scrollaction=""
endif
exec "normal " . scrollaction
redraw
let counter=1
while counter<&scroll*n_scroll
let counter+=1
sleep 10m " ms per line
redraw
exec "normal " . scrollaction
endwhile
endfunction
" smoothly scroll the screen for some scrolling operations
nnoremap <C-U> :call SmoothScroll(1,1)<cr>
nnoremap <C-D> :call SmoothScroll(2,1)<cr>
nnoremap <C-B> :call SmoothScroll(1,2)<cr>
nnoremap <C-F> :call SmoothScroll(2,2)<cr>

"Minimizing" vertical VIM window splits

I use horizontal and vertical window splits in religiously in VIM and up until recently, I enjoyed the comfort of two commands to effectively hide (or minimize) my horizontal splits. I set them up adding the following lines to my .vimrc file:
set winminheight=0
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
Hitting Control-j or Control-k navigates through horizontal splits by going up or down. What I'd like to accomplish is the same thing for vertical splits by showing or hiding the left or right split using Control-Shift-h and Control-Shift-l; h moving to the left, l moving to the right. I have tried the following with little to no success:
set winminwidth=0
map <S-C-L> 500<C-W>h<C-W>_
map <S-C-H> 500<C-W>l<C-W>_
The action would be similar to utilizing Control-w-< and Control-w->, only moving the vertical split completely to the left or write, not just one line at a time.
Any ideas on how to accomplish this? Thanks.
First up, you won't be able to use <S-C- (shift + control) in your code (see below). But you can use the 'mapleader' as your "shift" and then use the <C-h> and <C-l> like you want to. Like this:
set winminwidth=0
nmap <Leader><C-h> <C-W>h500<C-W>>
nmap <Leader><C-l> <C-W>l500<C-W>>
The common leader keys in vim are comma and back-slash:
:let mapleader = ","
But you'll find that this gets annoying to require 3 keystrokes for this, so you might as well just drop the control key stroke. This way (if your leader is comma) you can just press ",h" and ",l" to go to the splits to your left and right:
set winminwidth=0
nmap <Leader>h <C-W>h500<C-W>>
nmap <Leader>l <C-W>l500<C-W>>
" (FTW) :D
...
A guy named Tony Chapman answers why you can't use control + shift:
Vim maps its Ctrl+printable_key
combinations according to ASCII. This
means that "Ctrl+lowercase letter" is
the same as the corresponding
"Ctrl+uppercase letter" and that
Ctrl+<key> (where <key> is a printable
key) is only defined when <key> is in
the range 0x40-0x5F, a lowercase
letter, or a question mark. It also
means that Ctrl-[ is the same as Esc,
Ctrl-M is the same as Enter, Ctrl-I is
the same as Tab.
So yes, Ctrl-s and Ctrl-S (i.e. Ctrl-s
and Ctrl-Shift-s) are the same to
Vim. This is by design and is not
going to change.
Try
set winminwidth=0
map <S-C-L> <C-W>h<C-W>|
map <S-C-H> <C-W>l<C-W>|
This doesn't move a window completely to the left or right (that's <C-W>H and <C-W>L), it just moves the cursor to the left (or right) window and maximizes that window horizontally.
See :help CTRL-W_bar for more.
Crl-w 1 |
will minimize current window in Vim.

Resources