Assign formatting with an extension to a keyboard shortcut - visual-studio-code

I have the Prettier extension for Visual Studio Code installed. All the documentation goes over how to make Prettier the default formatter. However, in my case, I need to use Prettier for a class, but I don't want to use it at work. Given that my settings are synced, I know that I can do Ctrl + Shift + P and select Format Document With... and then select Prettier. My question is, is there a way to assign a shortcut to formatting a document with a certain formatter so that I don't have to navigate my way through the VS Code menus?
I did assign Format Document With... to Ctrl + Shift + Alt + F but I'm just wondering if there is a better way. Thanks!

Related

Is it possible to format a specific piece of Apex code using Prettier in VS code?

In visual studio code, I can specify a specific range and press Ctrl + K & Ctrl + F to format the selected range, but it does not work well in Apex code.
We use Prettier as our formatting tool and it is compatible with APEX.
I strictly checked to see if the shortcut was registered in VS code and it was.

Is there any keyboard shortcut to select/copy the auto highlighted text in VS Code?

When I place my cursor at any position inside a text like the string in this photo, vs code automatically mildly highlights the entire string. Is there any shortcut key for selecting and copying this entire text highlighted by vs code?
Example: String being mildly highlighted by vs code How to select/copy this with a keyboard shortcut?
Update:
Added gif to show the highlighting - gif showing the highlighting
There's not a single, builtin keyboard shortcut for the behavior you described, but you can use a combination of two to achieve the result you want:
The first is called "Expand Selection" in the VS Code app, and the online keybindings documentation describes it as "Expand AST Selection" (editor.action.smartSelect.expand). The default platform keybindings for this are:
linux: shift + alt + right
mac: shift + ctrl + cmd + right
win: shift + alt + right
If the highlighted string is one word, it will require one use, and if the string is multi-word, it will require two uses.
After that, the selection will be complete, and you can simply copy (cmd/ctrl + c).
Demo:
If in a string, triggering the command editor.action.smartSelect.grow a couple of times should select that entire string. And then you can copy the normal way Ctrl+C.
That command is unbound by default. You can assign a keybinding to it in the Keyboard Shortcuts editor by searching for smartSelect and click on the little pencil icon next to the command.

What is the shortcut to auto indent code in VSCode?

I work with both C++ and Python, however ctrl+k f doesn't seem to auto-indent my code. Is there anything I might be missing?
If I need to install an extension that is OK, but I would prefer to use a built in method if there is one.
I am wondering the same thing and find this native solution.
On Windows Shift + Alt + F
On Mac Shift + Option + F
On Ubuntu Ctrl + Shift + I
Ref:
https://www.codegrepper.com/code-examples/whatever/how+to+auto+indent+in+visual+studio+code
Press Ctrl+Shift+P to open Command Palette. Type in settings and select Open User Settings.
In Search settings box, input indent to search for settings related to indentation. Select full in Editor: Auto Indent section.
Automatic indentation is now enabled. Changes are saved automatically and applies to all opened files as well.
But its better to install plugins like linter or formatter or beautifier
so you can use commands like Shift + Alt + F to format your code
if you go into Options > C/C++ > Code Style > General > When I paste,
the first option is "Indent and format".
so if you select all, cut, then paste, it should auto indent everything for you.
probably not the fanciest or most robust answer but it works for me.
These solutions did not work for me in Ubuntu.
What worked for me was:
Shift + tab

In VSCode, which command will auto-indent my current line?

I have read this post about auto formatting code, and it worked when formatting some JSON. It seems to format the entire file at once. This could be somewhat drastic, e.g., if the indent rules used by VS Code conflicts with a project, this would lead to a major version control diff when used on a large file. How could I auto-indent my current line?
In Emacs, the cursor can be anywhere on the line and pressing Ctrl+i will auto-indent the current line. I searched through the possible Keyboard Shortcuts and did not find one that would do this. I am looking something like "Indent Line" but to auto-indent.
You can simulate that by pressing ctrl + i + k + f. These are two separate shortcuts: ctrl + i will select the current line under cursor and ctrl + k + f will format the selection.
You can remap the editor.action.formatSelection command to something shorter if you want to save keystrokes.

How to select current word in Visual Studio Code (VS Code)?

How to select the current word, that is where the caret is at.
Note: I am looking for the shortcut for Visual Studio Code(VS Code), the text editor, and not Visual Studio IDE.
On Mac OS: Cmd+D
On Windows & Linux: Ctrl+D
Above solved the purpose for me.
But ⌘D is defined as "editor.action.addSelectionToNextFindMatch", so if you press it more than once, it will try to search and select same word in the file which then can be used to do "multi word editing".
You are looking for Shrink/Expand Selection.
Trigger it with Shift+ Alt+Left and Shift + Alt+Right
Update:
This is now called Smart select API.
This feature uses semantic knowledge to intelligently expand selections for expressions, types, statements, classes, and imports.
It is Ctrl + D that works for me in latest Visual Studio Code on Windows.
Go to File -> Preferences -> Keyboard shortcuts, you will find this:
If you want to ctrl+w to behave the same as in Idea just go keyboard settings
Search for Expand selection. Set new shortcut cmd+w or ctrl+w depending on your OS.
Also re-bind other commands that use ctrl+w to use another shortcut that you want, for example cmd+f4
You can edit keybindings.json to avoid using UI.
Shift + Alt+Right Arrow if the word is in camelCase then you will have to click Right Arrow again to select the whole camelCase. Every time you press Right Arrow again while still holding Shift + Alt down you will select a further part of the code.
so:
first the word.
then if it's part of a camelCase then the camelCase.
then if it is in a string the whole string.
... (many other posibilities)
the whole line.
everything inside the parentheses code block
the whole file
at any given time you can go back to the last selection by clicking Left Arrow instead of Right Arrow
I don't know about CTRL + w in the old Visual Studio Code but in the JetBrains IDE's this is the equivalent to CTRL + w by holding down CTRL and clicking w to select more and holding down CTRL + Shift and clicking w to unselect.
Another possibility which helps to avoid selecting only one word in camelCase is CTRL + d this will just select the whole camelCase. This will however have the side-effect of also changing the current "find" criteria.
thanks Chandan Nayak for this extra shortcut.
An unpopular opinion: you can now have Resharper keybindings, if you come from Jetbrain's camp.
The Ctrl+W expansion grow and shrinks is different from expansion selection.
On "File/Preferences/Keyboard Shortcuts" I deleted the shortcut "Ctrl + W" to close the current tab action, because for this "Ctrl+F4" works for me.
Update (14 days later): Yesterday I installed VSCode 1.34.0 - I think since then the functionality is "Ctrl + D". I was very suprised.
For any editor, you can use the below shortcuts. These shortcuts work for every text area also.
Ctrl + Shift + LeftArrow/RightArrow - this will select text word by word
Shift + UpArrow/DownArrow - this will select text line by line
Ctrl + BackSpace - this will delete text word by word
Additional
in intellijIdea
Ctrl + w - use for the select current word, after giving second Ctrl + W it will select the second word also. Like that you can select the whole line.
Ctrl + d - you can duplicate current line.