Update docs/tech_docs/linux/vim.md

This commit is contained in:
2024-03-08 16:22:26 +00:00
parent d2e3ac5c41
commit 305bc53302

View File

@@ -1,10 +1,7 @@
Here's a basic `.vimrc` file structured with documentation and functionality, including some commonly used settings and plugins. This example assumes you're using Vim 8+ or Neovim, which has built-in package management capabilities. You can further customize it according to your preferences.
```bash
" .vimrc - Basic Configuration
" Author: [Your Name]
" Last Modified: [Date]
## `.vimrc` File: Structured Guide with Documentation
### Compatibility and Initial Setup
```vim
" Ensure compatibility with older Vim versions
if version < 700
echo "You need Vim 7.0 or greater"
@@ -16,246 +13,109 @@ if exists("g:loaded_vimrc")
finish
endif
let g:loaded_vimrc = 1
```
- Checks for Vim version compatibility.
- Prevents multiple loads of the `.vimrc` file in a single session.
" ==============================================================================
" General Settings
" ==============================================================================
" Set UTF-8 encoding
### General Settings
```vim
" Set UTF-8 encoding for text and files
set encoding=utf-8
set fileencoding=utf-8
" Use system clipboard
" Use system clipboard to easily copy/paste outside of Vim
set clipboard=unnamedplus
" Enable syntax highlighting
syntax on
" Set tabs and spaces preferences for indentation
set tabstop=4 " Number of visual spaces per TAB
set softtabstop=4 " Number of spaces in tab when editing
set shiftwidth=4 " Number of spaces to use for autoindent
set expandtab " Convert tabs to spaces
" Enable line numbers
set number
" Enable relative line numbers
set relativenumber
" Highlight current line
set cursorline
" Enable mouse support
set mouse=a
" Disable swap files
set noswapfile
" ==============================================================================
" Search Settings
" ==============================================================================
" Highlight search results
set hlsearch
" Case-insensitive searching
set ignorecase
" Case-sensitive if contains uppercase
set smartcase
" Incremental search
set incsearch
" ==============================================================================
" Visual Settings
" ==============================================================================
" Set color scheme
colorscheme desert
" Enable 256 colors
set t_Co=256
" Set status bar
set laststatus=2
" ==============================================================================
" Plugin Management (Using Vim 8+ package feature)
" ==============================================================================
" Specify directory for plugins (change '~/.vim' to '~/.config/nvim' for Neovim)
execute "set runtimepath^=~/.vim/pack/plugins/start"
" Plug: vim-plug installation and setup
" Note: For automatic installation, refer to vim-plug documentation
" ==============================================================================
" Plugins Configuration
" ==============================================================================
" Plug 'tpope/vim-surround' " Surround text with characters
" Plug 'preservim/nerdtree' " File system explorer
" Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } " Fuzzy finder
" ==============================================================================
" Key Bindings
" ==============================================================================
" Map leader key to space
let mapleader=" "
" NERDTree toggle
nnoremap <leader>n :NERDTreeToggle<CR>
" ==============================================================================
" Custom Functions
" ==============================================================================
" Add your custom Vim functions here
" ==============================================================================
" Autocommands
" ==============================================================================
" Auto-close NERDTree on closing Vim if it's the last window
autocmd vimexit * if !argc() | NERDTreeClose | endif
" ==============================================================================
" Final Touches
" ==============================================================================
" Filetype plugin indent on
filetype plugin indent on
" Enable spell checking for certain file types
autocmd FileType gitcommit,markdown setlocal spell
" ==============================================================================
" End of .vimrc
" ==============================================================================
```
This `.vimrc` example includes essential configurations for a smooth and efficient Vim experience, such as UTF-8 encoding, tab settings, line numbering, and search behaviors. It also outlines the use of Vim's package feature for plugin management, though the actual installation of plugins like `vim-plug` would require additional steps not covered directly in this file.
For each plugin listed (e.g., `vim-surround`, `nerdtree`, `fzf`), you would typically clone the plugin's repository into the specified directory or use a plugin manager like `vim-plug` to automate their installation and updates.
Remember, this is a starting point. As you grow more comfortable with Vim and your personal workflow evolves, you'll likely add, remove, or tweak these settings to better suit your needs.
---
Enhancing your `.vimrc` file to support syntax highlighting for Python, JavaScript, Markdown, and Lua, alongside other quality-of-life improvements using Vim's built-in capabilities, is straightforward. Here's an updated section to include in your `.vimrc` file, focusing on syntax support and quality-of-life enhancements:
```bash
" Enhanced .vimrc Configuration for Syntax Support and Improvements
" ==============================================================================
" General Enhancements
" ==============================================================================
" Enable line numbers
set number
" Enable relative line numbers for easier text navigation
set relativenumber
" Enable syntax highlighting
syntax enable
" Show matching parentheses/brackets
set showmatch
" Automatically indent new lines
" Indentation settings for a consistent coding style
set tabstop=4 softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
" Set search to be case insensitive unless capital letter is used
set ignorecase
set smartcase
" Line number settings for easier navigation
set number
set relativenumber
" Enable mouse in all modes
" Highlight the line cursor is on
set cursorline
" Enable mouse support across all modes
set mouse=a
" Use 24-bit (true color) support
if exists('+termguicolors')
set termguicolors
endif
" Set command line height to 2 lines for more visibility
set cmdheight=2
" Show a visual indicator for lines that exceed a certain length (e.g., 80 characters)
set colorcolumn=80
" ==============================================================================
" Filetype-specific Enhancements
" ==============================================================================
" Enable filetype plugins
filetype plugin on
filetype indent on
" Syntax highlighting for Python, JavaScript, Markdown, Lua
au BufRead,BufNewFile *.py,*.js,*.md,*.lua setlocal syntax=on
" For Python: Set tab space
autocmd FileType python setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab
" For JavaScript: Set tab space
autocmd FileType javascript setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
" For Markdown: Enable spell checking and wrap text
autocmd FileType markdown setlocal spell textwidth=80 wrap
" For Lua: Set tab space
autocmd FileType lua setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
" ==============================================================================
" Additional Quality-of-Life Improvements
" ==============================================================================
" Use incremental search
set incsearch
" Highlight search results
set hlsearch
" Map <leader> to a space (or your preferred leader key)
let mapleader=" "
" Disable creating backup files
set nobackup
set nowritebackup
" Disable swap files
" Disable swap files to avoid clutter
set noswapfile
" Open new split panes to right and bottom, which feels more natural
set splitbelow
set splitright
" Enable clipboard to use system clipboard
set clipboard=unnamedplus
" Quiet startup
set shortmess+=I
" ==============================================================================
" Custom Key Bindings for Improved Navigation and Editing
" ==============================================================================
" Map <leader>w to save the current file
nnoremap <leader>w :w<CR>
" Map <leader>q to quit
nnoremap <leader>q :q<CR>
" ==============================================================================
" End of Enhancements
" ==============================================================================
```
- Configures basic editing behaviors and preferences.
- Sets up indentation and line number visibility for ease of reading and navigation.
This enhanced `.vimrc` configuration focuses on leveraging Vim's built-in features to improve the editing experience for Python, JavaScript, Markdown, and Lua, as well as general quality-of-life improvements. It sets up syntax highlighting, adjusts indentation and tab spaces according to common conventions for each language, enables spell checking for Markdown, and includes several navigation and editing conveniences.
### Search Enhancements
```vim
" Improve search functionality
set hlsearch " Highlight search results
set ignorecase " Case-insensitive searching
set smartcase " Case-sensitive if contains uppercase
set incsearch " Incremental search
```
- Enhances search functionality within Vim, making it easier to find text.
Keep in mind, while this setup uses Vim's built-in capabilities, exploring additional plugins can further extend functionality. However, as requested, this guide focuses on what can be achieved without external tools.
### Visual Customizations
```vim
" Visual settings for a better editing experience
colorscheme desert " Set color scheme
set t_Co=256 " Enable 256 colors
set laststatus=2 " Always display the status line
```
- Customizes the appearance of Vim, including color schemes and the status bar.
### Plugin Management
```vim
" Plugin management using Vim's native package loading
execute "set runtimepath^=~/.vim/pack/plugins/start"
```
- Outlines the method for managing plugins with Vim 8+ or Neovim's built-in package feature.
### Plugins Configuration (Examples)
```vim
" Example plugins that enhance Vim's functionality
" Plug 'tpope/vim-surround' " Easier manipulation of surrounding characters
" Plug 'preservim/nerdtree' " File system explorer
" Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } " Fuzzy file, buffer, mru, tag, etc. finder.
```
- Demonstrates how to include plugins in your `.vimrc`. Plugins like `vim-surround`, `nerdtree`, and `fzf` are popular choices for boosting productivity.
### Key Bindings
```vim
" Custom key bindings for faster workflow
let mapleader=" " " Set space as the leader key
nnoremap <leader>n :NERDTreeToggle<CR> " Toggle NERDTree with <leader>n
```
- Customizes key mappings, including setting a leader key for shortcuts.
### Custom Functions and Autocommands
```vim
" Place for custom Vim functions and autocommands
autocmd vimexit * if !argc() | NERDTreeClose | endif
" Example: Auto-close NERDTree on Vim exit if it's the last window
```
- A section for defining custom functions and autocommands for automating tasks.
### Filetype-specific Enhancements and Quality-of-Life Improvements
```vim
" Enhancements for specific file types and additional settings
au BufRead,BufNewFile *.py,*.js,*.md,*.lua setlocal syntax=on
" Enable syntax highlighting based on file type
" Quality-of-Life Improvements
set splitright " Vertical splits to the right
set splitbelow " Horizontal splits below
set clipboard=unnamedplus " Integrate Vim clipboard with the system clipboard
```
- Adjusts settings for specific file types and adds general improvements to make editing more pleasant.
This guide breaks down the `.vimrc` file into manageable sections, making it easier to understand and customize according to your needs. Remember, the best `.vimrc` is one that evolves with you, fitting your workflow and preferences as they develop over
time.