当前位置: 首页 > 工具软件 > pathogen.vim > 使用案例 >

【Linux vim 入门及渐进过程2 - vim 常用插件配置】

劳仲渊
2023-12-01

1.1 vim 常用配置

1.1.1 vim 别名 alias 配置

alias v='vim'
alias r='rm'
#alias .='cd -'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

1.1.2 vim 终端颜色配置

# di=35: blue
# fi=36 cyan
# *.sh=33 orange
LS_COLORS='no=00:di=35;01:tw=33;02:ow=33;01'
LS_COLORS=$LS_COLORS':fi=36:ln=00:pi=00:so=00:bd=00:cd=00:or=00:mi=00:ex=00'
LS_COLORS=$LS_COLORS':*.sh=33:*.sh=33:*.exe=31:*.bat=31:*.com=31'
export LS_COLORS

1.1.3 vim cscope .barshrc 函数配置

vim 中配置 cscope 工具来看代码的时候,如查看某个函数的定义,这个时候可能会跳转出来多个文件,但是我们只关注 arch/armarch/arm64 目录下该函数的实现,这个时候我们就需要在生成 cscope.files 的时候过滤掉不关注的目录,按照下面方式在 ~/.bashrc 目录下实现下面函数 mycscope ,在这个函数中过滤掉不关注的目录即可。

function mycscope()
{
    CODE_PATH=`pwd`
    echo "$CODE_PATH"
    echo "start cscope...."

    if [ ! -f "$CODE_PATH/cscope.files" ];then
        echo "cscope.files not exist!"
    else
        rm -f $CODE_PATH/cscope.*
    fi

    find $PWD -path "./rtos/rt-thread/rt-thread/bsp/mb9*" -prune          \
            -o -path "./rtos/rt-thread/rt-thread/bsp/at91*" -prune      \
            -o -path "./rtos/rt-thread/rt-thread/bsp/ess*" -prune       \
            -o -path "./rtos/rt-thread/rt-thread/bsp/gd3*" -prune       \
            -o -path "./rtos/rt-thread/rt-thread/bsp/lpc*" -prune       \
            -o -path "./rtos/rt-thread/rt-thread/bsp/ls*" -prune        \
            -o -path "./rtos/rt-thread/rt-thread/bsp/mi*" -prune        \
            -o -path "./rtos/rt-thread/rt-thread/bsp/imx*" -prune       \
            -o -path "./rtos/rt-thread/rt-thread/bsp/rasp*" -prune      \
            -o -path "./rtos/rt-thread/packages/packages/iot/*" -prune  \
            -o -path "./rtos/rt-thread/rt-thread/bsp/stm32/stm32f*" -prune \
            -o -path "./bootrom" -prune                                 \
            -o -path "./u-boot" -prune                                  \
            -o -path "./tools" -prune                                   \
            -o -path "./arch/arc" -prune                                \
            -o -path "./arch/alpha" -prune                              \
            -o -path "./arch/blackfin" -prune                           \
            -o -path "./arch/cris" -prune                               \
            -o -path "./arch/h8300" -prune                              \
            -o -path "./arch/ia64" -prune                               \
            -o -path "./arch/m68k" -prune                               \
            -o -path "./arch/microblaze" -prune                         \
            -o -path "./arch/mn10300" -prune                            \
            -o -path "./arch/openrisc" -prune                           \
            -o -path "./arch/powerpc" -prune                            \
            -o -path "./arch/score" -prune                              \
            -o -path "./arch/sparc" -prune                              \
            -o -path "./arch/um" -prune                                 \
            -o -path "./arch/x86" -prune                                \
            -o -path "./arch/c6x" -prune                                \
            -o -path "./arch/m32r" -prune                               \
            -o -path "./arch/microblaze" -prune                         \
            -o -path "./arch/nios2" -prune                              \
            -o -path "./arch/powerpc" -prune                            \
            -o -path "./arch/sh" -prune                                 \
            -o -path "./arch/um" -prune                                 \
            -o -path "./arch/xtensa" -prune                             \
            -o -path "./arch/cris" -prune                               \
            -o -path "./arch/hexagon" -prune                            \
            -o -path "./arch/mips" -prune                               \
            -o -path "./arch/openrisc" -prune                           \
            -o -path "./arch/s390" -prune                               \
            -o -path "./arch/sparc" -prune                              \
            -o -path "./arch/unicore32" -prune                          \
            -o -path "./arch/parisc" -prune                             \
            -o -path "./arch/tile" -prune                               \
            -name "*.cc" -o -name "*.[chxsS]" -print > cscope.files
            echo "cscope -Rbkq -i cscope.files"
            cscope -Rbkq -i $PWD/cscope.files
    echo "cscope finished"
}

