How to indent/format a selection of code in Visual Studio Code? - visual-studio-code

I want to indent a specific section of code in Visual Studio Code.
I read How do you format code in Visual Studio Code? that gives shortcuts to indent the whole code, but it doesn't work when selecting a specific section of code.
I tried Ctrl + Shift + F after selecting some line in my code, but the whole file is indented. I'm on Windows with Visual Studio Code Insider 1.8.0. How can I do it?

I want to indent a specific section of code in Visual Studio Code:
Select the lines you want to indent.
Use Ctrl + ] to indent them.
If you want to format a section (instead of indenting it):
Select the lines you want to format.
Use Ctrl + K, Ctrl + F to format them.

You can also indent a whole section (multi-lines) by selecting it and clicking
TAB
and also indent backward using Shift+TAB
And of course for auto indentation and formatting, following the language you're using, you can see which good extensions do the good job, and which formatters to install or which parameters settings to enable or set. For each language and its available tools. Just make sure to read well the documentation of the extension, to install and set all what it needs. Exemple: prettier is the most common used formatter for JavaScript and typescript. And it's widely used by all projects and code style requirements and setup. And in CI pipelines.
Up to now the indentation problem bothers me with Python when copy pasting a block of code. If that's the case, here is how you solve that: Visual Studio Code indentation for Python

On OS X, choose "Document Format", and select all lines that you need format.
Then Option + Shift + F.

(This works at least up to version 1.74.2, checked in Jan 2023)
On macOS Visual Studio Code version 1.36.1 (2019)
To auto-format the selection, use ⌘K ⌘F (the trick is that this is to be done in sequence, ⌘K first, followed by ⌘F).
To just indent (shift right) without auto-formatting, use ⌘]
As in Keyboard Shortcuts (⌘K ⌘S, or from the menu as shown below)

This should be able to set to whatever keybindings you want for indent/outdent here:
Menu File → Preferences → Keyboard Shortcuts
editor.action.indentLines
editor.action.outdentLines

F1 → open Keyboard Shortcuts → search for 'Indent Line', and change keybinding to Tab.
Right click > "Change when expression" to editorHasSelection && editorTextFocus && !editorReadonly
It will allow you to indent line when something in that line is selected (multiple lines still work).

For German keyboard layout, the standard settings are:
Indent selection: Strg + ´
Outdent selection: Strg + ß

As you've seen there are two ways to indent the code (this for Windows).
Reindenting the entire file
Shift+Alt+F
Reindenting only selected lines
First set the shortcut for Reindent Selected Lines
Menu File → Preferences → Keyboard Shortcuts → In the Search in keybindings type in Reindent Selected Lines → Select it and press Enter → Type in your own shortcut, e.g. Shift + 5, followed by Enter
Now select your code lines in the editor and use the shortcut set above, e.g. Shift + 5, to automatically indent those lines only.

On windows its "Ctrl+[" and "Ctrl+]" for indent and unindent You can find rest of the shortcuts here
For mac, you can find the shortcuts here: https://code.visualstudio.com/docs/getstarted/keybindings

For me on windows it was Ctrl+¡ , indent line. It adds a tab at the beggining of each line.

Many of the answers were not able to solve my problem too.
Just go for fn+tab
Welcome in advance.

On linux ubuntu: select text then ctrl + shift + i

This is the way I had my code before formatting...
Then I used the command like this... (Make sure to select the code part that you need to format)
Shift+ Alt+F
And I got the formatted code like this....

For me, using a mac in 2022 it was CMD + ] to indent multiple lines after selecting the desired indented lines.

Crtl + Alt + F can also formate (windows)

Windows - 2022
Shift+Alt+F

Related

How to decrese indent of a selection in xml in vscode

I have pasted some xml into an xml file in vscode.
The selection is indented to much. I would like to move the block to the left.
In Eclipse this is done using Source / Shift Left.
Is there an equivalent for this in vscode?
I found the shortcut to format Shift Option F but it does not do anything in my selection and I do not want to reformat the whole file.
I think you should use LeftShift + Tabulation it does the work for me.

How to select whole word under the cursor in Visual Studio Code?

What hotkey or command helps to select the whole word under the cursor in VSC?
(Like CTRL or ALT + D in ST3)
To select the word under the cursor in VSCode:
ctrl + d
Note: the definition of a "word" is different when your document edited with VSCode is a markdown one.
That is why VSCode 1.52 (Nov. 2020) proposes:
Markdown inline smart select
Expand and shrink selection in Markdown documents using the following commands:
Expand: kb(editor.action.smartSelect.expand)
Shrink: kb(editor.action.smartSelect.shrink)
Selection applies to italics, bold, inline code blocks, and links.
Another simple way is to double click the mouse left button.

Issue with gist indentation

