Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Go to a specific line in VIM?

    I'm editing some configuration files that can tend to get very long. Oftentimes I do make mistakes, and the program for which I am writing these files (Nagios) will tell me on what line the mistake is when I try to verify my configuration data. I'm getting somewhat tired of scrolling with the d-pad to line 656. Is there a command to jump to a particular line number in the VIM editor?



  2. #2
    There are many good VIM tutorial on the web, just type VIM tutorial in google. Here is an expert from this one :

    Even faster

    If you want to go to the end of the file, press G. If you want to go to the beginning of the file, press 1G.

    If you want to go to the nth line, type the number n followed by G. For example, 150G takes you to the 150th line. If you don't have as many as 150 lines, you won't be punished -- the cursor just jump to the last line.

    There is an alternative command to jump to a line if you know its number. Type the colon character : followed by the line number and ENTER, such as :150. This has the same effect as the G command, but the advantage is that the colon and the line number you have typed are shown at the bottom of the screen.
    But how do we know the number of a line? Usually we don't know. Therefore, usually we only use G and 1G, which take us to the end or the beginning of the file. Of course, you may have an estimate of the line number where you want to go. Then just go there and then use j k to adjust the cursor location further.

    If you do want to use line numbers precisely, you can type :set number to let vim display line numbers on the screen. The line numbers on the screen are not part of your file and can be hidden by type :set nonumber.

    A good place to use line numbers is when you compile and debug programs. The compiler usually report the number of the lines where errors occur. Using those numbers you can jump directly to the error spot.

    Move by screen.
    Press Ctrlf moves you forward by one screen of text, and Ctrlb moves you backward by one screen of text.

    Ctrlf and Ctrlb are faster, but pressing Ctrl key can be tiring. To avoid that, you may use the following commands to map Space key and Backspace key to them respectively.

    :map <space> <c-f>
    :map <backspace> <c-b>

    Move to the matched bracket
    The % command is a special cursor motion command. When the cursor is at any one of the bracket characters ( ) [ ] { }, hitting the % takes the cursor to its match, if there is one. If you're a C programmer, you'll find this very useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts