Pages

Tuesday, May 14, 2024

Mastering Navigation in Vi/Vim: Line Numbers and Efficient Movement

How to Enable Line Numbers

To start using line numbers in vi or vim, you'll first need to open your file with vi by typing:

bash
vi filename

Once you’re in, you might notice that line numbers are not displayed by default. To enable them, ensure you are in normal mode (not insert mode), press the Esc key, and type:

arduino
:set number

Press Enter to execute this command. You should now see line numbers appearing on the left side of your editor window.

Making Line Numbers Permanent

If you find line numbers useful and would like them to appear every time you open a file, you can make this setting permanent by adding it to your vimrc configuration file:

  1. Open the vimrc file in vi:

    bash
    vi ~/.vimrc
  2. Add the following line:

    arduino
    set number
  3. Save and close the file.

This will ensure that line numbers are always enabled whenever you use vi or vim.

Navigating to a Specific Line

Knowing how to jump directly to a specific line can save you time, especially when working with large files. Here’s how you can do it:

  • Using the G Command: Press Esc to go to normal mode, type the line number you want to go to followed by G. For example, 100G takes you to line 100.

  • Using the Command-Line Mode: Alternatively, you can use the command-line mode by typing : followed by the line number. For example, :100 will also take you directly to line 100.


No comments:

Post a Comment