vim+ctags+cscopemac os 或者 redhat 应用时,经常出现使用 cscope 出现错误: E259: no matches found for csope query xx 问题。经过检查是用下面的命令生成 cscope 数据文件出错了,可以按照下面方式修改测试下:

find . -name "*.h" -o -name "*.c" -o -name "*.s" >cscope.files && cscpe -Rbkqi cscope.files
//改为:
find ./ -name "*.h" -o -name "*.c" -o -name "*.s" >cscope.files  && cscpe -Rbkqi cscope.files

1.1.4 vim 二进制文件查看

vim -b rtthread.bin
:%!xxd -e

默認使用的是大端顯示,使用 “-e” 之後使用小端顯示。

1.1.5 vimrc 配置

""https://www.cnblogs.com/dylancao/p/11220510.html
set rtp+=~/.vim/bundle/Vundle.vim
filetype off
filetype plugin indent on
syntax on
set hidden

set nocompatible
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"                     MobaXterm backspace sends ^H
"
" MobaXterm also proposes a checkbox setting "Backspace sends ^H" that
" you can try to toggle (in MobaXterm global settings --> "Terminal" tab).
"
" If you are using a saved session, you will have to edit this session,
" go to the "Terminal settings" tab and toggle the "Backspace sends ^H"
" checkbox.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set backspace=2

set showcmd
set hls
set nu
set cursorline
set showmatch
set timeoutlen=4000
set history=400
set nobackup
set magic
"Include search
set incsearch
set ignorecase
"Highlight search things
set hlsearch
set cindent
set scrolloff=5
set autoindent
set tags=tags
set autochdir
set textwidth=80
"set spell spelllang=en_us
set laststatus=2
highlight StatusLine cterm=bold ctermfg=yellow ctermbg=blue
set statusline=[%F]%y%r%m%*%=[L:%l/%L,C:%c][%p%%]
colorscheme default
""colorscheme industry
""syntax enable
""set background=dark
""colorscheme solarized

"""""""""""""""""""""""""""""""""""""""
" 初始化NERDTree变量
"""""""""""""""""""""""""""""""""""""""
nmap <F2> :NERDTree <CR>
nmap <F2> :NERDTreeToggle<CR>
let g:NERDTreeGlyphReadOnly=0
let g:NERDTreeDirArrows = 1
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'

""""""""""""""""""""""""""""""
" Tag list (ctags)
""""""""""""""""""""""""""""""
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
let Tlist_Show_One_File = 1            "不同时显示多个文件的tag,只显示当前文件的
let Tlist_Exit_OnlyWindow = 1          "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1         "在右侧窗口中显示taglist窗口
let Tlist_WinWidth=25
map <silent> <leader>tl :TlistToggle<cr>
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

map <C-Right> <C-W>H
map <C-Up> <C-W>K
map <C-Down> <C-W>J
map <C-Left> <C-W>L
set backspace=2
map <leader>sa ggVG"

""^I:tab,$:enter
""nnoremap <F3> :set list! list?<CR>
""nnoremap <F4> :set nohls! nohls?<CR>
nnoremap <F5> :set nu! nu?<CR>
nnoremap <F6> :exec exists('syntax_on') ? 'syn off' : 'syn on'<CR>
""map <F7> <esc>ort_kprintf("schan %s %d\n", __func__, __LINE__);<esc>
nnoremap ; :
nnoremap <S-d> :vertical resize -1<CR>
nnoremap <S-a> :vertical resize +1<CR>
nnoremap <silent> n nzz
nnoremap <silent> N Nzz
nnoremap <leader>fu :CtrlP<CR>

