303

How do I go to first line in a file in vim ?

3
  • 20
    Feel like this question needs the complementary fact that G will go to the last line and G$ will go to the end of the file.
    – User
    Commented May 6, 2014 at 5:05
  • @User so whats the difference between G vs G$? Commented Jan 20, 2017 at 23:55
  • 5
    @CharlieParker: G takes you to the beginning of the last line, G$ takes you to the end of the last line.
    – User
    Commented Jan 21, 2017 at 8:13

5 Answers 5

514

In command mode (press Esc if you are not sure) you can use:

  • gg,
  • :1,
  • 1G,
  • or 1gg.
4
  • 4
    gg does a "soft" beginning of first line, if there is whitespace you'd need to do gg0 Commented Sep 13, 2019 at 4:34
  • There has to be a better way to go to the true start of the file than gg0, if the first line starts with whitespaces. Commented Dec 25, 2022 at 16:32
  • Found a true better way to do a "hard" beginning of first line, and made an answer out of it - see the answer below for the details and (potential, but extremely unlikely) restrictions. Commented Dec 25, 2022 at 16:41
  • 1
    @RalphCallaway or just type go, see my answer below and save yourself a keystroke (RE: gg0). Commented Apr 1 at 17:00
64

Type "gg" in command mode. This brings the cursor to the first line.

2
  • 2
    why does gg do that? I've found usually vim commands are composed of others so maybe there is a reason its double g than single g. Commented Jan 20, 2017 at 23:57
  • 1
    In ˋLessˋ: ˋgˋ beginning file, ˋGˋ end file
    – Timo
    Commented Feb 17, 2018 at 16:40
16

Go to first line

  • :1

  • or Ctrl + Home

Go to last line

  • :%

  • or Ctrl + End


Go to another line (f.i. 27)

  • :27

[Works On VIM 7.4 (2016) and 8.0 (2018)]

5

Solution

Move to the true starting character of the file, even if it is a whitespace character, by typing only two characters:

go

Go to [count] byte in the buffer. Default [count] is one, start of the file.

It's arguably even easier to remember than gg (e.g., mnemonic: "Just go to the real beginning of the file, already!"), but perhaps not as comfortable as gg in terms of minimal finger motion (or the ability to use a single finger on one hand to drum this particular "cursor motion beat.")

Unfortunately gg has the "limitation" that it does not go to the true beginning character of the first line when this character and perhaps the characters that follow happen to be whitespaces.

The following other options have the same limitation:

  • 1G
  • :1<CR>
  • <Ctrl>-<Home>

Alternatives

Along with gg0, typing <Ctrl>-<Home> <Home> does perform the desired cursor motion to the true first character of the file. It seems somewhat antithetical to ViM principals to use a combination like the latter alternative, but who knows, there may be those that prefer this particular (logical) two key-stroke variation to go, which is why I present it as an potential alternative.

An Aside

Somewhat perplexingly, <Ctrl>-<End> does position the cursor at the true last character of the file, whitespace or not, and is probably the only single (logical) key stroke to do so. Personally, I prefer G$ (or the far more contrived Gg_ to position the cursor at the last non-whitespace character of the file), but if my fingers just happen to be "on the long road home after visiting other relatives in that far away neighborhood of the keyboard and there is a pressing need to stop for gas at the end of the file," well, you just might catch me "breaking the rules" and striking <Ctrl>-<End> to "get the job done."

Limitations

The only pre-requisite for go to work is that ViM is compiled with the byte_offset feature enabled (i.e., +byte_offset). Fortunately, this feature is enabled by default.

To check your particular build of ViM for this feature, you can use the :ve[rsion] command or vim --version from the command line. Both of these display the values of all of the build-time features that were in effect when the instance of ViM being tested was compiled. It is a good idea to commit these all-feature query commands to memory, btw, since the ViM help often refers to feature based restrictions while explaining commands and topics whose availability and/or effect is build feature controlled or restricted.

1
  • This is the way!
    – daviewales
    Commented Jul 2 at 4:42
4

If you are using gvim, you could just hit Ctrl + Home to go the first line. Similarly, Ctrl + End goes to the last line.

1
  • 9
    Too much hand movement.
    – wingerse
    Commented Jul 8, 2016 at 11:48

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.