How do I go to first line in a file in vim ?
5 Answers
In command mode (press Esc if you are not sure) you can use:
- gg,
- :1,
- 1G,
- or 1gg.
-
4
gg
does a "soft" beginning of first line, if there is whitespace you'd need to dogg0
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
Type "gg" in command mode. This brings the cursor to the first line.
-
2why does
gg
do that? I've found usually vim commands are composed of others so maybe there is a reason its doubleg
than singleg
. Commented Jan 20, 2017 at 23:57 -
1
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)]
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.
If you are using gvim, you could just hit Ctrl + Home to go the first line. Similarly, Ctrl + End goes to the last line.
-
9
G
will go to the last line andG$
will go to the end of the file.G
vsG$
?