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

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?

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.

Simply go look it up or change it in your key binding settings:
File > Preferences > Keyboard shortcuts
It's named cursorLineEnd.
Same can be done for cursorLineStart of course.
Interfering other shortcuts can be changed or deleted as well in that menu.

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

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

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.

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:
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

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

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"
}

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

No need to do these messy things, just use your keyboard's home key and end key
Press the home key to go to the beginning of the line
Press the end key to go to the end of the line

Related

VSCode How do I skip past an automatically generated end </tag>?

Say I were typing
<h2>Show Subject*</h2>
How would I jump to the end of the brackets if my cursor was at the *. Usually I would just arrow key right 4 times or use my mouse to select the next line. I see others on youtube doing this easily.
example: https://youtu.be/PlxWf493en4?t=567
Her cursor instantly jumps to the end of the . My < > also do not highlight as hers does when I autogenerate them.
The magics you're seeing in the linked video are features of Emmet. For example there's Emmet: Go to matching pair, which you can bind to something convenient using the Keyboard Shortcuts dialog (Ctrl-k Ctrl-s). Try Ctrl-Shift-p to bring up the command pallete and then type Emmet to see all of the available commands. I would suggest taking some time to read the docs and discover what functionality is available to you.
What looks like jumping to the outside of the end tag in the video is really just jumping to the end of the line, which is bound by default to the End key. Also useful is word jumping (Ctrl+RightArrow/Ctrl+LeftArrow) and word selection (Ctrl+Shift+RightArrow/Ctrl+Shift+LeftArrow). You might take a look at this wiki article for a fairly comprehensive list of common shortcuts.

PhpStorm shortcut to wrap text with function call

I'm in PhpStorm and I need to select some text, press some shortcut and have that text wrapped in a function call (that I would have defined somewhere in the settings beforehand).
For example:
"Hello World" would become input("Hello World").
$_GET["foo"] would become input($_GET["foo"]).
I don't know if this is even possible, but it could help me save so much time if so.
Applying regex to solve this problem is unfortunately not possible. Manually selecting what I need to wrap isn't an issue.
Will the function name be the same every time or different?
In any case: it can be done this way:
Make a Live Template of "surround template" type with the following content:
$FUN$($SELECTION$)$END$
The $SELECTION$ variable here tells that it's a surround template.
Apply correct Context (where this template can be used)
Give it an abbreviation (name used to locate & invoke it) and brief description.
Here is mine:
NOTE: replace $FUN$ by a fixed function name if the function will always be the same. You can have additional templates with different abbreviations (that will have different hardcoded function names).
To use it:
Make a selection and invoke Code | Surround with... action where you select the right template. On Windows keymap it's Ctrl + Alt + T
In action (NOTE: it's without hardcoding the function name hence me typing the myFunc part):
(HINT: you can select the desired entry in a few keypresses if the name is unique -- just start typing the name in the popup -- the standard Speed Search work here)
P.S. Code | Surround with... can have other (possibly irrelevant for you in this case) entries. To list Live Templates only, use the shortcut for Surround with Live Template... action (Ctrl + Alt + J here on my Windows keymap). You can check the shortcut or change it in the Settings/Preferences | Keymap:
This way the popup menu will be a bit shorter:
Less keypresses:
You may be able to use Macros functionality to record the invoking the popup and selecting the right entry. You can then assign a custom shortcut to that Macros: select the text, hit the shortcut and it will playback the recorded sequence.
Sadly I cannot 100% guarantee that Macros will always work nicely (sometimes/on some setups it can "eat" keypresses).
P.S. It would be much easier if the IDE would support assigning keyboard shortcuts to specific Live Templates .. but it's in the backlog and no ideas on when this might be implemented. Anyway: https://youtrack.jetbrains.com/issue/IDEA-67811 -- watch this ticket (star/vote/comment) to get notified on any progress.
P.S. You can also try Postfix completion. It's good for writing the code and not really suitable for your case (editing small parts of it), but who knows. You will have to make a custom postfix for this -- should not be an issue though.
https://www.jetbrains.com/help/phpstorm/2021.3/auto-completing-code.html#postfix_completion

How do I prevent Sublime Text 3 from auto-indenting a line as a one off

When I'm editing HTML or CSS in Sublime Text 3 the lines auto-indent when I hit the [ENTER] key, which generally is very useful.
On occasion I find myself wanting to paste a line of markup which is already indented, and this results in double indentation.
Is there some other key combination along with the [ENTER] key that prevents the next line from auto-indenting and instead returns the curson to the very beginning of the line? Please note I don't want to turn off auto-indent on a global basis.
You can do it in various steps, using various keybindings, so you can simply record a macro and do it in one simple custom keybinding (tools > record macro).
Steps:
Press [Enter] to move to a new line.
Press [shift+home] to select until the beginning of line (spaces or tabs used to indent).
Press [backspace] to remove the selected indentation charaters
You're done. Stop recording and save the macro, then use a custom keybinding to do it in a single step.
I found in Windows that CTRL + SHIFT + V (rather than just CTRL + V) does a "paste and indent" whereby the indentation is corrected as necessary.
It's also possible to switch the default paste for paste as indent as per these instructions: https://gist.github.com/twosixcode/1988097

Sublime Text - How to Make "Quick Add Next" NOT wrap around the beginning of the file

The functionality "Quick Add Next" is handy, however there is one problem, that after it reaches the last match in the file, if I do it again, it will go on to include the first match in the file also (wrap), which is not what I want, because it's usually hard to realize whether you've reached the last match or not. This occurs even if the option "wrap" is disabled among search options. Is there anyway to disable it?
If no, is there a way to unselect the match I just added by "quick add next"? That would be a valid alternative. Thanks.
Quick Add Next has the keyboard shortcut ⌘D on Mac, CtrlD on Windows/Linux. If you want to skip a selection, hit ⌘K,⌘D (CtrlK,CtrlD) - meaning, hold down the ⌘ (or Ctrl) key, hit and release K, then while still holding down ⌘, hit D.
There is no way that I know of to disable the "wrapping" feature, but if you go too far and want to back up, hit ⌘U (or CtrlU), known as soft_undo, as many times as needed to go back to where you want to be.

Is there any way to switch multiple cursors with tab?

The situation is: in my html-file I have a lot of "href" attributes to be filled with specific links. And I can get multiple cursors on every needed place.(via Ctrl+D, or Alt+Enter).
Now it would be very helpful if I could switch between this cursors with tab(like emmet plugin does with self-generated html-content, if you know what I'm talking about).
You can have multi cursor, and switch between them in using :
Ctrl + F3
or
Ctrl + Shift + F3
You can get the behavior I believe you want by selecting all the "href" attributes using ctrl+d. If you are selecting all of them, you cna run the find_all_under command (alt + f3 in windows, unsure of other platforms but you can search for the command). Then, bookmark the cursor positions (ctrl+f2 in windows). You can then use f2 to cycle through the bookmarks. You could bind the appropriate command to tab if you want also, though you would have to do some work do you don't break normal tab behavior.
As a side note, I believe emmet simply inserts a snippet, so it defines various locations for the cursor to jump to through the built in snippet behavior.
#Jahnux solution may be more ideal since you wouldn't have to select initially, but I believe you would have to move the cursor back to the "href" attribute for continuing to the next token.
You may also want to investigate simply using the find functionality, though perhaps you have run into some limitations with that.