When creating a Gist on Github there is a setting for indentation (tabs or spaces; size 2, 4, or 8). After setting indents to tabs size 4, it changes to tabs size 8 after I save it. Editing it afterwords doesn't do anything. Other settings don't produce the expected result either. Am I misunderstanding this feature somehow? Can't find any documentation regarding this.
I replaced tabs with four spaces in Notepad++ (Ctrl+H), and it works. You can use any numbers of spaces.
Those tabs are automatically displayed as a 8-character-tab in Github Gist.
This is happening because while writing the code, you used the tab key which inserted 8 spaces. Here's a solution that I use.
Copy all your code to a local file and open it in the vi editor.
cat>temp.js
ctrl+shift+v to paste and ctrl+d to save.
vim temp.js (Or change the extension as per your file.)
Run the following command that I found from here. This will half the existing space.
:%s;^\(\s\+\);\=repeat(' ', len(submatch(0))/2);g
Press the esc key then :x and enter key to save and exit vi.
Copy the code in your temp.js file and paste it in your gist with spaces as 4.
Convert the indentation from Tabs to Spaces or Spaces to Tabs by using vscode with the easy and simple following steps.
Open the file with vscode.
Press,
On MacOS, command + shift + p
On Windows, ctrl + shift + p
Type "convert indentation to spaces" and select then option. (As shown in the below fig)
Save the file. (ctrl+s / ⌘+s)

What is the property name to break long lines in VS Code?

When I have long text line I would like to show it next line.
What is the property name in VS Code?
The menu under File > Preferences or press Ctrl+, (on Mac Code > Preferences > Settings or press Command (or Cmd) ⌘+,) provides entries to configure user and workspace settings. You are provided with a list of default Settings.
Set editor.wordWrap: on in your User Settings or Workspace Settings under preference.
Select the below options to change to the desired settings.
Off - Lines will never wrap.
on - Lines will wrap at the viewport width.
wordWrapColumn - Lines will wrap at "Editor: Word Wrap Column".
bounded - Lines will wrap at the minimum of viewport and "Editor: Word Wrap Column".
You can toggle word wrap for the VS Code session with Alt+Z (macOS: Option (or Alt) ⌥+Z) or select View > Word Wrap from Menu.
For more about User and Workspace Settings or Key Bindings for Visual Studio Code
On Windows, press Alt + Z (macOS: Option ⌥ + Z) to toggle word wrapping, or, select View > Toggle Word Wrap from VSCode Menu.
For Windows, Pressing Alt+Z will break the line.
For Windows users do the following in -->
Open VScode
Go to Settings(Gear icon on the bottom-left side)
Click Settings in the menu
In the searching bar search for "word wrap"
Click the drop-down menu and change it to "on"
This setting will wrap your words according to your editor: viewport.
In case this auto wrapping isn't working you can press --> Alt + z to wrap content
In VS Code:
Install Beautify plugin. (or any Formatter)
Go to the file & select the desired part.
Right-Click on it & select Format Selection Or press Ctrl + k Ctrl + F.
Hope this helps.
HTML > Format: Wrap Line Length
set the maximum number of characters (for example - 1000)
You can press Ctrl + W / Ctrl + L to toggle word wrapping or select Main menu "Edit>Advanced>Word Wrap" .

eclipse multiple text selection like sublime text 2

is there an option or plugin for eclipse which would enable multiple simultaneous selections in the same editor.
In sublime text, selecting some text and then pressing Ctrl+d will add next instance of the same text to the selection if possible. After selecting the instances needed the editor has multiple carrets (not necessarily on the same or adjacent columns and rows). In this mode it is possible to move all cursors forward or back simultaneously and to edit all instances of text simuntaneously.
I find this feature very usefull and miss it sorely in eclipse..
This Eclipse plugin attempts to provide this feature: https://github.com/caspark/eclipse-multicursor. From the README:
What is this?
A work-in-progress attempt to provide Sublime-Text-like
multi cursor support for text editors in the Eclipse IDE.
What works?
Multiple identical lines can be edited simultaneously using Eclipse
linked mode editing (similar to existing "rename in file"
functionality)
Next steps
"select next" functionality + associated editing using Eclipse linked
mode
"find next" + associated editing
editing of non-identical text / editing without using linked mode
split selection to lines
regexp support for find next
This feature is available in LiClipse.
See it in action (more towards the end of the video).
It supports linking with Ctrl+K, unlink with Shift+Alt+K, Ctrl+Alt+mouse double click to select words or Ctrl+Alt+Mouse to make a selection of a region (or just end lines).
Preferences>General>keys>Rename - refactoring
I changed the binding to command + shift + R when > Editing Text.
Sorry for bringing up an old question, stumbled upon it after searching google for the problem
Alt + Shift + A, then you can hold shift and use the cursor in multiple lines.
Like Ctrl+D I could not find, but like Alt+F3 in sublime (multiselects all matches), you can do by pressing Alt+Shift+R, or select text > right click > refactor > rename.
Must say that this does not work with any kind of text. It works with names of variables, functions, classes etc.
Tested on Eclipse 3.8.1
ALT + SHIFT + F worked for me.
You can see shortcuts for all here:
Goto -> Window -> Preferences -> General -> Keys and search for replace then you will see binding for Find and replace. In the bottom of that window, you can add your key to Binding text box. There you can add or edit any keys as shortcut.
If you want to replace selected word's matching words or find selected words, use below keys because you do not need to select all words in eclipse:
Ctrl+F gives me Find/Replace dialog box.
Or you can,
First Alt+A
Next Alt+F
Then press on Replace or Search button occurding to your need.