Advanced Techniques for Using Visual Mode in Vim

Strategic Text Selections

  1. Columnar Selections (Ctrl-v):

    • Use columnar or block visual mode for tasks like editing columns of data or aligning text.
    • Combine with movement commands (like I or A) to insert or append text uniformly across rows.
  2. Non-Continuous Selections:

    • Make non-continuous selections using Ctrl-v and then modifying the selection with directional keys (h, j, k, l) to precisely adjust the selected area.

Combining Commands with Visual Mode

  1. Combining with : Commands:

    • Once a selection is made, pressing : will prompt you with :'<,'>, indicating the range is selected.
    • Apply commands over this range, such as :sort to sort lines or :norm to apply normal mode commands across the selected lines.
  2. Advanced Formatting:

    • Use visual mode to select text and then use formatting commands like = to auto-indent or ! followed by an external command (e.g., fmt) to format text.

Using Visual Mode for Efficient Editing

  1. Indentation:

    • Use visual mode to select multiple lines, then use > or < to shift the text right or left, respectively.
    • Repeatedly press . (dot) after shifting to continue indenting at the same level across different blocks.
  2. Integration with Search Operations:

    • After making a selection in visual mode, use : followed by s/<pattern>/<replacement>/ to replace text within the selected area only.

Advanced Editing Techniques

  1. Filtering Text Through External Programs:

    • Select text and use :! followed by a command to filter the selected text through an external program, such as :!sort or :!awk.
  2. Executing Macros over Line Selections:

    • Record a macro and then apply it to a selection by selecting lines in visual mode and running :norm @a where a is your macro.

Enhancing Visual Mode with Plugins

  • vim-visual-multi: Enhance visual mode with multiple cursors, allowing simultaneous edits in multiple locations.
  • vim-expand-region: Increase the granularity of visual selections incrementally, perfect for precisely expanding selections.

Tips for Mastering Visual Mode

  • Frequent Practice: Regularly practice these advanced techniques to build muscle memory.
  • Custom Mappings: Create custom key mappings for frequently used visual mode commands to speed up your workflow.
  • Plugin Integration: Explore plugins that extend visual mode functionality to suit specific needs or workflows.