How do I go to the end of a line in VS code without using the mouse or some key combinations?
Asked Answered
M

14

34

I am using VS Code to write some HTML. I noticed that the IDE will auto-insert some code for me. For example, if I want a <p> tag, VS Code will create <p></p> for me and the cursor will land in the middle (between the opening and closing paragraph tags). However, when I am done typing the content inside the <p> tags, typically, I use my mouse or the directional right-arrow to move to the end. Is there a way to not move my fingers from the typing positions (e.g. fdsa and jkl;) to go to the end of the line or tag, or would I always have to use the mouse or directional pad?

I find IDEs like what JetBrains provide do not have this limitation for certain languages. For example, in Python, if I want to print something using PyCharm, I can type (the closing single quote and right parenthesis are auto-added)

print('')

My cursor will land inside the single quotes. To simply get outside the closing parenthesis, I simply type in ' followed by ) and the IDE is smart enough to know to not place the single quote and closing parenthesis there (it's like using the right arrow twice to get outside the print statement).

WebStorm, like VS Code, has the "problem" when dealing with HTML. If I am inside an opening and closing <p> tag, and right next to the closing one </p>, simply typing <, /, p, > will not land me outside (as with Python and PyCharm). On Windows, I can press the End key or on Mac I can press fn+right to get to the end; but that requires breaking the flow and continuity of my hands in the typing positions (eyes have to be redirected too).

Any tips on how to be a more productive coder using VS Code or other modern IDEs with HTML? Are there plugins that we may use to address this problem?

Meteorite answered 22/7, 2020 at 20:49 Comment(1)
You can create a custom keybind or chorded keybind to whatever you like. No extensions required. Just press CTRL + k then CTRL + S to open your shortcuts. – Screwy
D
27

Simply go look it up or change it in your key binding settings:

File > Preferences > Keyboard shortcuts


It's named cursorLineEnd.

enter image description here

Same can be done for cursorLineStart of course.


Interfering other shortcuts can be changed or deleted as well in that menu.

Demarcation answered 22/7, 2020 at 21:39 Comment(0)
M
20

For me it is Fn + Right Arrow Key that's the quickest possible approach

Mariandi answered 18/1, 2023 at 6:15 Comment(1)
That should be the right answer, but at least in the terminal this does nothing. πŸ€¦β€β™‚οΈ VS Code keybindings fail again... – Unfailing
M
16

You can paste this code in your keybindings.json file in vs code

  • ctrl+RightArrow ....>>> for Move cursor to Line end
  • ctrl+LeftArrow ....>>> for Move cursor to Line start
  • alt+RightArrow .....>>> for moving cursor word by word
  • alt+LeftArrow ....>>> for moving cursor word by word

Just paste this code into your keybindings.json in vs code

[
{
        "key": "alt+right",
        "command": "cursorWordEndRight",
        "when": "textInputFocus && !accessibilityModeEnabled"
    },
    {
        "key": "alt+left",
        "command": "cursorWordEndLeft",
        "when": "textInputFocus && !accessibilityModeEnabled"
    },
    {
        "key": "ctrl+right",
        "command": "cursorLineEnd"
    },
    {
        "key": "ctrl+left",
        "command": "cursorLineStart"
    },
]

Make sure to adjust commas and brackets, if there is already some code.

Miscarriage answered 30/7, 2022 at 15:47 Comment(2)
File > Preferences > Keyboard Shortcuts. – Scourings
amazing, that's exactly what i needed. <3 – Impression
I
9

I often just use <CTRL> + β†’ (right arrow) a few times to quickly navigate past words and code blocks. It won't immediately get you to the end but if there isn't a ton of code after your current cursor location, a few quick uses of this keystroke can be faster than lifting your hands and checking with eyes to find the key.

Depending on what your keyboard layout is, this could be faster. Personally, if it's a big issue, I would second other posters here and add a custom keybind / hotkey to a lesser-used key nearby.

//Begin CAVEAT

I'm not mentioning just using the <END> key here as a solution purposefully, since you indicated that using that using keys too far from home row broke your flow. Depending on my keyboard layout, that is often the fastest option by far, however my current keyboard makes that a non-starter. Part of the issue here is that people's experience is so different based on what keyboard they're using, as well as hand size and dexterity. YMMV with any solutions we mention that isn't a custom keybind.

//END CAVEAT

Inspirational answered 22/7, 2020 at 22:36 Comment(1)
best solution, my opinion – Varlet
M
7

If you want to go to the end of a line, just press the End button on the keyboard, and press Home to go to start of a line.

Murillo answered 3/1, 2023 at 12:33 Comment(1)
It surprises me that this answer is not accepted as all standard-layout-keyboards have dedicated keys for these actions since before most of us were born. And for the stripped keyboards one can always create own shortcuts as mentioned by i-m-yoga – Psia
A
3
Cmd + Right

