How do I move to end of line in Vim?
Asked Answered
L

16

1422

I know how to generally move around in command mode, specifically, jumping to lines, etc. But what is the command to jump to the end of the line that I am currently on?

Lahdidah answered 19/9, 2008 at 21:19 Comment(1)
Basic vim commands covered in less than 10 minutes youtube.com/watch?v=71YTkxUNwmgDisaccharide
K
2433

Just the $ (dollar sign) key. You can use A to move to the end of the line and switch to editing mode (Append). To jump to the last non-blank character, you can press g then _ keys.

The opposite of A is I (Insert mode at beginning of line), as an aside. Pressing just the ^ will place your cursor at the first non-white-space character of the line.

Kenn answered 19/9, 2008 at 21:20 Comment(7)
Jump to last non blank g_Distrain
The _g variant is really useful when working in visual mode, as it allows you to use the line without any newline at the end.Wearisome
While Home and End keys work on a regular keyboard, $ and ^ are needed when you're using a laptop :)Combustible
You should add ^to the list.Panoply
If your line wraps, you can use g$ to reach the end of the current wrap.Acquire
"You can use A to move to the end of the line" is misleading. Perhaps rephrase? And/or include some prerequisites (if that is the case).Loxodrome
OK, Shift + ALoxodrome
G
356

As lots of people have said:

  • $ gets you to the end of the line

but also:

  • ^ or _ gets you to the first non-whitespace character in the line, and
  • 0 (zero) gets you to the beginning of the line incl. whitespace
Gregarious answered 19/9, 2008 at 22:22 Comment(3)
I knew | gets us to the beginning of the line but I was not aware of 0. So both 0 and | do exactly the same thing, yeah?Merlon
@kami: whilst the descriptions are different they do appear to behave identically. However the pipe | command can take a count, e.g. 3| to jump to the third column.Additive
wow, just learn that _ does the same thing as ^Disheveled
A
151
  • $ moves to the last character on the line.
  • g _ goes to the last non-whitespace character.

  • g $ goes to the end of the screen line (when a buffer line is wrapped across multiple screen lines)

Additive answered 28/9, 2010 at 11:4 Comment(9)
I feel like g_ is the same thing as $bRadiocarbon
@DaveAaronSmith: b goes to the beginning of the previous word which is not the same thing at all. It's roughly equivalent to $ge.Additive
I use _ and g_ for visual/yanking, as $ will also copy/delete the new line (LN) character.Didi
For me, $ goes to the end of the current screen line (when wrapped). I haven't managed to figure out why. g_ is the only way I found to get to the actual end of the line.Szymanski
@Szymanski $ goes to the end of the line even when wrapped for me. I know of no setting to adjust its behaviour. Perhaps it's been remapped by your .vimrc to g$?Additive
@PaulRuane- You nailed it. I use spf13, and .vimrc line 310 maps it. Now the question becomes how do I unmap it later in the load chain (just changing this line will be overwritten next time I pull spf13).Szymanski
I figured it out. I had to add let g:spf13_no_wrapRelMotion = 1 to my .vimrc.before.local file to disable this behaviour, which is specific to spf13.Szymanski
I like to nnoremap L g_ and nnoremap <leader>L L, also in operator pending and visual modes. More intuitive and less of a stretch.Alwin
is _ same as ^ for first non-blank character at the beginning of the line?Labana
A
92

The main question - end of line

$ goes to the end of line, remains in command mode

A goes to the end of line, switches to insert mode

Conversely - start of line (technically the first non-whitespace character)

^ goes to the start of line, remains in command mode

I (uppercase i) goes to the start of line, switches to insert mode

Further - start of line (technically the first column irrespective of whitespace)

0 (zero) goes to the start of line, remains in command mode

0i (zero followed by lowercase i) goes the start of line, switches to insert mode

For those starting to learn vi, here is a good introduction to vi by listing side by side vi commands to typical Windows GUI Editor cursor movement and shortcut keys.

vi editor for Windows users

Authoritative answered 7/2, 2015 at 11:2 Comment(0)
Y
53

I can't see the hotkey for a MacBook for using Vim in the standard terminal.

For macOS users (tested on a MacBook Pro 2018):

fn + - move to the beginning of the line

fn + - move to the end of the line

fn + - move a page up

fn + - move a page down

fn + g - move the cursor to the beginning of the document

fn + shift + g - move the cursor to the end of the document

For the last two commands, it sometimes needs to be tapped twice.

Yvette answered 8/5, 2019 at 14:58 Comment(3)
The first 4 commands also work in Insert mode, which is of a great help! Many thanks Alexander!Nuclei
What version of Vim? Installed in what way?Loxodrome
Isn't gg move cursor to the top of document and G move cursor to the bottom of the document?Puncheon
B
49

If your current line wraps around the visible screen onto the next line, you can use g$ to get to the end of the screen line.

Binnings answered 19/9, 2008 at 22:49 Comment(3)
I just discovered pre-pending the up and down commands with g make vim move the cursor screen-based, rather than line-based. I.e. gj and gk allow you to move up and down on a long line. Really useful.Landgravine
Also discovered that typing gw and something seems to change j and k permanently to behave like this. Not sure how or why. Can anyone explain this?Landgravine
@Druckles: A bit of a late reply, but if you're still wondering, (or if anyone else reading this is) gw and gq are commands that reformat text. (see :h gw and :h gq) What probably happened was you did gw{motion} and changed some text so that the screen lines corresponded to the actual lines.Custard
D
23

