1

I am beginning to use Vim for C++ development.

Many of my lines are within deep for loops or if conditions that with indenting and wrapping disallowed gives a structure that looks like this. x here is the cursor position.

 ___________________________________
 a window in a tab                  |
    {                               |
           {                        |
                //Really Long line  |                                
                     //Another Long.|
        x                      //Lo.|  
                                    |
____________________________________|

There are multiple lines that extend beyond the window's right margin (especially when there are vertical splits) and I have to scroll to the right to see the line in its entirety.

Is it possible to set up a command that can be toggled, dimensions increased or decreased that allows one to view the display of a window in a tab not from column 0 but from, say, column 10 to the right like so?

 ___________________________________
 a window in a tab                  |
                                    |
 {                                  |
    //Really Long line              |                                
         //Another Long line        |
 x                 //Long line      |  
                                    |
____________________________________|

I tried using repeated zl's (scroll right) to reach my desired scroll position. The trouble, however, is that whenever an empty line is encountered on pressing j or k (scrolling down or up), the display goes back to column 1 and the horizontal scrolled position is lost.

5
  • 1
    Are you using tabs or spaces? If tabs, you can shrink the visual display width of a tab character.
    – D. Ben Knoble
    Commented Sep 5, 2021 at 11:33
  • @D.BenKnoble I use the default setting provided by youcompleteme plugin. Does that answer your question? I will have to look into those default values. I format my code using clang-format with the following settings: Standard: Cpp11, BasedOnStyle: LLVM, IndentWidth: 4, ColumnLimit: 0 I am unable to make out whether this corresponds to tab or spaces. Is there an easy check to figure out if my code uses tab or spaces within vim? If tab display width can be reduced, I should possibly force format and convert space to tabs it seems.
    – Tryer
    Commented Sep 5, 2021 at 11:39
  • 1
    Well, i prefer spaces, but if they were tabs you could make them display however wide you want. You can use the list and listchars options to see if they are tabs or spaces.
    – D. Ben Knoble
    Commented Sep 5, 2021 at 11:46
  • I did some digging along the lines you suggested and it appears that the clang-format above gives me 4 spaces. There are no tabs in my code. I will have to do some experimenting to figure out how to format using tabs and how subsequently my code behaves. Is stackoverflow.com/questions/2054627/… a good link to follow to set tab sizes?
    – Tryer
    Commented Sep 5, 2021 at 12:05
  • it’s alright—i would use an ftplugin instead of a filetype autocommand though.
    – D. Ben Knoble
    Commented Sep 5, 2021 at 12:53

1 Answer 1

1

Is it possible to set up a command that can be toggled, dimensions increased or decreased that allows one to view the display of a window in a tab not from column 0 but from, say, column 10 to the right like so?

There is no built-in way to do it afaik.

I tried using repeated zl's (scroll right) to reach my desired scroll position. The trouble, however, is that whenever an empty line is encountered on pressing j or k (scrolling down or up), the display goes back to column 1 and the horizontal scrolled position is lost.

Here you can use :h 'virtualedit' as set ve=all. Having it your cursor would not jump to column 1 when you move cursor up/down.

BTW, there are

  • zL move the view half a screenwidth to the right
  • zH move the view half a screenwidth to the left
  • zs scroll text horizontally making your cursor positioned on the very left of the window. (and ze is the opposite)

you can add to your zl.

There are also gm and gM to position your cursor on the half of screenwidth/linewidth. And probably more exists :)

3
  • virtualedit indeed seems quite powerful and useful in this context. The other option would have been for me to force wrap long lines, but understanding my own code with wrap turned on is quite difficult.
    – Tryer
    Commented Sep 5, 2021 at 11:24
  • 1
    yep, and instead of zl, try to position your cursor with regular movements and press zs
    – Maxim Kim
    Commented Sep 5, 2021 at 11:29
  • 1
    Yes, just tested zs out and it is very useful for right horizontal scrolling when combined with ve=all
    – Tryer
    Commented Sep 5, 2021 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.