This will go to the end of you current line.

Tips:

  • Hold down shift to start a selection
  • Use Alt or Ctrl instead of Cmd to change the distance the cursor travels
Appreciative answered 22/7, 2020 at 21:23 Comment(1)
only on mac! In windows there is no alt+arrow – Gley
K
3

In "Preferences" "Keyboard Shortcuts" search for "cursorEnd". Click on the edit "pencil" and change the Keybinding in the popup to Tab Tab. It's tap tap on the Tab key and the cursor moves to the end of the line and your hands stay in typing position. My "End" key is not within pinky reach.

Katt answered 3/3, 2023 at 20:33 Comment(1)
I found that this impacts the tab key. When I try to hit just one tab, it's waiting for the next tab. I use Caps Lock Caps Lock instead. Thanks for the idea! – Trichromatic
G
2

Change the key binding for cursor down to Shift + Space. I didn't remap right Arrow for reasons I'll explain shortly, but I could have easily done that as well.

When I code, I like to use indented formatting. So, when I type <p> and </p> is automatically generated, I go one step further and press ENTER. By default, that causes </p> to move to the same indention level as <p> on the line below my cursor, and puts my cursor on an indented line below <p>. Gif for reference:

Shortcut to make an indented block:

Shortcut to make an indented block

That leaves my fingers basically on the home keys, because when I'm done in that level, I'll press Shift + Space to go to the line below, where the ending tag will be, and then I can press ENTER a couple times as normal and create a new tag or whatever else I'll be adding to the file.

If you would like to set up your key bindings like mine, or do something slightly different, here is what I did:

  • Ctrl + K Ctrl + S (to open key bindings)
  • Search for "cursorDown"
  • Highlight the row and press ENTER
  • Press Shift + Space
Gausman answered 19/10, 2021 at 20:37 Comment(0)
T
2

If you like to stick to the home row and do not want to use arrow keys, you can customize your keyboard shortcuts.

paste the below code to your keybindings.json file in vs code.

Feel free to customize these shortcuts by changing key combinations.

,{
    "key": "alt+l",
    "command": "cursorEnd",
    "when": "editorTextFocus"
  },
  {
    "key": "alt+j",
    "command": "cursorHome",
    "when": "editorTextFocus"
  }
Tropophilous answered 6/9, 2022 at 20:14 Comment(0)
S
1

No need to do these messy things, just use your keyboard's home key and end key

  1. Press the home key to go to the beginning of the line

  2. Press the end key to go to the end of the line

Stillman answered 19/1, 2023 at 16:23 Comment(0)
S
0

I'm not sure which OS you specifically use being that you mentioned but an OS agnostic approach is to create your own keybind / key chord (sequence).

A key chord is essentially a way to use another 'layer' of key shortcuts, if you use your imagination for lingo. To illustrate: consider CTRL + S is a keyboard shortcut. Now consider CRTL + K chord CTRL + S, which is a completely different shortcut even though you use the same sequence.

Consider an edit/insert mode

I don't use vim, nor have I ever tried, but I really find value in the idea of having different 'modes', one visual one for insert and edit. Personally, I have elected CTRL + E to be my chord sequence for 'edit mode'. Now, every single key and sequence of keys becomes a brand new possability. Why CTRL + E? Well 'e' for edit, naturally, but also because it is default duplicate; for whatever reason vscode identifies this shortcut to be the same as CTRL + P by default.

I then use $ to go to the end of the line and ^ to the beginning, arbitrarily because of regex, but the point is you can create your own according to your own preference, which appears to be that of the home row. So if you elect to go this approach you can use j if you want. If you were to argue this is too much user input for a single action, consider the position of using a PC at home and a mac at work, as I do, you would already be comfortable with your settings and not need to 're-learn' shortcuts.

This answer uses an approach that affords you the creative freedom to define what a 'productive coder' looks like for you, provides a different approach to going to the end of line while maintaining your home row position, and hopefully demonstrates to any new vscoders that you are not bound to just using the native CTRL + K sequence as the chord identifier.

Regardless, re:

without using ... some key combinations?

That's unavoidable, I think, unless you choose to remap normal typing keys for this purpose

Screwy answered 22/7, 2020 at 23:26 Comment(0)
S
0

To jump to the end of line on Mac the hotkey combo is:

Cmd + Shift + \

⌘ + ⇧ + \

This is called "Go to Bracket" in the VS Code Keyboard Shortcuts settings

Suggestibility answered 18/10, 2023 at 15:37 Comment(0)
T
0

I remember it to be same as in bash, zsh (Ctrl+e / Ctrl+a) respectively (End / Beginning) of the line

Trimerous answered 8/1 at 10:39 Comment(0)
W
0

It does really depend on the keyboard for my laptop it was ctrl+fn+page down

page down as in "End" in terms of fn key

Wrigley answered 24/6 at 15:34 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.