308

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. 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. Commented Jan 21, 2017 at 8:13

5 Answers 5

525

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

  • gg,
  • :1,
  • 1G,
  • or 1gg.
Sign up to request clarification or add additional context in comments.

4 Comments

gg does a "soft" beginning of first line, if there is whitespace you'd need to do gg0
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.
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.
@RalphCallaway or just type go, see my answer below and save yourself a keystroke (RE: gg0).
65

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

2 Comments

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.
In ˋLessˋ: ˋgˋ beginning file, ˋGˋ end file
17

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)]

Comments

7

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 Comment

This is the way!
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 Comment

Too much hand movement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.