From 8ebf460408ea61419c6a8e59178bf0a46e6d52c5 Mon Sep 17 00:00:00 2001 From: medusa Date: Sun, 14 Apr 2024 15:41:59 +0000 Subject: [PATCH] Update docs/tech_docs/linux/vim.md --- docs/tech_docs/linux/vim.md | 210 ++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) diff --git a/docs/tech_docs/linux/vim.md b/docs/tech_docs/linux/vim.md index 6ad999c..5a75425 100644 --- a/docs/tech_docs/linux/vim.md +++ b/docs/tech_docs/linux/vim.md @@ -1,3 +1,213 @@ +# Vim Guide + +## Introduction + +Vim is a powerful text editor that utilizes a modal editing approach, allowing users to perform complex text manipulations efficiently and with minimal keystrokes. By mastering Vim's modal editing paradigm, users can significantly increase their productivity and streamline their workflow. + +Key benefits of using Vim include: + +- Increased efficiency through modal editing and powerful commands +- High level of customization and extensibility +- Ubiquity across Unix-based systems and remote development environments + +This guide will cover the essential concepts and commands that form the foundation of Vim's modal editing. + +## Essential Vim Modes + +### Normal Mode + +Normal mode is the default mode in Vim and is used for navigation and text manipulation. In Normal mode, keys perform different actions based on their assigned functions. + +### Insert Mode + +Insert mode is used for entering and editing text. In Insert mode, keys function as they would in a traditional text editor, inserting characters at the cursor position. + +### Visual Mode + +Visual mode is used for selecting text and performing operations on the selected text. There are three types of Visual mode: character-wise, line-wise, and block-wise. + +### Command-line Mode + +Command-line mode is used for executing commands, searching, and performing ex commands. To enter Command-line mode from Normal mode, press the `:` key. + +## Key Commands and Motions + +### Basic Navigation + +- `h`, `j`, `k`, `l`: Move the cursor left, down, up, and right, respectively. +- `w`, `b`, `e`: Move the cursor to the beginning of the next word, the beginning of the previous word, or the end of the current word. +- `0`, `$`: Move the cursor to the beginning or end of the line. +- `gg`, `G`: Move the cursor to the first or last line of the file. + +### Text Manipulation + +- `y`: Yank (copy) the selected text. +- `d`: Delete the selected text. +- `p`, `P`: Paste the yanked or deleted text after or before the cursor. +- `u`: Undo the last change. +- ``: Redo the last undone change. + +### Search and Replace + +- `/pattern`, `?pattern`: Search forward or backward for the specified pattern. +- `n`, `N`: Move to the next or previous occurrence of the search pattern. +- `:s/old/new/g`: Replace all occurrences of "old" with "new" in the current line. +- `:%s/old/new/g`: Replace all occurrences of "old" with "new" in the entire file. + +### Working with Multiple Files + +- `:e filename`: Open the specified file in a new buffer. +- `:bn`, `:bp`: Move to the next or previous buffer. +- `:ls`: List all open buffers. +- `:b`: Switch to the buffer with the specified number. +- `:sp`, `:vsp`: Split the window horizontally or vertically. + +## Configuring Vim with .vimrc + +### Essential Settings + +- `syntax on`: Enable syntax highlighting. +- `set number`: Display line numbers. +- `set ignorecase`: Use case-insensitive search by default. +- `set smartcase`: Override `ignorecase` when the search pattern contains uppercase characters. +- `set incsearch`: Show search matches as you type. +- `set hlsearch`: Highlight search matches. +- `set tabstop=4`: Set the width of a tab character to 4 spaces. +- `set shiftwidth=4`: Set the number of spaces to use for autoindentation. +- `set expandtab`: Use spaces instead of tabs for indentation. + +### Key Mappings + +- `let mapleader = ","`: Set the leader key to comma (`,`). +- `nnoremap w :w`: Map `w` to save the current file. +- `nnoremap q :q`: Map `q` to quit Vim. +- `nnoremap h :nohlsearch`: Map `h` to clear search highlighting. + +### Plugin Management + +Vim has a rich plugin ecosystem that extends its functionality. Popular plugin managers include: + +- [vim-plug](https://github.com/junegunn/vim-plug) +- [Vundle](https://github.com/VundleVim/Vundle.vim) +- [Pathogen](https://github.com/tpope/vim-pathogen) + +Some recommended plugins for various use cases: + +- [NERDTree](https://github.com/preservim/nerdtree): File system explorer. +- [ctrlp.vim](https://github.com/ctrlpvim/ctrlp.vim): Fuzzy file finder. +- [lightline.vim](https://github.com/itchyny/lightline.vim): Configurable status line. +- [vim-surround](https://github.com/tpope/vim-surround): Easily manipulate surrounding delimiters. + +## Vim Cheatsheet + +| Command | Description | +|---------|-------------| +| `h`, `j`, `k`, `l` | Move the cursor left, down, up, right | +| `w`, `b`, `e` | Move to the start of the next, previous, or end of the current word | +| `0`, `$` | Move to the start or end of the line | +| `gg`, `G` | Move to the first or last line of the file | +| `y`, `d`, `p` | Yank (copy), delete, or paste text | +| `u`, `` | Undo or redo changes | +| `/pattern`, `?pattern` | Search forward or backward for a pattern | +| `:s/old/new/g` | Replace all occurrences of "old" with "new" in the current line | +| `:e filename` | Open a file in a new buffer | +| `:bn`, `:bp` | Move to the next or previous buffer | + +## Appendix A: Tips and Tricks for Efficiency + +### Combining Motions and Operators + +Vim's power lies in the ability to combine motions and operators to perform complex editing tasks efficiently. Here are some examples: + +- `d3w`: Delete the next three words. +- `y$`: Yank (copy) from the cursor to the end of the line. +- `cis`: Change inside a sentence (delete the current sentence and enter Insert mode). +- `>aB`: Indent a block (the current block surrounded by curly braces). + +Tip: Practice combining different motions and operators to build muscle memory for common editing tasks. + +### Macros for Automating Repetitive Tasks + +Macros allow you to record a series of keystrokes and replay them to automate repetitive tasks. Here's how to use macros: + +1. Press `q` followed by a register (e.g., `a`) to start recording a macro. +2. Perform the desired keystrokes. +3. Press `q` again to stop recording. +4. To execute the macro, press `@` followed by the register (e.g., `@a`). +5. To repeat the macro multiple times, prefix the `@` command with a count (e.g., `10@a`). + +Tip: Use macros to automate tasks like formatting, code generation, or bulk replacements. + +### Splits and Tabs for Working with Multiple Files + +Vim allows you to split the screen and work with multiple files simultaneously using splits and tabs. + +Splits: +- `:sp` or `:split`: Split the window horizontally. +- `:vsp` or `:vsplit`: Split the window vertically. +- `w`: Cycle through the split windows. +- `h/j/k/l`: Move the focus to the left, bottom, top, or right split. + +Tabs: +- `:tabnew`: Open a new tab. +- `:tabn` or `gt`: Move to the next tab. +- `:tabp` or `gT`: Move to the previous tab. +- `:tabclose`: Close the current tab. + +Tip: Use splits and tabs to view and edit related files side by side, making it easier to reference and navigate between them. + +### Registers for Advanced Copy/Paste Operations + +Vim provides multiple registers for storing and retrieving text. Some commonly used registers include: + +- `"a` to `"z`: Named registers for storing text. +- `"0`: The yank register, which stores the last yanked text. +- `"+`: The system clipboard register (requires Vim compiled with clipboard support). + +To use registers: + +- `"ayw`: Yank the current word into register `a`. +- `"ap`: Paste the contents of register `a`. +- `"+p`: Paste the contents of the system clipboard. + +Tip: Use registers to store and recall frequently used snippets or to perform precise copy/paste operations. + +## Appendix D: Vim for Specific Workflows + +### Vim for Software Development + +Vim offers various features and plugins that make it well-suited for software development tasks: + +Code Navigation: +- ``: Jump to the definition of the keyword under the cursor (requires ctags). +- ``: Jump back from the definition. +- `gd`: Go to the local declaration of the variable under the cursor. + +Debugging: +- Plugins like `vimspector` or `vim-debug` provide debugging capabilities within Vim. +- Set breakpoints, step through code, and inspect variables without leaving the editor. + +Integration with Build Tools: +- Plugins like `vim-dispatch` or `vim-make` allow you to run build commands and view results within Vim. +- Configure key mappings to quickly compile, run, or test your code. + +### Vim for Data Manipulation + +Vim's powerful text manipulation capabilities make it suitable for working with structured data formats like CSV or JSON. + +CSV Data: +- Use `:%s/,/\t/g` to convert comma-separated values to tab-separated values for easier manipulation. +- Leverage Vim's column editing features (e.g., visual block mode) to work with tabular data. +- Plugins like `csv.vim` provide additional functionality for working with CSV files. + +JSON Data: +- Use `:%!jq .` to format and pretty-print JSON data (requires the `jq` command-line tool). +- Plugins like `vim-jdaddy` or `vim-json` offer improved syntax highlighting and formatting for JSON files. + +Tip: Combine Vim's text manipulation commands with external tools (e.g., `sort`, `awk`, `sed`) to process and transform data efficiently. + +--- + It's great to see the foundation of your Vim guide taking shape! Let’s review and enrich the content with some more context and practical examples, especially focusing on programming and working with structured data. Here's a refined and expanded version based on your feedback and previous content: ---