Vim Tips Wiki
(Uploaded by JohnBot from a locally edited file)
No edit summary
Tag: apiedit
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=105
 
|id=105
 
|previous=104
 
|previous=104
 
|next=106
 
|next=106
|created=September 3, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
 
|author=Andrew Pimlott
 
|author=Andrew Pimlott
 
|version=5.7
 
|version=5.7
 
|rating=4/5
 
|rating=4/5
  +
|category1=
  +
|category2=
 
}}
 
}}
  +
It can be convenient to scroll down or up by one line, while not changing the cursor screen line.
I sometimes found myself moving down a few lines with j, then scrolling down about the same number of lines with <C-E> to put the cursor in roughly the same place as it started. I decided I wanted to map <C-J> (and <C-K>, respectively) to the move-and-scroll operation. First, I did
 
   
  +
You can scroll down in this way by typing <code><C-e>j</code> (Ctrl-e to scroll down to see more text at the bottom, and <code>j</code> to move the cursor down so its screen line hasn't changed). An alternative is <code>1<C-d></code> (Ctrl-d to scroll down, with a count of <code>1</code> which sets the <code>'scroll'</code> option to 1 so one line is scrolled).
:map &lt;C-J&gt; &lt;C-E&gt;j
 
   
  +
A superior solution is to place the following in your [[vimrc]]. This script behaves better at the top and bottom of the buffer, and does not change the <code>'scroll'</code> option. Press Ctrl-J to scroll down or Ctrl-K to scroll up, in normal and visual modes.
This was pretty good, but behaved funny at the beginning and end of files. Then, I realized that &lt;C-D&gt; already combined move and scroll, so I figured that giving &lt;C-D&gt; a count of 1 would do it:
 
 
:map &lt;C-J&gt; 1&lt;C-D&gt;
 
 
Unfortunately, this permanently attaches a count to &lt;C-D&gt; (ugh!), so I have to undo that:
 
 
:map &lt;C-J&gt; 1&lt;C-D&gt;:set scroll=0&lt;CR&gt;
 
 
This has the drawback of not necessarily resetting scroll to its original value, but since I never change scroll, it's good enough for me. It would be nice if there were a version of &lt;C-D&gt; that did not have the side-affect of changing scroll.
 
 
==Comments==
 
And for Page-Up/Page-Down, see [[VimTip320]]. My solution:
 
 
map &lt;PageDown&gt; :set scroll=0&lt;CR&gt;:set scroll^=2&lt;CR&gt;:set scroll-=1&lt;CR&gt;&lt;C-D&gt;:set scroll=0&lt;CR&gt;
 
map &lt;PageUp&gt; :set scroll=0&lt;CR&gt;:set scroll^=2&lt;CR&gt;:set scroll-=1&lt;CR&gt;&lt;C-U&gt;:set scroll=0&lt;CR&gt;
 
 
----
 
If you set the scroll to 1 for C-D and C-U, you can still use C-F and C-B to go forward and back one screen per press. Best of both worlds!!
 
 
----
 
'''TODO: Merge following into the above tip 105.'''<br>
 
'''Following is from tip 417 that has been removed.'''
 
 
A long time ago, I entered tip 105. I used that mapping for a long time, but it always had a couple problems. One, it reset the scroll parameter. Two, it didn't work in visual mode, because :set scroll exits visual mode. I was reviewing my Vim configuration and learning some new tricks, and in the process I improved this mapping.
 
 
Now, Ctrl-J and Ctrl-K will move the cursor one line down or up, and scroll one line down or up--so the cursor remains on the same screen line (except near the beginning and end of the file)--in both normal and visual modes. And the scroll parameter is unaffected.
 
   
 
<pre>
 
<pre>
" N&lt;C-D&gt; and N&lt;C-U&gt; idiotically change the scroll setting
 
 
function! s:Saving_scroll(cmd)
 
function! s:Saving_scroll(cmd)
let save_scroll = &amp;scroll
+
let save_scroll = &scroll
execute "normal" a:cmd
+
execute 'normal! ' . a:cmd
let &amp;scroll = save_scroll
+
let &scroll = save_scroll
 
endfunction
 
endfunction
 
nnoremap <C-J> :call <SID>Saving_scroll("1<C-V><C-D>")<CR>
 
 
vnoremap <C-J> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-D>")<CR>
" move and scroll
 
nmap &lt;C-J&gt; :call &lt;SID&gt;Saving_scroll("1&lt;C-V&gt;&lt;C-D&gt;")&lt;CR&gt;
+
nnoremap <C-K> :call <SID>Saving_scroll("1<C-V><C-U>")<CR>
vmap &lt;C-J&gt; &lt;Esc&gt;:call &lt;SID&gt;Saving_scroll("gv1&lt;C-V&gt;&lt;C-D&gt;")&lt;CR&gt;
+
vnoremap <C-K> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-U>")<CR>
nmap &lt;C-K&gt; :call &lt;SID&gt;Saving_scroll("1&lt;C-V&gt;&lt;C-U&gt;")&lt;CR&gt;
 
vmap &lt;C-K&gt; &lt;Esc&gt;:call &lt;SID&gt;Saving_scroll("gv1&lt;C-V&gt;&lt;C-U&gt;")&lt;CR&gt;
 
 
</pre>
 
</pre>
   
 
==Comments==
This is an example of several terrible Vim hacks, to boot.
 
  +
For PageDown and PageUp, see the comments in [[VimTip320|Page up/down and keep cursor position]].
   
 
----
 
----
I set 'scrolloff' value for somewhat of the same effect. Your way is more complete though.
 
   
  +
If you want to keep the cursor centered vertically, you can also set scrolloff to a large value (such as 50). This won't work if you want the cursor closer to the edge, but it has the benefit that it works with all movement commands.
----
 

Latest revision as of 22:38, 9 September 2016

Tip 105 Printable Monobook Previous Next

created 2001 · complexity basic · author Andrew Pimlott · version 5.7


It can be convenient to scroll down or up by one line, while not changing the cursor screen line.

You can scroll down in this way by typing <C-e>j (Ctrl-e to scroll down to see more text at the bottom, and j to move the cursor down so its screen line hasn't changed). An alternative is 1<C-d> (Ctrl-d to scroll down, with a count of 1 which sets the 'scroll' option to 1 so one line is scrolled).

A superior solution is to place the following in your vimrc. This script behaves better at the top and bottom of the buffer, and does not change the 'scroll' option. Press Ctrl-J to scroll down or Ctrl-K to scroll up, in normal and visual modes.

function! s:Saving_scroll(cmd)
  let save_scroll = &scroll
  execute 'normal! ' . a:cmd
  let &scroll = save_scroll
endfunction
nnoremap <C-J> :call <SID>Saving_scroll("1<C-V><C-D>")<CR>
vnoremap <C-J> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-D>")<CR>
nnoremap <C-K> :call <SID>Saving_scroll("1<C-V><C-U>")<CR>
vnoremap <C-K> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-U>")<CR>

Comments[]

For PageDown and PageUp, see the comments in Page up/down and keep cursor position.


If you want to keep the cursor centered vertically, you can also set scrolloff to a large value (such as 50). This won't work if you want the cursor closer to the edge, but it has the benefit that it works with all movement commands.