Jean
Jean

Reputation: 22685

Go to first line in a file in vim?

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

Upvotes: 306

Views: 277092

Answers (5)

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74360

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.

Upvotes: 7

DimiDak
DimiDak

Reputation: 5601

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

Upvotes: 17

Lazer
Lazer

Reputation: 94810

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

  • gg,
  • :1,
  • 1G,
  • or 1gg.

Upvotes: 518

Bharat Lagali
Bharat Lagali

Reputation: 41

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

Upvotes: 4

andrew
andrew

Reputation: 2879

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

Upvotes: 65

Related Questions