Update tech_docs/vim_reference.md

This commit is contained in:
2024-05-06 06:12:10 +00:00
parent 615d93ec8c
commit 6bfce5ab6b

View File

@@ -513,7 +513,255 @@ Customizing VIM allows you to tailor the editor to your specific needs and prefe
Remember to keep your `.vimrc` file organized and well-commented, as it can quickly grow in complexity as you add more customizations. It's also a good idea to keep your `.vimrc` file under version control (e.g., in a Git repository) so that you can easily track changes and synchronize your configuration across different machines. Remember to keep your `.vimrc` file organized and well-commented, as it can quickly grow in complexity as you add more customizations. It's also a good idea to keep your `.vimrc` file under version control (e.g., in a Git repository) so that you can easily track changes and synchronize your configuration across different machines.
## VI. Useful Tips and Tricks
### A. Macros
Macros allow you to record a series of keystrokes and replay them later, which can be incredibly useful for automating repetitive tasks.
1. To start recording a macro, press `q` followed by a register name (a-z) in normal mode. For example, `qa` starts recording a macro in register "a".
2. Perform the desired sequence of keystrokes.
3. To stop recording, press `q` again in normal mode.
4. To execute the macro, press `@` followed by the register name. For example, `@a` executes the macro recorded in register "a".
5. To execute the macro multiple times, prefix the `@` command with a number. For example, `10@a` executes the macro in register "a" 10 times.
Example:
```
qa
I// <Esc>
j
q
```
This macro records the following actions:
- `qa` starts recording in register "a"
- `I` enters insert mode at the beginning of the line
- `//` inserts "// " (useful for commenting lines in C-style languages)
- `<Esc>` exits insert mode
- `j` moves down to the next line
- `q` stops recording
To comment out multiple lines using this macro, move the cursor to the first line and execute `@a` or a numbered prefix like `10@a`.
### B. Abbreviations
Abbreviations allow you to define shortcuts for frequently used words or phrases. When you type the abbreviation and press the expansion key (usually `<Space>` or `<Enter>`), VIM automatically expands it to the full text.
1. To create an abbreviation in insert mode, use the following command:
```vim
:iabbrev <abbreviation> <expanded_text>
```
2. To create an abbreviation that works in both insert and command mode, use:
```vim
:abbrev <abbreviation> <expanded_text>
```
Example:
```vim
:iabbrev adr Address
:iabbrev eml example@example.com
```
Now, when you type "adr" in insert mode and press `<Space>` or `<Enter>`, it will expand to "Address". Similarly, "eml" will expand to "example@example.com".
### C. Folding
Folding allows you to collapse and expand sections of your code or text, making it easier to navigate and understand the structure of your document.
1. To enable folding based on indentation, add the following line to your `.vimrc` file:
```vim
set foldmethod=indent
```
2. To open and close folds:
- `zo` opens the fold under the cursor
- `zc` closes the fold under the cursor
- `zR` opens all folds
- `zM` closes all folds
3. To create manual folds:
- `zf{motion}` creates a fold based on the specified motion. For example, `zf2j` creates a fold spanning the current line and the next two lines.
- Visual selection + `zf` creates a fold for the selected lines.
Example:
```python
def function1():
# Code for function1
pass
def function2():
# Code for function2
pass
```
With `set foldmethod=indent`, VIM will automatically create folds for each function. You can then use `zo` and `zc` to open and close the folds, respectively.
### D. Spell Checking
VIM has built-in spell checking functionality that can help you catch spelling errors in your text.
1. To enable spell checking, use the following command:
```vim
:set spell
```
2. To move between spelling errors:
- `]s` moves to the next misspelled word
- `[s` moves to the previous misspelled word
3. When the cursor is on a misspelled word:
- `z=` suggests corrections for the misspelled word
- `zg` adds the word to the spell file as a good word
- `zw` adds the word to the spell file as a wrong (bad) word
4. To disable spell checking, use:
```vim
:set nospell
```
Example:
```
This is a sampple text with a misspeled word.
```
With spell checking enabled, "sampple" and "misspeled" will be highlighted as misspelled words. You can use `]s` and `[s` to navigate between them, and `z=` to get suggestions for corrections.
### E. Indenting and Formatting Code
VIM provides several commands and settings to help you maintain consistent indentation and formatting in your code.
1. To indent lines:
- `>>` indents the current line one level
- `<<` unindents the current line one level
- `>{motion}` indents lines based on the specified motion
- `<{motion}` unindents lines based on the specified motion
- Visual selection + `>` or `<` indents or unindents the selected lines
2. To set up automatic indentation based on file type, add the following lines to your `.vimrc` file:
```vim
filetype plugin indent on
set autoindent
set smartindent
```
3. To format an entire file according to predefined rules (e.g., for C-style languages), use:
```vim
gg=G
```
This command applies the `=` (indent) operation from the first line (`gg`) to the last line (`G`).
Example:
```python
def example_function():
x = 10
y = 20
z = 30
return x + y + z
```
To fix the indentation in this Python code, you can visually select the lines inside the function and press `=`. VIM will automatically adjust the indentation based on the Python indentation rules.
These tips and tricks can greatly enhance your VIM workflow and make you more productive. As you continue to use VIM, you'll discover more tips and develop your own set of useful commands and configurations.
Remember to practice these techniques regularly and incorporate them into your daily VIM usage. Over time, they will become second nature, and you'll find yourself navigating and editing text with greater speed and efficiency.
## VII. Integrating VIM with Other Tools
### A. VIM as an IDE - Python and JavaScript
With the right plugins and configurations, VIM can be transformed into a powerful Integrated Development Environment (IDE) for various programming languages, including Python and JavaScript.
For Python:
1. Install the following plugins:
- [python-mode](https://github.com/python-mode/python-mode): Provides Python-specific features like code completion, syntax checking, and more.
- [jedi-vim](https://github.com/davidhalter/jedi-vim): Offers auto-completion and code navigation using the Jedi library.
- [vim-flake8](https://github.com/nvie/vim-flake8): Integrates the Flake8 linter for Python code style checking.
2. Configure your `.vimrc` file:
```vim
" Enable Python syntax highlighting
syntax on
filetype plugin indent on
" Set up Python-mode
let g:pymode_python = 'python3'
let g:pymode_lint = 1
let g:pymode_lint_checkers = ['pyflakes', 'pep8']
let g:pymode_rope = 1
" Set up jedi-vim
let g:jedi#auto_initialization = 1
let g:jedi#completions_enabled = 1
let g:jedi#auto_vim_configuration = 1
" Set up vim-flake8
autocmd FileType python map <buffer> <F7> :call flake8#Flake8()<CR>
```
For JavaScript:
1. Install the following plugins:
- [vim-javascript](https://github.com/pangloss/vim-javascript): Provides improved syntax highlighting and indentation for JavaScript.
- [tern_for_vim](https://github.com/ternjs/tern_for_vim): Offers auto-completion, function argument hints, and jump-to-definition using the Tern server.
- [vim-jsx](https://github.com/mxw/vim-jsx): Adds JSX syntax highlighting and indentation support.
2. Configure your `.vimrc` file:
```vim
" Enable JavaScript syntax highlighting
syntax on
filetype plugin indent on
" Set up vim-javascript
let g:javascript_plugin_jsdoc = 1
let g:javascript_plugin_ngdoc = 1
let g:javascript_plugin_flow = 1
" Set up tern_for_vim
let g:tern_show_argument_hints = 'on_hold'
let g:tern_map_keys = 1
" Set up vim-jsx
let g:jsx_ext_required = 0
```
These configurations provide a solid foundation for using VIM as an IDE for Python and JavaScript development. You can further customize and extend your setup based on your specific needs and preferences.
### B. VIM and Version Control Systems
VIM integrates well with version control systems like Git, enabling you to perform common version control tasks without leaving the editor.
1. Install the [vim-fugitive](https://github.com/tpope/vim-fugitive) plugin, which provides a Git wrapper for VIM.
2. Configure your `.vimrc` file:
```vim
" Set up vim-fugitive
nmap <leader>gs :Gstatus<CR>
nmap <leader>gc :Gcommit<CR>
nmap <leader>gp :Gpush<CR>
nmap <leader>gd :Gdiff<CR>
```
These mappings allow you to quickly access Git commands like `Gstatus`, `Gcommit`, `Gpush`, and `Gdiff` using leader key shortcuts.
3. Some useful vim-fugitive commands:
- `:Gstatus` opens the Git status window, where you can stage, unstage, and commit changes.
- `:Gcommit` opens the commit message window for creating a new commit.
- `:Gpush` pushes the current branch to the remote repository.
- `:Gdiff` shows the diff of the current file against the last commit.
In addition to vim-fugitive, you can also use plugins like [vim-gitgutter](https://github.com/airblade/vim-gitgutter) to display Git diff markers in the sign column, indicating added, modified, or removed lines.
### C. VIM and Terminal Multiplexers (tmux)
Terminal multiplexers like tmux allow you to manage multiple terminal sessions within a single window. Integrating VIM with tmux can greatly enhance your workflow by enabling seamless navigation between VIM and other terminal applications.
1. Install tmux on your system (e.g., `sudo apt-get install tmux` on Ubuntu).
2. Configure your `.tmux.conf` file to enable mouse support and vi-style key bindings:
```
set -g mouse on
setw -g mode-keys vi
```
3. Install the [vim-tmux-navigator](https://github.com/christoomey/vim-tmux-navigator) plugin, which allows you to use the same key bindings (`Ctrl+h/j/k/l`) to navigate between VIM splits and tmux panes seamlessly.
4. Configure your `.vimrc` file:
```vim
" Set up vim-tmux-navigator
let g:tmux_navigator_no_mappings = 1
nnoremap <silent> <C-h> :TmuxNavigateLeft<cr>
nnoremap <silent> <C-j> :TmuxNavigateDown<cr>
nnoremap <silent> <C-k> :TmuxNavigateUp<cr>
nnoremap <silent> <C-l> :TmuxNavigateRight<cr>
```
5. With this setup, you can use `Ctrl+h/j/k/l` to navigate between VIM splits and tmux panes effortlessly.
### D. VIM and Window Managers (i3)
Tiling window managers like i3 can be used in conjunction with VIM to create a highly efficient and keyboard-driven development environment.
1. Install and configure i3 on your system following the official documentation.
2. Configure your i3 key bindings to launch and navigate between VIM instances. For example, in your i3 configuration file:
```
bindsym $mod+v exec urxvt -e vim
bindsym $mod+h focus left
bindsym $mod+j focus down
bindsym $mod+k focus up
bindsym $mod+l focus right
```
These bindings allow you to launch VIM in a new terminal window using `$mod+v` and navigate between i3 windows using `$mod+h/j/k/l`.
3. You can also configure i3 to use specific workspaces for different projects or tasks, and assign VIM instances to those workspaces for quick access.
By integrating VIM with i3, you can create a highly customized and efficient development environment that maximizes screen real estate and minimizes the need for mouse usage.
Integrating VIM with other tools and platforms can significantly enhance your productivity and streamline your workflow. Experiment with different combinations and find the setup that best suits your needs and preferences.
Remember to refer to the documentation and guides specific to each tool or plugin for more detailed information on installation, configuration, and usage.
--- ---