Skip to main content

Indent/Dedent Text Add or remove indentation from text with spaces or tabs.

Indent/Dedent Text illustration
📝

Indent/Dedent Text

Add or remove indentation from text with spaces or tabs.

1

Paste Your Text

Paste text to indent or dedent.

2

Set Indentation Options

Choose indent/dedent mode, tab/space, and amount.

3

Copy the Adjusted Text

Copy the re-indented text.

Loading tool...

What Is Indent/Dedent Text?

Indent/Dedent Text is a text processing tool that modifies the indentation of every line in a given text by either adding or removing whitespace characters. Developers and writers use it to format code, text blocks, and content for different contexts, solving the specific problem of inconsistent indentation that can make text difficult to read. For instance, when working with code, it's essential to remove leading whitespace to maintain clean and readable code structure.

This tool stands out due to its customizable features, such as the ability to choose between space-based and tab-based indentation. In space-based mode, users can specify the number of spaces to add or remove, ranging from 1 to 16, allowing for precise control over text indentation. The option to use tabs instead of spaces provides an alternative way to indent code with tabs, which is useful when working with certain programming languages or text editors.

When using it to add indentation to text, the tool prepends the specified whitespace characters to each line, making it a convenient text indentation tool for formatting text blocks and preparing content for different indentation contexts. Conversely, in dedent mode, it removes leading whitespace from each line up to the specified amount, effectively removing indentation from code and text. This functionality is particularly useful when trying to remove leading whitespace or dedent text online, making it easier to work with indent code that uses spaces or tabs.

Why Use Indent/Dedent Text?

  • Add consistent indentation to text blocks
  • Remove unwanted leading indentation
  • Switch between tab and space indentation
  • Configurable indent amount

Common Use Cases

Code Formatting

Add or remove indentation levels from code blocks.

Email Quoting

Indent text blocks for email quoting or documentation.

Content Preparation

Adjust indentation for pasting into different contexts.

Documentation

Format code examples with proper indentation.

Technical Guide

The tool works under the hood by using React's useState and useCallback hooks to manage its state and handle user input. When a user selects either indent or dedent mode, the tool updates its internal state with the chosen action. The amount of indentation and whether to use tabs or spaces are also stored in this state. The onProcess function is then called with the user's text input, at which point it splits the input into individual lines using the split method with a newline character as the separator.

In indent mode, the tool uses JavaScript's repeat method to create a string of spaces that will be prepended to each line, or a tab character if the useTab option is enabled. This string is then concatenated with each line using the map method and finally joined back together into a single string with newline characters in between. The TextToolLayout component from the @/components/shared directory handles rendering this output.

When operating in dedent mode, the tool checks each line to see if it starts with a tab character or spaces. If useTab is enabled, it simply removes one leading tab character if present using slicing. For space-based dedentation, it iterates over the characters at the start of each line and counts how many spaces are there until it reaches either a non-space character or has removed the specified amount of spaces. This count is then used to slice off the leading whitespace from the line.

The tool uses JavaScript's built-in string methods like split, map, join, and slicing for text manipulation, while React handles the user interface updates based on state changes triggered by user interactions with form elements such as radio buttons and checkboxes. The amount of spaces to add or remove is constrained between 1 and 16 using Math.max and Math.min functions when handling input from a number type input field.

Tips & Best Practices

  • 1
    Indent adds whitespace to the beginning of every line
  • 2
    Dedent removes whitespace from the beginning of every line
  • 3
    Use tab mode when pasting into editors or projects that require tab characters
  • 4
    Dedent will not remove more spaces than exist on each line

Related Tools

Frequently Asked Questions

Q What is the difference between indent and dedent?
Indent adds whitespace to the start of each line. Dedent removes whitespace from the start of each line.
Q Does dedent remove all indentation?
It removes up to the specified amount. To remove all indentation, use a large value or repeat the operation.
Q Can I use tabs instead of spaces?
Yes, toggle the tab option to indent/dedent with tab characters.
Q Does it affect empty lines?
Indent adds whitespace to all lines including empty ones. Dedent only removes existing whitespace.
Q How many spaces can I indent by?
The space amount is configurable from 1 to 16 characters.

About This Tool

Indent/Dedent Text is a free online tool by FreeToolkit.ai. All processing happens directly in your browser — your data never leaves your device. No registration or installation required.