[Vim artifact] Configure the editing method that suits you, configuration reference (~/.vimrc)

Foreword:

      This blog is just a brief introduction to vim usage optimization. If readers want to know more about vim usage techniques, it is strongly recommended to read the second edition of "Vim Practical Tips".

        When writing code, in order to improve the readability and maintainability of the code, we often add some information tips at the head of the file, such as author, date, version number, etc. This article describes how to implement the function of automatically adding information prompts in the Vim editor.

       The ~/.vimr reference configuration is provided at the end, which can improve the speed of code writing (for example: automatic indentation, automatic completion, etc.)

Configuration:

        When using the Vim editor, we can use some configurations to automatically add information prompts when creating new files. How to set it up is explained in detail below. (two steps in total)

Step 1: Confirm whether you already have a `.vim` or `.vimrc` directory. If not, follow these steps to create one:

1. Open a terminal and enter your user home directory:

cd ~

2. Create `.vim` directory:

mkdir .vim

3. Enter the `.vim` directory:

cd .vim

4. Create the `templates` directory:

mkdir templates

5. Enter the `templates` directory:

cd templates

6. Create `skeleton.c`: ( template file )

vim skeleton.c

7. Copy the following template content to `skeleton.c`: ( can be modified according to your actual situation )

/*
 * Filename: %FILENAME%
 * Author: %AUTHOR%
 * Date: %DATE%
 * Version: %VERSION%
 *
 * Description: The purpose of this code.
 */

#include <stdio.h>
int main()
{
    
    return 0;
}

Step 2 : Modify Vim’s configuration file `~/.vimrc` and add the corresponding configuration. If the file does not exist, follow these steps to create it:

1. Enter the user home directory in the terminal:

cd ~

2. Create `.vimrc` file:

touch .vimrc

3. Open the `.vimrc` file using a text editor (such as Vim):

vim .vimrc

        In the `~/.vimrc` file, we can define a function to replace the placeholder in the template and call this function when creating a new file. The following is an example `~/.vimrc` configuration: (just add the following information)

" 获取当前用户名
let s:current_user = $USER

" 设定默认版本号
let s:default_version = "1.0"

function! ReplaceTemplateVariables(timer_id)
    let l:current_date = strftime("%Y-%m-%d")
    let l:current_filename = expand('%:t')

    " 替换对应的占位符
    execute "%s/%DATE%/" . l:current_date . "/g"
    execute "%s/%FILENAME%/" . l:current_filename . "/g"
    execute "%s/%AUTHOR%/" . s:current_user . "/g"
    execute "%s/%VERSION%/" . s:default_version . "/g"

    " 设置光标位置
    call cursor(13, 1)
endfunction

" 安装 Timer 插件(如果尚未安装)
if !exists('g:loaded_timer') && !exists('g:did_timer_plugin')
    silent! timer defer timer_start 1
    let g:did_timer_plugin = 1
endif

autocmd BufNewFile *.c 0r ~/.vim/templates/skeleton.c | call timer_start(100, 'ReplaceTemplateVariables')

        In this configuration, we first get the current user name and set the default version number. Then, a function `ReplaceTemplateVariables` is defined, which will be called when a new file is created. This function will get the current date and file name and replace this information.

Test: After the above configuration is completed, normal `vim file.c` can be verified.

Reference (~/.vimrc configuration):

The following is my .vimrc settings when programming: slowly improve according to your own habits (here for reference only)

" 获取当前用户名                                                                                                                    
let s:current_user = $USER

" 设定默认版本号
let s:default_version = "1.0"

function! ReplaceTemplateVariables(timer_id)
    let l:current_date = strftime("%Y-%m-%d")
    let l:current_filename = expand('%:t')

    " 替换对应的占位符
    execute "%s/%DATE%/" . l:current_date . "/g"
    execute "%s/%FILENAME%/" . l:current_filename . "/g"
    execute "%s/%AUTHOR%/" . s:current_user . "/g"
    execute "%s/%VERSION%/" . s:default_version . "/g"

    " 设置光标位置
    call cursor(10, 1)
endfunction

" 安装 Timer 插件(如果尚未安装)
if !exists('g:loaded_timer') && !exists('g:did_timer_plugin')
    silent! timer defer timer_start 1
    let g:did_timer_plugin = 1 
endif

autocmd BufNewFile *.c 0r ~/.vim/templates/skeleton.c | call timer_start(100, 'ReplaceTemplateVariables')

" 设置编码为 UTF-8
set encoding=utf-8

" 设置制表符宽度为4个空格
set tabstop=4
set shiftwidth=4
" 将制表符自动转换为空格
set expandtab

" 显示行号、标尺和相对行号
set number
set ruler
set relativenumber

" 自动缩进和智能缩进
set autoindent
set smartindent

" 显示匹配的括号
set showmatch

" 开启自动换行
set wrap

" 高亮显示当前行
set cursorline

" 代码折叠设置
" 单个折叠/打开(切换):zc/zo(za)                                                                                                 
" 全部折叠/打开:zM/zR
set foldmethod=indent "使用缩进来折叠代码
set foldlevel=99    "默认打开所有折叠

" 开启搜索时忽略大小写
set ignorecase
" 搜索时自动高亮匹配项
set hlsearch

" 自己设置快捷键,根据自己习惯设置即可
nmap H ^i
nmap L $a
nmap J 3j
nmap K 3k

" 自动补全括号,根据自己的习惯设置即可
inoremap ( ()<Left>
inoremap [ []<Left>
inoremap { {<CR>}<Esc>O
inoremap " ""<Left>
inoremap ' ''<Left>
inoremap ` ``<Left>

" 删除成对符号内的全部内容,保留符号
nnoremap di( di(bi<Right>
nnoremap di[ di[bi<Right>
nnoremap di' di'bi<Right>
nnoremap di" di"bi<Right><Right>
nnoremap di` di`bi<Right>

" 启用文件类型检测
filetype plugin on
filetype indent on

Summarize:

         According to your actual situation, you can copy and execute step by step. Please note that when copying vim content (you must enter the editing mode first, for example: press i) , if you have any questions, you can post them in the comment area.

Reference books:

"Vim Practical Tips" --->[US] Written by Drew Neil

Guess you like

Origin blog.csdn.net/crr411422/article/details/131541243