The dollar sign: $

Division answered 19/9, 2008 at 21:20 Comment(0)
I
20

Press A to enter edit mode starting at the end of the line.

Inflow answered 19/9, 2008 at 21:20 Comment(2)
It doesn't jump to the end. It stays (or rather the cursor moves one to the right). Tried on Vim 8.0.3741 (stock installation on Ubuntu).Loxodrome
OK, Shift + ALoxodrome
A
16

The advantage of the 'End' key is it works in both normal and insert modes.

'$' works in normal/command mode only but it also works in the classic vi editor (good to know when vim is not available).

Acquittal answered 19/9, 2008 at 22:5 Comment(0)
T
14

Also note the distinction between line (or perhaps physical line) and screen line. A line is terminated by the End Of Line character ("\n"). A screen line is whatever happens to be shown as one row of characters in your terminal or in your screen. The two come apart if you have physical lines longer than the screen width, which is very common when writing emails and such.

The distinction shows up in the end-of-line commands as well.

  • $ and 0 move to the end or beginning of the physical line or paragraph, respectively:
  • g$ and g0 move to the end or beginning of the screen line or paragraph, respectively.

If you always prefer the latter behavior, you can remap the keys like this:

:noremap 0 g0
:noremap $ g$
Tuttifrutti answered 17/12, 2010 at 18:31 Comment(2)
How to get to the last non-whitespace of the line? (whether it's a screen line or physical line)Merlon
g_ is last non-whitespace of the physical line. How about screen line?Droll
C
13

In many cases, when we are inside a string we are enclosed by a double quote, or while writing a statement we don't want to press escape and go to end of that line with arrow key and press the semicolon(;) just to end the line. Write the following line inside your vimrc file:

imap <C-l> <Esc>$a

What does the line say? It maps Ctrl+l to a series of commands. It is equivalent to you pressing Esc (command mode), $ (end of line), a (append) at once.

Cloistered answered 27/11, 2010 at 7:4 Comment(6)
thank you for that one. I have been wanting to add Ctrl-e back to my vim as go to end of line like on my first machine and didnt know howOldenburg
As the accepted answer says, you can press A to do the same thing.Droll
@Droll you misunderstood, this doesn't move away from editing mode which is quite nice.Verdha
You're right. vim certainly allows you to do much more in much less time with things like this. @razorxpress, a minor adjustment would be imap <C-l> <Esc>A, which makes Ctrl+l the equivalent of pressing Esc (exit insert mode), then A (append to end of line).Droll
I think the more idiomatic way to map this would be inoremap <C-l> <C-o>AFastigium
I fail to understand how two key presses (Ctrl+l) is better than ...two keystrokes which come inbuilt (<Esc>A).Colver
G
9

Or there's the obvious answer: use the End key to go to the end of the line.

Gilpin answered 19/9, 2008 at 21:35 Comment(7)
Yeah, mac terminal is one I've noticed that hijacks keys and doesn't pass them along to the stuff running inside terminal. One of the things that annoy me whenever I use a mac for a bit.Gilpin
The whole point of using vim is not having to use keys like 'End'. Never leave home row.Issue
On Mac there is no End key.Bushido
@Bushido There was when I posted the comment...but Apple likes to remove features over the years. netdna.webdesignerdepot.com/uploads/2009/01/… this is actually one of my favorite keyboards of all timeGilpin
@JonThoroddsen it was much easier to not leave home row when Esc on terminals was not placed so far away.Dicast
@kenorb: There is on the AEKII (though the legend is not literally "End", like for PgUp and PgDn).Loxodrome
Yes, this is perfectly fine for casual use. Especially on non-American keyboard layouts (no need to use modifier keys).Loxodrome
A
9

Possibly unrelated, but if you want to start a new line after the current line, you can use O anywhere in the line.

Anabelanabella answered 1/10, 2008 at 18:47 Comment(0)
E
3

The easiest option would be to key in $. If you are working with blocks of text, you might appreciate the command { and } in order to move a paragraph back and forward, respectively.

Endurance answered 29/7, 2014 at 13:53 Comment(0)
S
2

I was used to Home and End getting me to the start and end of lines in insert mode (from use in Windows and I think Linux), which Mac doesn't support.

This is particularly annoying because when I'm using Vim on a remote system, I also can't easily do it. After some painful trial and error, I came up with these .vimrc lines which do the same thing, but bound to Ctrl + A for the start of the line and Ctrl + D for the end of the line. (For some reason, Ctrl + E I guess is reserved or at least I couldn't figure a way to bind it.) Enjoy.

:imap <Char-1> <Char-15>:normal 0<Char-13>
:imap <Char-4> <Char-15>:normal $<Char-13>

There's a good chart here for the ASCII control character codes here for others as well:

ASCII Table

You can also do Ctrl + V, + Ctrl + <Letter> you want to bind or execute as well, but that doesn't paste as well to places like this.

Shakedown answered 2/8, 2014 at 22:0 Comment(0)
S
0

from the command line you can specify to start at the end of the line with the $ symbol

i.e.

unixSYSblah$ vi +$ any.txt

Takes you to the last line. +30 would take you to line 30.

Strive answered 13/3 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.