Update docs/tech_docs/linux/vim.md

This commit is contained in:
2024-04-14 16:05:06 +00:00
parent 8ebf460408
commit 17daecf04c

View File

@@ -1,3 +1,433 @@
## Vim Productivity Tips and Tricks
### Core Document Editing
1. Formatting paragraphs:
- `gqip` - Format the current paragraph to fit within the specified text width.
- `:set textwidth={width}` - Set the desired text width for formatting (e.g., `:set textwidth=80`).
- Example: Easily format long paragraphs in documents, emails, or README files.
2. Spell checking:
- `:set spell` - Enable spell checking.
- `]s` - Jump to the next misspelled word.
- `[s` - Jump to the previous misspelled word.
- `z=` - Suggest corrections for the misspelled word under the cursor.
- Example: Quickly identify and fix spelling errors in documents or text files.
3. Counting words, lines, and characters:
- `g Ctrl-g` - Display word, line, and character count for the current document.
- Example: Monitor the length of your writing in real-time, especially useful for meeting word count requirements.
### Programming and Code Editing
1. Commenting and uncommenting code:
- `gcc` - Comment out the current line using the language-specific comment syntax.
- `gc{motion}` - Comment out the lines covered by the specified motion (e.g., `gcip` comments out the current paragraph).
- Example: Quickly comment or uncomment blocks of code during development and debugging.
2. Indenting and unindenting code:
- `>>{motion}` - Indent the lines covered by the specified motion (e.g., `>}` indents the current code block).
- `<<{motion}` - Unindent the lines covered by the specified motion.
- Example: Easily adjust the indentation of code blocks to improve readability and adhere to coding conventions.
3. Jumping to function definitions:
- `[{` - Jump to the previous function definition.
- `]}` - Jump to the next function definition.
- Example: Quickly navigate between functions in a source code file, enhancing code comprehension and analysis.
### Media Creation and Editing
1. Editing multiple lines simultaneously:
- `Ctrl-v` - Enter visual block mode.
- Select the desired lines and columns using motion keys.
- `I{text} Esc` - Insert the specified text at the beginning of each selected line.
- Example: Efficiently add prefixes or suffixes to multiple lines in structured data files, such as CSV or TSV.
2. Sorting lines:
- `:sort` - Sort the lines in the current selection or entire file.
- `:sort!` - Sort the lines in reverse order.
- Example: Quickly sort lists, data entries, or log files for better organization and analysis.
3. Aligning text:
- `:left`, `:center`, or `:right` - Left-align, center-align, or right-align the current selection or line.
- Example: Format text-based tables, lists, or presentations to improve readability and visual appeal.
### Advanced Search and Replace
1. Searching for patterns across multiple files:
- `:vimgrep /{pattern}/g {files}` - Search for the specified pattern in the given files.
- `:cn` - Jump to the next match.
- `:cp` - Jump to the previous match.
- Example: Find occurrences of a specific keyword or code snippet across a project or directory.
2. Replacing with confirmation:
- `:%s/{pattern}/{replacement}/gc` - Replace all occurrences of the pattern with the replacement text, prompting for confirmation before each replacement.
- Example: Selectively replace text or code, reviewing each change before applying it.
3. Saving and reusing search patterns:
- `/` - Enter search mode and type the desired pattern.
- `:let @/ = '{pattern}'` - Save the current search pattern to the search register.
- `n` - Jump to the next occurrence of the saved search pattern.
- Example: Quickly reuse complex search patterns across multiple editing sessions.
### Workflow Integration
1. Executing external commands:
- `:!{command}` - Execute the specified external command in the shell.
- Example: Run code formatters, linters, or build tools without leaving Vim.
2. Filtering text through external commands:
- `{range}!{command}` - Pass the specified range of lines through an external command and replace the content with the command's output.
- Example: Format code, minify CSS/JS, or convert data using external tools seamlessly within Vim.
3. Managing sessions:
- `:mksession {file}` - Save the current Vim session to a file.
- `:source {file}` - Load a previously saved Vim session.
- Example: Preserve and restore complex workspace setups, including open files, splits, and settings.
Remember, these examples are just the tip of the iceberg! Vim's flexibility and extensive feature set allow you to adapt these techniques to your specific needs and workflows. Experiment, combine, and customize these tips to unlock new levels of productivity in your editing process.
# Mastering Data Manipulation and Analysis with Vim
## Introduction
As a data analyst or engineer, you often find yourself working with a variety of structured data files, such as XML, YAML, JSON, CSV, SQL, and more. Efficiently navigating, manipulating, and analyzing these files is crucial for your productivity. Vim, coupled with powerful command-line tools like sed and awk, provides a robust and flexible environment for tackling data-related tasks.
In this guide, we'll explore advanced techniques, practical examples, and valuable tips to help you master data manipulation and analysis using Vim and its ecosystem of tools.
## 1. Navigating and Editing Structured Data Files
### 1.1 Syntax Highlighting and Folding
- Enable syntax highlighting for better readability:
- `:set syntax=xml`, `:set syntax=json`, etc.
- Use folding to collapse and expand sections:
- `zi` - Toggle folding.
- `zc` - Close the current fold.
- `zo` - Open the current fold.
- Example: Collapse and expand XML tags, JSON objects, or YAML blocks for easier navigation.
### 1.2 Jumping to Specific Sections
- Use Vim's built-in search features to quickly navigate to specific sections:
- `/pattern` - Search forward for a pattern.
- `?pattern` - Search backward for a pattern.
- `n` - Jump to the next search match.
- `N` - Jump to the previous search match.
- Example: Search for specific XML tags, JSON keys, or CSV headers.
### 1.3 Formatting and Indentation
- Automate indentation and formatting for structured files:
- `gg=G` - Indent the entire file.
- `={motion}` - Indent the specified lines or motion.
- Example: Automatically format JSON files or SQL queries for better readability.
## 2. Data Transformation and Manipulation
### 2.1 Regular Expressions and Substitution
- Leverage Vim's powerful regular expressions for complex data transformations:
- `:%s/{regex}/{replacement}/g` - Replace all occurrences of the regex pattern with the replacement text.
- Example: Extract specific fields from CSV files, replace XML tags, or reformat JSON data.
### 2.2 Filtering and Sorting
- Use external tools like sed and awk for advanced filtering and sorting:
- `:%!sed 's/{pattern}/{replacement}/g'` - Filter and replace using sed.
- `:%!awk '{print $1, $3}'` - Filter and rearrange columns using awk.
- Example: Extract specific columns from CSV files, filter log entries based on patterns, or sort data based on a key.
### 2.3 Batch Processing and Macros
- Automate repetitive data manipulation tasks using Vim macros:
- `q{register}` - Start recording a macro in the specified register.
- `q` - Stop recording the macro.
- `@{register}` - Play back the recorded macro.
- Example: Apply a series of transformations to multiple lines or files, such as formatting CSV data or updating JSON fields.
## 3. Integrating with Command-Line Tools
### 3.1 Executing External Commands
- Run command-line tools directly from Vim:
- `:!{command}` - Execute the specified command in the shell.
- Example: Run data processing scripts, invoke data analysis tools, or trigger data pipelines.
### 3.2 Piping and Redirection
- Pipe Vim buffer content through external commands and redirect the output:
- `:{range}!{command}` - Pipe the specified range of lines through an external command.
- `:{range}w !{command}` - Redirect the specified range of lines to an external command.
- Example: Pipe SQL query results to a formatting tool or redirect JSON data to a validation script.
### 3.3 Vim as a Command-Line Tool
- Use Vim as a powerful command-line tool for data processing:
- `vim -c '{command}' {file}` - Open a file and execute the specified Vim command.
- Example: Perform bulk search and replace operations, reformat data files, or apply complex transformations.
## 4. Data Visualization and Reporting
### 4.1 ASCII Tables and Graphs
- Create ASCII tables and graphs directly in Vim:
- Use Vim's drawing capabilities to create tables and borders.
- Leverage external tools like gnuplot or ASCII charting libraries.
- Example: Generate quick data summaries, visualize trends, or create simple reports.
### 4.2 Markdown and Reporting
- Use Vim's Markdown support for generating data reports:
- `:set filetype=markdown` - Set the file type to Markdown.
- Use Markdown syntax for headers, tables, and formatting.
- Example: Create structured data reports, documentation, or analysis summaries.
## 5. Best Practices and Tips
### 5.1 Customizing Vim for Data Work
- Tailor Vim settings and mappings for data manipulation tasks:
- Create custom mappings for frequently used commands or tools.
- Customize syntax highlighting and file type detection.
- Example: Set up a dedicated Vim configuration for data analysis projects.
### 5.2 Vim Plugins for Data Analysis
- Explore Vim plugins specifically designed for data manipulation and analysis:
- CSV plugins for enhanced CSV file handling.
- JSON plugins for formatting and validation.
- Database plugins for querying and interacting with databases.
- Example: Streamline your data workflows with specialized plugins.
### 5.3 Collaboration and Version Control
- Integrate Vim with version control systems for collaborative data work:
- Use Vim's diff mode to compare and merge data files.
- Leverage plugins for seamless integration with Git, SVN, or other VCS.
- Example: Track changes, review data modifications, and collaborate with peers.
## Conclusion
By mastering Vim's advanced features and integrating it with powerful command-line tools, you can significantly enhance your productivity and efficiency when working with structured data files and performing data analysis tasks. The combination of Vim's flexibility, regex capabilities, and seamless integration with external tools makes it an indispensable asset in your data manipulation and analysis workflow.
Remember to practice these techniques regularly, adapt them to your specific needs, and continuously explore new ways to leverage Vim's capabilities in your data-related tasks. Happy data analysis and manipulation with Vim!
# Advanced Data Manipulation and Analysis with Vim
## 6. Data Pipelines and Preprocessing
### 6.1 Building Data Pipelines with Vim
- Use Vim as the centerpiece of your data preprocessing pipeline:
- Combine Vim commands with external tools to create powerful data transformations.
- Chain multiple Vim commands and external tools using pipes and redirections.
- Example: Preprocess large datasets, clean and transform data, and prepare it for analysis.
### 6.2 Automating Data Preprocessing Tasks
- Create Vim scripts and functions to automate common preprocessing tasks:
- Write custom Vim functions to encapsulate complex data transformations.
- Use Vim's built-in scripting capabilities to create reusable preprocessing scripts.
- Example: Automate data cleaning, normalization, or feature extraction tasks.
### 6.3 Batch Processing and Parallel Execution
- Leverage Vim's batch processing capabilities for efficient data processing:
- Use Vim's `argdo` and `bufdo` commands to apply commands across multiple files.
- Combine Vim with parallel execution tools like GNU Parallel for faster processing.
- Example: Preprocess multiple data files simultaneously or distribute data processing across multiple cores.
## 7. Scripting and Automation
### 7.1 Vimscript for Data Manipulation
- Harness the power of Vimscript to create custom data manipulation functions:
- Write Vimscript functions to encapsulate complex data transformations.
- Use Vimscript variables, loops, and conditionals for flexible data processing.
- Example: Create custom functions to parse and transform specific data formats.
### 7.2 Integrating with Shell Scripts
- Combine Vim with shell scripting for advanced data processing tasks:
- Call shell scripts from within Vim to perform external data manipulations.
- Pass data between Vim and shell scripts using pipes and redirections.
- Example: Integrate Vim with data processing shell scripts or command-line tools.
### 7.3 Vim as a Scripting Environment
- Use Vim as a complete scripting environment for data analysis:
- Leverage Vim's built-in features and plugins to create data analysis scripts.
- Combine Vim with other scripting languages like Python or Lua for extended functionality.
- Example: Create data analysis scripts that combine Vim's text manipulation capabilities with statistical computations.
## 8. Integration with Programming Languages
### 8.1 Vim and Python for Data Analysis
- Integrate Vim with Python for powerful data analysis capabilities:
- Use Vim as a Python development environment with plugins like `vim-python`.
- Execute Python code directly from Vim and capture the results.
- Example: Perform data analysis tasks, create visualizations, or interact with data libraries using Python within Vim.
### 8.2 Vim and R for Statistical Analysis
- Combine Vim with R for statistical analysis and data visualization:
- Use Vim as an R development environment with plugins like `Nvim-R`.
- Execute R code from Vim and view the results directly in the editor.
- Example: Perform statistical tests, create plots, or run R scripts seamlessly within Vim.
### 8.3 Vim and SQL Databases
- Interact with SQL databases directly from Vim:
- Use Vim plugins like `dbext` or `vim-dadbod` to connect to databases.
- Execute SQL queries, browse result sets, and manipulate data from within Vim.
- Example: Query databases, explore data schemas, or perform data transformations using SQL within Vim.
## 9. Data Exploration and Visualization
### 9.1 Interactive Data Exploration
- Use Vim as an interactive environment for data exploration:
- Leverage Vim's split windows and buffers to compare and analyze data side by side.
- Use Vim's search and filter capabilities to quickly find patterns and insights.
- Example: Explore large datasets, compare data subsets, or investigate anomalies interactively.
### 9.2 Data Visualization with Vim
- Create data visualizations directly within Vim:
- Use ASCII charting libraries or plugins to render charts and graphs.
- Integrate Vim with external data visualization tools for more advanced visualizations.
- Example: Create quick data visualizations, plot trends, or visualize data distributions.
### 9.3 Literate Programming and Notebooks
- Combine Vim with literate programming tools for reproducible data analysis:
- Use Vim as a notebook environment with plugins like `vim-notebook`.
- Mix code, documentation, and results in a single Vim buffer for literate programming.
- Example: Create data analysis notebooks, document your analysis process, and share reproducible results.
## 10. Collaboration and Reproducibility
### 10.1 Collaborative Data Analysis with Vim
- Use Vim's collaboration features for team-based data analysis:
- Leverage Vim's built-in diff and merge tools for comparing and merging data files.
- Use Vim's version control integration for tracking data changes and collaborating with others.
- Example: Share data analysis scripts, collaborate on data preprocessing tasks, or review data changes.
### 10.2 Reproducible Data Workflows
- Ensure reproducibility in your data analysis workflows with Vim:
- Use Vim's project-specific configurations and settings for consistent environments.
- Document your data analysis steps, dependencies, and configurations within Vim.
- Example: Create reproducible data analysis projects, share analysis workflows, or enable others to replicate your results.
### 10.3 Integration with Reproducibility Tools
- Combine Vim with reproducibility tools and platforms:
- Integrate Vim with tools like Docker or Jupyter for reproducible data analysis environments.
- Use Vim as a front-end for accessing and manipulating data in reproducible workflows.
- Example: Create reproducible data analysis containers, interact with Jupyter notebooks, or integrate Vim with workflow management systems.
## Conclusion
By leveraging Vim's advanced features, scripting capabilities, and integration with various tools and programming languages, you can create powerful and efficient data manipulation and analysis workflows. Whether you're preprocessing large datasets, performing statistical analysis, or creating visualizations, Vim provides a flexible and extensible environment for all your data-related tasks.
Remember to experiment with different techniques, customize Vim to suit your specific needs, and continuously learn from the vibrant Vim community. With practice and exploration, you'll be able to tackle even the most complex data challenges using Vim as your go-to tool.
Happy data manipulation and analysis with Vim!
Certainly! Let's break down the provided `.vimrc` file and explain each section to help you understand how to build your own Vim configuration. We'll go through the file section by section, providing explanations and best practices along the way.
```vim
" General Settings
set number " Show line numbers
set tabstop=4 " Number of spaces that a <Tab> in the file counts for
set shiftwidth=4 " Number of spaces to use for each step of (auto)indent
set expandtab " Use spaces instead of tabs
set smartindent " Enable smart indent
set autoindent " Enable auto indenting
set wrap " Wrap lines
set linebreak " Break lines at word (requires 'wrap')
set nocompatible " Turn off compatibility with legacy vi
filetype plugin indent on " Enable filetype detection for automatic indenting
set backspace=indent,eol,start " Make backspace key more powerful
syntax enable " Turn on syntax highlighting
set showcmd " Show command in bottom bar
set wildmenu " Visual autocomplete for command menu
set ignorecase " Ignore case when searching
set smartcase " Override ignorecase if search pattern contains uppercase characters
set incsearch " Show search matches as you type
set hlsearch " Highlight search matches
```
This section contains general settings for Vim. Here's what each setting does:
- `set number`: Displays line numbers in the editor.
- `set tabstop=4`: Sets the number of spaces that a tab character counts for.
- `set shiftwidth=4`: Sets the number of spaces to use for each step of indentation.
- `set expandtab`: Replaces tabs with spaces.
- `set smartindent` and `set autoindent`: Enables intelligent and automatic indentation.
- `set wrap` and `set linebreak`: Wraps long lines and breaks them at word boundaries.
- `set nocompatible`: Disables compatibility with legacy vi, enabling Vim-specific features.
- `filetype plugin indent on`: Enables filetype detection for automatic indentation based on file type.
- `set backspace=indent,eol,start`: Allows the backspace key to delete indentation, line breaks, and beyond the start of the current line.
- `syntax enable`: Enables syntax highlighting.
- `set showcmd`: Shows the command being typed in the bottom bar.
- `set wildmenu`: Enables visual autocomplete for the command menu.
- `set ignorecase` and `set smartcase`: Configures case-insensitive searching, but overrides it if the search pattern contains uppercase characters.
- `set incsearch` and `set hlsearch`: Highlights search matches as you type and after the search is completed.
```vim
" Enhanced File Type Support
augroup filetype_html_markdown_csv_sql
autocmd!
autocmd FileType html,markdown,csv,sql setlocal tabstop=2 shiftwidth=2 expandtab
augroup END
```
This section defines an `augroup` (autocmd group) for enhanced file type support. It sets specific settings for HTML, Markdown, CSV, and SQL files. The `autocmd!` command clears any existing autocommands in the group to prevent duplicates. The `autocmd FileType` command sets `tabstop`, `shiftwidth`, and `expandtab` to 2 for the specified file types, ensuring consistent indentation.
```vim
" Python-specific settings
let python_highlight_all=1
set fileformat=unix
au BufNewFile,BufRead *.py \
\ setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab autoindent fileencoding=utf-8
```
This section contains Python-specific settings. It enables enhanced Python syntax highlighting with `let python_highlight_all=1`. The `set fileformat=unix` command sets the file format to Unix-style line endings. The `au BufNewFile,BufRead *.py` autocommand sets specific settings for Python files, including tab settings, automatic indentation, and file encoding.
```vim
" Enhanced Syntax Highlighting with Plugins (assuming use of a plugin manager)
" Plug 'sheerun/vim-polyglot' " Uncomment if you use Vim-Plug
" Plug 'plasticboy/vim-markdown' " Markdown-specific enhancements
```
This section is commented out but shows an example of how to enhance syntax highlighting using plugins. In this case, it suggests using the `vim-polyglot` plugin for enhanced syntax support and the `vim-markdown` plugin for Markdown-specific enhancements. These lines would be uncommented if you are using a plugin manager like Vim-Plug.
```vim
" Mapping keys for more efficient navigation and editing
nnoremap <Space> <Nop>
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
nnoremap <C-h> <C-w>h
```
This section defines custom key mappings for more efficient navigation and editing. The `nnoremap` command creates a non-recursive normal mode mapping. In this case, it maps:
- `<Space>` to `<Nop>`, which disables the default behavior of the spacebar.
- `<C-j>`, `<C-k>`, `<C-l>`, and `<C-h>` to window navigation commands for moving between split windows.
```vim
" Disable arrow keys in normal mode to encourage hjkl navigation
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
```
This section disables the arrow keys in normal mode to encourage using the `hjkl` keys for navigation. It maps the arrow keys to `<Nop>`, effectively disabling them.
```vim
" Plugin Management Example with Vim-Plug (Uncomment to use)
" call plug#begin('~/.vim/plugged')
" Plug 'sheerun/vim-polyglot' " Enhanced syntax support
" Plug 'plasticboy/vim-markdown' " Markdown-specific enhancements
" call plug#end()
" Remember to install plugins with :PlugInstall if you use Vim-Plug
```
This section provides an example of how to manage plugins using the Vim-Plug plugin manager. The lines are commented out, but if you uncomment them and have Vim-Plug installed, you can use the `plug#begin()` and `plug#end()` functions to specify the plugins you want to use. In this case, it includes the `vim-polyglot` and `vim-markdown` plugins. After uncommenting and saving the file, you would need to run `:PlugInstall` in Vim to install the plugins.
```vim
set rtp+=/opt/homebrew/opt/fzf
```
This line adds the `fzf` (fuzzy finder) plugin to Vim's runtime path. It assumes that `fzf` is installed using Homebrew on macOS at the specified location.
Remember to customize the `.vimrc` file based on your preferences and needs. You can add or remove settings, plugins, and mappings as desired. It's also a good idea to document your configuration with comments to make it easier to understand and maintain.
I hope this breakdown of the `.vimrc` file helps you understand how to build your own Vim configuration! Let me know if you have any further questions.
---
# Vim Guide
## Introduction