Vim: Lock top line of a window
Asked Answered
C

4

10

Is it possible in Vim to lock the top line of a window so that the first line in buffer is always seen on top of the window?

I have a file, say, dump of a database table. On the first line there are names of columns, other lines contain data. I want to be able to scroll contents up and down, and always see column names.

N.B. Lines can be lengthy, so I use nowrap and want column names and contents to scroll right and left simultaneously. That's why :1split doesn't suit -- unless there's a way to scroll two windows at the same time.

Thanks.

Conviction answered 20/11, 2009 at 21:21 Comment(0)
V
2

You can scroll two windows at the same time, so I think you can do what you want by splitting your window, and locking the scrolling behaviour. See :scrollbind and this tip for more details. Note that you have to lock each window in order that they move in sync.

Vinasse answered 20/11, 2009 at 21:23 Comment(3)
The is is a good tip, but the scrollbind suggestion needs a little tweaking. By default scrollbind will sync scrolling in the two windows vertically -- which is exactly what the OP doesn't want, and it won't sync the horizontal scrolling -- which is what the OP does want. The way to get desired behavior is to adjust the scrolloptions (:set sbo) so that 'ver' is not included and so 'hor' is included. Then windows will sync horizontally but not vertically.Variant
@Herbert - thx for the clarification. I'd not explicitly listed the options since I figured :scrollbind was a good starting point, but I think the extra info you've provided is worthwhile.Vinasse
Thanks a lot! Vim has great help system, but sometimes it's difficult to understand what do you actually need.Conviction
P
5

Thanks guys! Let me summarize the actual commands that did the job for me:

:1spl              # create an extra window with only 1 line 
:set scrollbind    # synchronize upper window
ctr+W , arrowDown  # switch to other window
:set scrollbind    # synchronize lower window
:set sbo=hor       # synchronize horizontally
Passport answered 13/3, 2014 at 7:41 Comment(0)
H
3

Split your window, decrease the top window height, set the top most line to be the first one and get back to the working window.

:split
:resize 1
gg
Ctrl-w w
Handily answered 28/8, 2012 at 17:22 Comment(1)
Thanks, this is useful to scroll down csv files while keeping the headers visible, is there an official way to :split and resize 1 in one command? It seems that :99sp does what I want. But I have set splitbelow, and set splitright in my .vimrc so it may behave differently on your system.Tevet
V
2

You can scroll two windows at the same time, so I think you can do what you want by splitting your window, and locking the scrolling behaviour. See :scrollbind and this tip for more details. Note that you have to lock each window in order that they move in sync.

Vinasse answered 20/11, 2009 at 21:23 Comment(3)
The is is a good tip, but the scrollbind suggestion needs a little tweaking. By default scrollbind will sync scrolling in the two windows vertically -- which is exactly what the OP doesn't want, and it won't sync the horizontal scrolling -- which is what the OP does want. The way to get desired behavior is to adjust the scrolloptions (:set sbo) so that 'ver' is not included and so 'hor' is included. Then windows will sync horizontally but not vertically.Variant
@Herbert - thx for the clarification. I'd not explicitly listed the options since I figured :scrollbind was a good starting point, but I think the extra info you've provided is worthwhile.Vinasse
Thanks a lot! Vim has great help system, but sometimes it's difficult to understand what do you actually need.Conviction
A
0

Attempted to automate it with a script in "<leader>fr"

https://github.com/PxCxio/vim-freeze-row/tree/master/doc

Did a version for nvim seems to be working smoothly

Appellant answered 22/8 at 2:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.