【vim 学习系列文章 1 - vim 常用配置】


请阅读【嵌入式开发学习必备专栏 之 Vim】



下篇文章:vim 学习系列文章 2 - vim 常用插件配置

简介

Vim是一个高度可配置的文本编辑器,是程序员和其他需要文本编辑的专业人士的流行工具。Vim的全名是Vi IMproved,意为“改进版的Vi”,Vi是Unix操作系统下的一个经典文本编辑器。

Vim提供了很多高级功能,包括语法高亮、代码折叠、多级撤销/重做、多窗口/多标签页、完全可自定义的键盘快捷键,以及强大的搜索和替换等。此外,Vim还有丰富的插件系统,用户可以安装各种插件来扩展或定制编辑器的功能。

Vim在许多Linux发行版中都是默认安装的编辑器。它以其强大的功能,以及对键盘操作的优化而受到许多用户的喜爱。虽然Vim有一定的学习曲线,但是一旦熟悉后,它可以极大地提高文本编辑的效率。

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

Vim和Cscope是Linux开发者工具包中的两个重要组成部分,它们可以很好地配合使用。Cscope,另一方面,是一个用于在C源代码中查找特定函数定义,变量等的工具。Cscope非常适合处理大型C项目,可以帮助开发者理解和跟踪源代码的结构和逻辑。

结合使用Vim和Cscope,你可以在编辑代码的同时,轻松查找和跳转到函数定义,全局变量,宏定义等。使用Cscope查询结果,你可以在Vim中直接打开文件并跳转到相关代码的位置。这极大地提高了在大型代码库中快速查找和理解代码的能力。

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.1.6 vim 宏的使用

使用 vim 寄存器来进行宏录入:

qaq
....
q

然后再.vimrc 中添加下面一句,既可以将 F2 映射为宏快捷键

nnoremap <F2> <esc>@a 

下篇文章:vim 学习系列文章 2 - vim 常用插件配置

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

主公CodingCos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值