Skip to content

TW `keep_cursor_position` Feature

Josh Peterson edited this page Jun 29, 2024 · 1 revision

Overview

The keep_cursor_position feature in Typewriter.nvim ensures that the cursor remains in the same position relative to the text when the view moves. This feature is particularly useful when navigating through code, as it keeps your focus on the current line or block without shifting the cursor's position.

Enabling the Feature

To enable the keep_cursor_position feature, include it in the configuration when setting up Typewriter.nvim. Here’s how you can enable it:

-- ~/.config/nvim/init.lua
require('typewriter').setup {
  keep_cursor_position = true,
}

How It Works

When keep_cursor_position is enabled, the view is centered around the cursor using the zz command, but the cursor itself does not move. This behavior is maintained during normal, insert, and visual modes. The feature is implemented using autocommands that trigger the centering action whenever the cursor moves.

Configuration

By default, keep_cursor_position is set to true. You can customize this behavior by modifying the configuration as follows:

-- ~/.config/nvim/init.lua
require('typewriter').setup {
  keep_cursor_position = true,  -- Keep the cursor in the same position relative to the text
}

Commands and Functions

Typewriter.nvim provides several commands and functions to control typewriter mode and centering behavior:

  • TWEnable: Enables typewriter mode.
  • TWDisable: Disables typewriter mode.
  • TWToggle: Toggles typewriter mode on and off.
  • TWCenter: Centers the view around the current code block or function.

These commands can be used directly in Neovim or mapped to keys for easier access.

Example Key Mappings

You can create key mappings for these commands to quickly enable, disable, or toggle typewriter mode and to center the view:

-- ~/.config/nvim/init.lua
vim.api.nvim_set_keymap('n', '<leader>te', ':TWEnable<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>td', ':TWDisable<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>tt', ':TWToggle<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>tc', ':TWCenter<CR>', { noremap = true, silent = true })

Use Case

The keep_cursor_position feature is especially beneficial when you want to maintain focus on a particular line or block of code while navigating. It prevents the cursor from jumping to different positions, which can be distracting and disrupt your workflow.

Summary

The keep_cursor_position feature in Typewriter.nvim enhances your coding experience by keeping the cursor steady while the view is centered. This feature, combined with the commands provided by Typewriter.nvim, allows for a more focused and efficient workflow in Neovim. Enable this feature to enjoy a distraction-free coding environment.

For more information on configuring and using Typewriter.nvim, refer to the official documentation and explore additional customization options to tailor the plugin to your needs.