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?
15 Answers
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.
-
102
-
10The _g variant is really useful when working in visual mode, as it allows you to use the line without any newline at the end. Jul 31, 2012 at 7:52
-
3While Home and End keys work on a regular keyboard, $ and ^ are needed when you're using a laptop :) Nov 12, 2012 at 22:26
-
1
-
3If your line wraps, you can use
g$
to reach the end of the current wrap. Sep 13, 2014 at 22:09
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
-
1I 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? May 18, 2011 at 20:23
-
8@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. Nov 21, 2011 at 21:57 -
- $ 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)
-
-
4@DaveAaronSmith:
b
goes to the beginning of the previous word which is not the same thing at all. It's roughly equivalent to$ge
. Mar 29, 2012 at 8:23 -
9I use
_
andg_
for visual/yanking, as$
will also copy/delete the new line (LN) character.– ErnestMay 8, 2014 at 13:08 -
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.– ezukJan 21, 2015 at 10:07 -
@ezuk
$
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 tog$
? Jan 21, 2015 at 10:51
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.
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.
-
4I 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.– DrucklesApr 11, 2012 at 17:29
-
1Also 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?– DrucklesApr 11, 2012 at 17:30
-
4@Druckles: A bit of a late reply, but if you're still wondering, (or if anyone else reading this is)
gw
andgq
are commands that reformat text. (see:h gw
and:h gq
) What probably happened was you didgw{motion}
and changed some text so that the screen lines corresponded to the actual lines.– PanduJul 14, 2013 at 18:50
I can't see hotkey for macbook for use vim in standard terminal. Hope it will help someone. For macOS users (tested on macbook pro 2018):
fn + ← - move to beginning line
fn + → - move to end line
fn + ↑ - move page up
fn + ↓ - move 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 sometime needs to tap twice.
-
The first 4 commands also work in Insert mode, which is of a great help! Many thanks Alexander!– Denis PMar 3 at 13:31
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).
-
43What's you hanging around insert mode for ?! No ordinary decent citizen goes there after daylight. You know what happens in there, don't you ? Code disappears there, y'know ? It disappears, and is never heard from again.– RookMay 20, 2009 at 22:59
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$
-
1How to get to the last non-whitespace of the line? (whether it's a screen line or physical line) May 18, 2011 at 20:45
-
g_
is last non-whitespace of the physical line. How about screen line?– trysisJul 15, 2015 at 21:36
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.
-
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 how Apr 27, 2012 at 14:36
-
1
-
4@trysis you misunderstood, this doesn't move away from editing mode which is quite nice.– MarconiOct 15, 2015 at 10:38
-
2You're right.
vim
certainly allows you to do much more in much less time with things like this. @razorxpress, a minor adjustment would beimap <C-l> <Esc>A
, which makes Ctrl+l the equivalent of pressingEsc
(exit insert mode), thenA
(append to end of line).– trysisOct 15, 2015 at 15:22 -
3I think the more idiomatic way to map this would be
inoremap <C-l> <C-o>A
– bennytyMay 2, 2017 at 19:40
Or there's the obvious answer: use the End key to go to the end of the line.
-
2Yeah, 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.– davrAug 31, 2011 at 23:57
-
The whole point of using vim is not having to use keys like 'End'. Never leave home row. Jan 7, 2015 at 14:12
-
10
-
1@kenorb 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 time– davrApr 23, 2017 at 2:21
-
@JonThoroddsen it was much easier to not leave home row when Esc on terminals was not placed so far away.– qwrFeb 21, 2022 at 1:07
Possibly unrelated, but if you want to start a new line after the current line, you can use o anywhere in the line.
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.
I was used to Home/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:
http://www.physics.udel.edu/~watson/scen103/ascii.html
You can also do Ctrl-V + Ctrl- as well, but that doesn't paste as well to places like this.