Skip to content

Enabling Horizontal Scrolling

Josh Peterson edited this page Jul 5, 2024 · 1 revision

Introduction

The Typewriter.nvim plugin for Neovim enhances the editing experience by keeping the cursor centered on the screen while typing or navigating through code. With the recent addition of the enable_horizontal_scroll configuration option, users can now also enjoy horizontal scrolling. This feature centers the cursor horizontally within the window, providing a more focused and distraction-free coding environment.

How to Enable Horizontal Scrolling

To enable horizontal scrolling in Typewriter mode, you need to set the enable_horizontal_scroll option to true in the plugin configuration.

Step-by-Step Guide

  1. Open your Neovim configuration file (init.vim or init.lua):

    nvim ~/.config/nvim/init.vim

    Or for Lua configuration:

    nvim ~/.config/nvim/init.lua
  2. Set the enable_horizontal_scroll option:

    If you are using init.vim, add the following lines:

    lua << EOF
    require('typewriter').setup({
        enable_horizontal_scroll = true
    })
    EOF

    If you are using init.lua, add the following lines:

    require('typewriter').setup({
        enable_horizontal_scroll = true
    })
  3. Save and close your configuration file.

  4. Restart Neovim to apply the changes:

    :source ~/.config/nvim/init.vim

    Or for Lua configuration:

    :source ~/.config/nvim/init.lua

How It Works

When enable_horizontal_scroll is set to true, the plugin ensures that the cursor remains horizontally centered within the window as you navigate through your code. This is particularly useful when working with long lines of code or text, as it reduces the need to manually scroll horizontally to keep the relevant portion of the line in view.

Function Details

The core functionality for horizontal scrolling is implemented in the center_cursor_horizontally function. Here is an overview of how it works:

  1. Get the current window width:

    local win_width = vim.api.nvim_win_get_width(0)
  2. Get the current cursor column position:

    local cursor_col = vim.fn.virtcol '.'
  3. Calculate the left column position to center the cursor horizontally:

    local left_col = math.max(cursor_col - math.floor(win_width / 2), 0) - 1
  4. Disable line wrapping to ensure horizontal scrolling:

    vim.api.nvim_win_set_option(0, 'wrap', false)
  5. Set the window view to the calculated left column position:

    vim.fn.winrestview { leftcol = left_col }

This function is integrated into the CursorMoved and CursorMovedI autocommands to ensure it is triggered whenever the cursor moves.

Example Configuration

Here is a complete example configuration for enabling Typewriter mode with horizontal scrolling:

init.vim

lua << EOF
require('typewriter').setup({
    enable_with_zen_mode = true,
    enable_with_true_zen = true,
    keep_cursor_position = true,
    enable_notifications = true,
    enable_horizontal_scroll = true,
})
EOF

init.lua

require('typewriter').setup({
    enable_with_zen_mode = true,
    enable_with_true_zen = true,
    keep_cursor_position = true,
    enable_notifications = true,
    enable_horizontal_scroll = true,
})

Conclusion

The addition of horizontal scrolling to Typewriter.nvim provides users with an even more immersive and focused coding experience. By centering the cursor both vertically and horizontally, distractions are minimized, allowing you to concentrate on the code that matters.

If you have any questions or encounter any issues, please refer to the README or open an issue on the GitHub repository.