Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move screen left/right or center it horizontally without moving cursor in vim?

Tags:

vim

After e.g. pasting in-line my window is showing a far right part of the document (column >80).

With leaving my cursor at the current position, how do I center the screen horizontally around it. Alternatively can I move the screen half a screen width further left, avoiding a fixed number as in e.g. 44zh?

While the title is non-specific all answers deal with only moving up/down: How to move screen without moving cursor in Vim?

like image 799
djangonaut Avatar asked May 11 '17 12:05

djangonaut


3 Answers

I don't know about centering the screen horizontally around the cursor, but as far as moving the screen half a screen left/right, try this (from :help scroll-horizontal)

zL          Move the view on the text half a screenwidth to the
            right, thus scroll the text half a screenwidth to the
            left.  This only works when 'wrap' is off.  {not in
            Vi}


zH          Move the view on the text half a screenwidth to the
            left, thus scroll the text half a screenwidth to the
            right.  This only works when 'wrap' is off.  {not in
            Vi}
like image 94
omnikron Avatar answered Nov 02 '22 15:11

omnikron


You can also use:

zl and zh to move only one character

like image 40
Fabien Avatar answered Nov 02 '22 15:11

Fabien


A nice configuration option that I am using is sidescrolloff, it's the opposite of scrolloff.

I am currently using them both in my .vimrc which is a setting I can't live without. Of course you can increase/decrease the offset(s) as desired. It kind of depends on the type of document(s) you are working on I guess :)

set scrolloff=20       " keep 20 lines visible above and below cursor at all times                                                                        
set sidescrolloff=30   " keep 30 columns visible left and right of the cursor at all times

Below some info from the Vim :help

scrolloff Minimal number of screen lines to keep above and below the cursor. This will make some context visible around where you are working.

sidescrolloff The minimal number of screen columns to keep to the left and to the right of the cursor if 'nowrap' is set.

like image 32
Rens Tillmann Avatar answered Nov 02 '22 14:11

Rens Tillmann