ChatGPT解决这个技术问题 Extra ChatGPT

Move cursor to end of file in vim

vim

When I want the cursor to go to the end of the file (i.e. the end of the last line) in Vim, I have to type six keystrokes:

<ESC>G$a - which translates to ESC + Shiftg + Shift4 + a on my keyboard layout.

How can I do this more efficiently?

Since I regularly work on many different machines, I cannot always change .vimrc, so I'm looking for answers without having to edit or create that file.

Just want to point out, the commands you're showing should be represented as follows: <esc>G$a, you show the shift by making the letter uppercase. And you show a $ instead of shift + 4, since no matter the keyboard the function of this key ($) won't change, but if you do shift + 4 then the meaning can change. :)
@Eduan I generally agree, but in this case actualy keypresses are also relevant. I added both :-)
@Eduan For the question of efficiency, the actual commands don't matter. The keystrokes do. And since there are huge differences in keyboard layout between languages, I don't know where the $ is on a US keyboard, and it might well be that for Americans entering the command $ is just one keystroke. I wanted to explicitly show that for me (with a German keyboard) it is two keystrokes.
@what IC. I can understand that. Just wanted to point it out so in the future you know. :)

M
Maxime Lorant

This is quicker. Just use this

:$

This doesnt move to the last column only to the last line
If you just want to quickly go to the end of file and your keyboard shortcuts are messed up, this is the right command. Thanks!
:$ + End would do it in that case.
This requires at the end as well as still needing to get to the end of the line which makes it no quicker.
:$ does the trick ! going to the end of file as the question asked ! thanks
t
timss

No need to explicitly go to the end of line before doing a, use A;
Append text at the end of line [count] times

<ESC>GA


If you want to stay in normal mode try G) (or <ESC>G) if you're in insert mode and want to end in normal mode over the last character). Same number of keystrokes, but to end up in normal mode instead of insert mode. I find this useful occasionally when I want to run some normal command on the last word instead of an insert mode command or edit.
@Alejandro While useful, ) moves on [count] sentences forward and will therefore not go to end of line if for example the last line is This is a test. Foo bar.. It will work for a lot of code though, which is often not considered sentences and will therefore go to the end anyway.
If you plan to write the next line, <ESC> G o will do the carriage return and put you in insert mode on the next line (at the end of the file), saving a couple more keystrokes.
Also if you don't want to switch to insert mode use G$.
P
Prasanth Ganesan

Hit Esc and then press: Shift + G


This goes to the beginning of the last line, not the end of the file.
this goes to the end of the file for me, and is a nice easy keybind too
G keeps the column number the same (if possible) and moves to the last line. Jacob was probably at the end of the line, and Jordan at the front when running the command.
j
jvriesem

For starters, there's no need for the return. G$ will do. And you're being misleading by counting <Esc> and a in the length of a normal mode command.

However, you can use Ctrl + End if you like.


I have not End-Key on my keyboard (if that is what you mean).
Actually, the answer by timss shows that I did well to count <ESC>and a, because he or she knew of a way to replace Shift+$, a with A.
@what most laptop keyboard nowadays have home/end put in up/down/left/right along with Fn if they don't have dedicated home/end keys
f
fuat

Go to Top - Double g

Go to Bottom - Shift + g


Excellent. Works on Mac too.
Takes the cursor to beginning of line in both of above cases
j
jvriesem

I thought the question was "Move cursor to end of file in vim" ?

End-of-file: Esc + G
Begin-of-file Esc + g (or gg if you are already in the command area)


g
greduan

The best way to go to the last line of the file is with G. This will move the cursor to the last line of the file.

The best way to go to the last column in the line is with $. This will move the cursor to the last column of the current line.

So just by doing G$ you get to the end of the file and the last line.

And if you want to enter insert mode at the end of the file then you just do GA. By doing this you go to the last line of the file, and enter insert mode by appending to the last column. :)


b
broc.seib

If you plan to write the next line, ESCGo will do the carriage return and put you in insert mode on the next line (at the end of the file), saving a couple more keystrokes.


S
Sandeep Amarnath

Try doing SHIFT + G and you will be at the end of the page, but you can't edit yet. Go to the top by doing G + G


G
Gili

Ctrl + Home = Jump to start of file

Ctrl + End = Jump to end of file


easy to remember
D
D. Ben Knoble

You could map it to a key, for instance F3, in .vimrc

inoremap <F3> <Esc>GA

While possible, this is generally a bad idea. Instead aim for muscle memory and use GA or G$.
I'm still winning with 1 key stroke though :) On a more serious note, if you really need something fast in vim, learning how to .vimrc is useful.
It would also be good to have a complete map example instead of telling the OP to go look it up. Since if you can't find it how is the OP supposed to?
You're not "winning" anything since you'd have to move away from your homerow. Additionally, you can't use this on someone elses config or even vi, if you happen to use it. You're really not saving any time. If anything, use C-end as mentioned by hobbs.
I agree with the comments here, but I cleaned up the mapping for you
X
Xinyi Xiang

I have been looking for the way to move the cursor to the end of a line and then edit on a mac terminal, here was how it worked for me. Exit your edit mode, type in g_(that's 'g' then underscore with no colon in the front), then simply press the 'a' key so you could "add" code to your scripts


Were you perhaps looking for A?
that’s odd, A is basically equivalent to $a
S
Sergiy Kolodyazhnyy

Another alternative:

:call cursor('.',strwidth(getline('.')))

This is may be useful if you're writing a function. Another situation would be when I need to open a specific template and jump to end of first line, I can do:

vi +"call cursor('.',strwidth(getline(1)))"  notetaking_template.txt

The above could also be done with

vi +"execute ':normal! $'" notetaking_template.txt

See also:

vim - Get length of the current line


S
SergioAraujo

If you want to paste some clipboard content at the end of the file type:

:$ put +

$ ............ last line
put .......... paste 
+ ............ clipboard

M
Mostafa Wael

To go to the last line and preserve the column you are already in: Shift + g

To go to the end of the line after this move: Shift + $

We can combine those moves using Shift + g + $

Remember that g + g will return you to the beginning of the file.


O
Okamiram

All you have to do is press ESC then SHIFT G, the cursor will move to the end of the file.