""""""""""""""""""""""""""""""""""""""""""""
" bracket and quotation marks auto complete
" """"""""""""""""""""""""""""""""""""""""""
inoremap ' ''<ESC>i
inoremap " ""<ESC>i
inoremap ( ()<ESC>i
inoremap < <><ESC>i
inoremap [ []<ESC>i
inoremap { {<CR>}<ESC>O
""inoremap jj <ESC>la
""inoremap <C-l> <esc>%%a
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
"""""""""""""""""""""""""""""""""""""""
" Used to show the spaces in code text
"""""""""""""""""""""""""""""""""""""""
highlight ExtraWhitespace ctermbg=red guibg=darkgreen
autocmd ColorScheme * highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$\| \+\ze\t/


execute pathogen#infect()
syntax on
filetype plugin indent on
"""""""""""""""""""""""""""""""""""""""
" Supertable Setting
" <leader> key is "\"
"""""""""""""""""""""""""""""""""""""""
let g:SuperTabDefaultCompletionType = "<c-n>"
let g:SuperTabRetainCompletionType=2
let g:SuperTabMappingForward = "<tab>"
let g:SuperTabMappingBackward= "s-tab"

"""""""""""""""""""""""""""""""""""""""
" Ctrlvim Setting
" <leader> key is "\"
"""""""""""""""""""""""""""""""""""""""
let g:ctrlp_cmd = 'CtrlP'
map fu :CtrlPMRU<CR>
let g:ctrlp_custom_ignore = {
    \ 'dir':  '\v[\/]\.(git|hg|svn|rvm)$',
    \ 'file': '\v\.(exe|so|dll|zip|tar|tar.gz|pyc)$',
    \ }
let g:ctrlp_match_window_bottom=1
let g:ctrlp_max_height=20
let g:ctrlp_match_window_reversed=0
let g:ctrlp_mruf_max=500
let g:ctrlp_follow_symlinks=1
let g:ctrlp_working_path_mode ='ra'

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nmap <C-\>g :cs f g <C-R><C-W><CR>
nmap <C-\>s :cs f s <C-R><C-W><CR>
nmap <C-\>c :cs f c <C-R><C-W><CR>
nmap <C-\>t :cs f t <C-R><C-W><CR>
nmap <C-\>e :cs f e <C-R><C-W><CR>
nmap <C-\>f :cs f f <C-R><C-W><CR>
nmap <C-\>i :cs f i ^<C-R><C-W>$<CR>
nmap <C-\>d :cs f d <C-R><C-W><CR>
""if has("cscope")
""  set csprg=/usr/bin/cscope
""  set csto=1
""  set cst
""  set nocsverb
""  set timeoutlen=2000
""  " add any database in current directory
""  if filereadable("cscope.out")
""      cs add cscope.out
""  endif
""  set csverb
""endif

""""""""""""""""""""""""""""""""""""""""
" https://github.com/luochen1990/rainbow
""""""""""""""""""""""""""""""""""""""""
let g:rainbow_active = 1 "set to 0 if you want to enable it later via :RainbowToggle

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"remember last update or view postion"
" Only do this part when compiled with support for autocommands
" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if has("autocmd")
" In text files, always limit the width of text to 78 characters
autocmd BufRead *.txt set tw=78
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif
endif

function! SetLinuxKernel()
        :set noexpandtab
        :set tabstop=8
        :set softtabstop=8
        :set shiftwidth=8
endfunc

function! SetLinuxUser()
        :set expandtab
        :set tabstop=4
        :set softtabstop=4
        :set shiftwidth=4
endfunc

nmap <F7> :call SetLinuxKernel()<CR>
nmap <F7><F7> :call SetLinuxUser()<CR>

function! OpenShowtTableSin()
        :set list
        :set listchars=tab:>-,trail:-
endfunc

function! CloseShowtTableSin()
        :set nolist
endfunc
nmap <F9> :call OpenShowtTableSin()<CR>
nmap <F9><F9> :call CloseShowtTableSin()<CR>

""""""""""""""""""""""""""""""""""""""""""""""
" syntax off for vimdiff
""""""""""""""""""""""""""""""""""""""""""""""
if &diff
        colorscheme solarized
        set background=dark
        set number
        syntax off
endif

""""""""""""""""""""""""""""""""""""""
" https://github.com/mbbill/undotree
" use F6 to call undotree
" tail -F ~/undotree_debug.log
""""""""""""""""""""""""""""""""""""""
nnoremap <F6> :UndotreeToggle<CR>
if has("persistent_undo")
   let target_path = expand('~/.undodir')

    " create the directory and any parent directories
    " if the location does not exist.
    if !isdirectory(target_path)
        call mkdir(target_path, "p", 0700)
    endif

    let &undodir=target_path
    set undofile
endif

1.2 vim 常用插件

1.2.1 vim 插件 Pathogen 管理

我一般使用 Pathogen 管理器安装 vim 插件, 那么什么是 Pathogen 呢? Pathogen 一般作为 vim 新手的第一个插件,用来统一管理 vim 插件包。(官方解释)非常容易地管理你的 ‘runtimepath’ , 在实际项目中,vim-pathogen 可以在它的私有文件夹下非常轻松地安装插件和管理运行时文件。

Pathogen 安装
复制以下代码到你的终端

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://gitee.com/jojo2019007/vim-pathogen/raw/master/autoload/pathogen.vim

添加以下代码到 ~/.vimrc 文件中

execute pathogen#infect()
syntax on
filetype plugin indent on

此时 Pathogen 已经成功安装,可以安装以下插件测试下

cd ~/.vim/bundle && 
git clone https://github.com/tpope/vim-sensible.git

1.2.2 vim 常用插件推荐

目前我主要使用以下 vim 插件:

CondingCos:~/.vim$ ls
autoload  bundle  doc  plugin  README.md

CondingCos:~/.vim/bundle$ ls
autoload_cscope.vim  ctrlp.vim  fzf.vim  nerdtree  supertab  undotree  
vim-colors-solarized  vim-commentary  vinarise.vim  LeaderF  ripgrep

1.2.3 vim Leaderf

由于我使用的 git submoudle 管理本地 vim 插件,首先使用 git submodule add 命令将远程项目 Leaderf 和 ripgrep 克隆到本地 ~/.vim/bundle 文件夹下,具体步骤如下:

CodingCos@developer-numb-1:~/.vim/bundle$
CodingCos@developer-numb-1:~/.vim/bundle$ git submodule add git@github.com:BurntSushi/ripgrep.git
Cloning into '/mnt/user_home/CodingCos/.vim/bundle/ripgrep'...
remote: Enumerating objects: 10128, done.
remote: Counting objects: 100% (181/181), done.
remote: Compressing objects: 100% (103/103), done.
remote: Total 10128 (delta 115), reused 123 (delta 78), pack-reused 9947
Receiving objects: 100% (10128/10128), 4.35 MiB | 3.09 MiB/s, done.
Resolving deltas: 100% (6866/6866), done.
CodingCos@developer-numb-1:~/.vim/bundle$
CodingCos@developer-numb-1:~/.vim/bundle$ git submodule add git@github.com:Yggdroot/LeaderF.git
Cloning into '/mnt/user_home/CodingCos/.vim/bundle/LeaderF'...
remote: Enumerating objects: 7616, done.
remote: Counting objects: 100% (778/778), done.
remote: Compressing objects: 100% (299/299), done.
remote: Total 7616 (delta 435), reused 692 (delta 379), pack-reused 6838
Receiving objects: 100% (7616/7616), 2.03 MiB | 1.68 MiB/s, done.
Resolving deltas: 100% (4584/4584), done.
CodingCos@developer-numb-1:~/.vim/bundle$
CodingCos@developer-numb-1:~/.vim/bundle$ git submodule status
 43433aa8dad1601acf923d3c25fa3325799508aa LeaderF (v1.23-131-g43433aa)
 26f428f400d96d25a9d633e6314f6e1760923db1 autoload_cscope.vim (0.5)
 564176f01d7f3f7f8ab452ff4e1f5314de7b0981 ctrlp.vim (1.79-29-g564176f)
 d5f1f8641b24c0fd5b10a299824362a2a1b20ae0 fzf.vim (d5f1f86)
 eed488b1cd1867bd25f19f90e10440c5cc7d6424 nerdtree (6.10.16-1-geed488b)
 bc5504932764d8d4735bf955f6f7e04a95f151b8 ripgrep (grep-0.2.10-55-gbc55049)
 f0093ae12a9115498f887199809a6114659fc858 supertab (2.1-41-gf0093ae)
 08e259be24d4476c1ee745dc735eefd44f90efdc undotree (rel_4.2-147-g08e259b)
 528a59f26d12278698bb946f8fb82a63711eec21 vim-colors-solarized (heads/master)
 3654775824337f466109f00eaf6759760f65be34 vim-commentary (v1.3-20-g3654775)
 84dd647932fbd029310cca31f417c42f56d60547 vinarise.vim (ver.1.0-140-g84dd647)

note: 需要登陆自己的 github,再使用 git submodule add, 否是 执行不成功

执行完上面命令后,需要将对应的文件推到 自己的 github

CodingCos@developer-numb-1:~/.vim$ git st .
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitmodules
        new file:   bundle/LeaderF
        new file:   bundle/ripgrep
CodingCos@developer-numb-1:~/.vim$ git commit -a -s
CodingCos@developer-numb-1:~/.vim$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
CodingCos@developer-numb-1:~/.vim$ git push origin HEAD:origin/main
Counting objects: 4, done.
Delta compression using up to 64 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 549 bytes | 549.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:xxxx/.vim.git

note: 使用Leaderf之前需要先安装 ripgrep

1.2.4 ripgrep 工具

ripgrep(简称 rg),是一个用 Rust 实现的命令行搜索工具,可以通过正则来搜索当前的目录。默认情况下 ripgrep 会遵循 .gitignore 的内容,并且自动跳过隐藏的文件目录,以及二进制文件

可以通过下面命令进行安装:

sudo apt-get install ripgrep

用法

  • 最直接的用法
$ rg 'device' ./

会显示当前目录下的搜索内容,会打印出文件名及关键字出现的行数。
和 grep 命令类似,也有三个打印出上下行的选项:
-A NUM 打印匹配行后面 after N
-B NUM 打印匹配行前面 before N
-C NUM 打印匹配行前后 N

  • 用正则表达式搜索
    使用 -e REGEX 来指定正则表达式:
rg -e "*device" -C2
  • 搜索所有内容包括 gitignore 和隐藏文件
    默认 rg 会忽略 .gitignore 和隐藏文件,可以使用 -uu 来查询所有内容:
rg -uu "device" ./
  • 显示匹配的次数
    使用 -c 来显示匹配的次数:
rg -c "device" ./

结果会在文件名后面增加一个次数。

  • 搜索指定的文件类型
    可以使用 -t type 来指定文件类型, 可以用 -T 来不搜索某种类型文件, 比如:只搜索 cpp 文件 (-t) :
rg -w 'device' ./ -tcpp

所支持的文件类型可以通过下面命令来查看

rg --type-list

看到这里,有些读者可能要问假如我要在两个文件类型中查找呢,这个时候 -t 参数就无法满足了,需要引入新的 -g 参数,man rg 看一下 -g 就知道该选项后面跟着一个 GLOB,正则表达式,包括或者去除一些文件或者目录。比如要在 c 文件或者 cpp 文件中查找 “device” 关键字:

rg -g "*.{c,cpp}" "device"
  • 显示不包含关键字的行
    使用选项 -v 来显示不包含关键字的行, 比如显示不含 “device” 的行:
rg -v "device" -tcpp ./
  • 忽略大小写(-i)

  • 只打印包含匹配内容的文件名
    使用 -l 来打印文件名:

rg -l -w "device" 

相反的是如果要打印没有匹配内容的文件名:

rg --files-without-match -w "device" ./

1.2.5 Leaderf 配合 rg

再 vim 中运行 LeaderF 之后的操作
可以通过 help leaderf-prompt 查看所有的操作, 比较常用的列在下面:

<C-C>, <ESC> : 退出
<C-R> : 在模糊查询和正则表达式模式间切换
<C-F> : 在全路径搜索和名字搜索模式间切换
<Tab> : 切换成normal模式
<C-V>, <S-Insert> : 从剪切板里copy字符串进行查询
<C-U> : 清除已经打出的字符
<C-J>, <C-K> : 在结果列表中移动
<Up>, <Down> : 从历史记录里调出上一次/下一次的输入pattern
<2-LeftMouse> or <CR> : 打开在光标处的文件或者被选择的多个文件
<F5>  : 刷新缓存
<C-P> : 预览选中结果
<C-Up> : 在预览popup窗口里滚动向上
<C-Down> : 在预览popup窗口里滚动向下

一旦按 tab, 则会进入 normal 模式, 可以使用大部分 normal 模式命令。

1.3 其它类型文件 vimrc 配置

1.3.1 System Verilog vimrc 配置

请参考:https://blog.csdn.net/weixin_42916702/article/details/117818186

推荐阅读
https://www.jianshu.com/p/9000cd49822c
https://retzzz.github.io/dc9af5aa/

 类似资料: