Question
Answer and Explanation
Vim provides several commands and features that are helpful for managing braces and lines effectively. Here's how to utilize some key techniques:
1. Navigating with Braces:
- `%`: This command moves the cursor to the matching brace. Position your cursor on an opening or closing brace, then press `%` to jump to its counterpart. It works for parentheses, curly braces, and square brackets.
2. Indentation and Formatting:
- `==`: This command formats the current line according to your language-specific indent rules. Make sure your file type is properly set to get the best results.
- `=` followed by a motion: This formats the lines covered by a motion. For example, `=i{` formats the block enclosed by the innermost curly braces around the cursor, and `=a{` formats the entire block. Other useful motion examples include `=ip` which formats the current paragraph, `=i[` or `=a[`, which are used for the blocks surrounded by square brackets
- `>>` or `<<`: These commands increase or decrease the indentation of the current line, or multiple selected lines in Visual Mode, by the current tabstop amount.
3. Deleting/Changing Within Braces:
- `di{`: This deletes the text within the innermost curly braces around the cursor.
- `da{`: This deletes the current code block, including the surrounding curly braces. Other delimiters like (
, [
, <
and matching characters also works
, including HTML/XML like tags <div> <span>
- `ci{`: This changes the text within the innermost curly braces around the cursor.
- `ca{`: This changes the entire content of the block of code.
4. Adding or Deleting lines:
- `o` (small case): Add a new line bellow and enters in INSERT mode, so the developer can continue the typing or `O` (Capital case): add new line above current line.
- `dd`: Delete the current line (the letter "d" stands for delete), and is important to remind this one, because of its nature as a core function.
5. Visual Mode Selection:
- `v` (Character-wise selection): Press `v` to enter character-wise Visual Mode, then select text, like with normal key mappings or `i}` `a}`.
- `V` (Line-wise selection): Use `V` to select entire lines.
- Once lines are selected, apply indentation commands: `>` to indent or `<` to deindent or actions to all, using `d` for deletion.
Example to deleting all block surrounded by "()"
da)
. In which:
- d - is for the Delete.
- a - for around, outside of block content.
- ) - means until matching symbol.
If you try the same as `di)` will work just fine and delete only content from inside of "()"
Configuration :
It's highly recommended to configure Vim with a `.vimrc` file to set indentations and other parameters according to personal needs.
By using these features effectively, you can increase productivity, making the coding flow smoother and maintain consistency in style, even with other text types.