【vim && neovim】从入门到放弃(“四种”模式、常用命令、正则表达式、文件属性、插件安装--代码补全、一键格式化、显示目录)(三)

简介: 本文所有操作均通过ssh连接腾讯云服务器完成。如果你正在使用安装GNOME桌面的Linux,很多操作可以通过鼠标完成,或许更加直观。推荐使用neovim(结合鼠标操作更加丝滑)。

4.44 C语言代码格式化:clang-format

上面的格式化命令是用的coc的,他的缩进是2个空格,研究了一下,没能成功修改成4个空格(我写C语言代码格式化的时候需要4个空格缩进,有些语言是2个空格)。


我去github上请教了开发人员,他们告诉我使用clang-format。

c32b2de6fdda092e048888624ef749e4_a3ec7c66bbd942e4b7484db4d440da59.png

C语言代码补全,已经安装了coc-clangd和clangd(前面刚刚讲过),现在现需要安装clang-format来实现C语言的代码格式化。(clangd是C语言等的语言服务器,用来代码补全等,而clang是编译器,clang-format是代码格式化工具)


ClangFormat 支持的语言有:C/C++/Java/JavaScript/Objective-C/Protobuf/C#

ClangFormat 支持的规范有:LLVM,Google,Chromium,Mozilla 和 WebKit

安装clang-format:

apt install clang-format

设置clang-format的语言规范,生成配置文件:

clang-format -style=llvm --dump-config >~/.clang-format

然后去上面这个配置文件中进行自定义修改,比如缩进4个空格(配置文件模式缩进2个空格),就改下面这句:

af40c008964d6cdf03bdc50ada1f154a_ca56f14d01154651aebbfe0af4536148.png

在命令行使用clang-format

clang-format -style=file  HashMap.c  >new.c

这里重定向是吧格式化后的代码保存到一个新文件中,如果不重定向,会直接输出在命令行,不能重定向到源文件,直接全部删除了,如果是追加的话,又会重复一次😅

现在可以在nvim中使用4.43节设置的快捷键,无需其他配置,coc-format-selected在安装clang-format后,在格式化C语言的时候,会使用clang-format的配置文件。

4.45 显示对齐线

在Plug那里添加:

Plug 'Yggdroot/indentLine'

然后安装这个插件就好了。

效果:

5cf96f677ecb6c4cb080ef84f6185c20_9974f54f93814ac4bfab263dc7289a43.png

4.5 我的nvim配置文件

自己看着修改吧

" ----------------------最常用、最基本设置-----------------------------
colorscheme  evening    "配色方案
syntax enable            " 开启语法高亮功能
syntax on                " 自动语法高亮
set t_Co=256             " 开启256色支持
set number               " 开启行号显示
set ruler                " 总是显示光标位置
set cursorline           " 高亮显示当前行
filetype on              " 设置开启文件类型侦测
set mouse=a              "开启鼠标支持
set mousemodel=extend    "开启侧边滚动条
" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let mapleader = ","        " 定义<leader>键
" set nocompatible         " 设置不兼容原始vi模式
" filetype plugin on       " 设置加载对应文件类型的插件
" set noeb                 " 关闭错误的提示
set cmdheight=1          " 设置命令行的高度
set showcmd              " select模式下显示选中的行数
set laststatus=2         " 总是显示状态栏
set whichwrap+=<,>,h,l   " 设置光标键跨行
" set ttimeoutlen=0        " 设置<ESC>键响应时间
set virtualedit=block,onemore   " 允许光标出现在最后一个字符的后面
" set history=100                        " 设置历史记录条数
" ----------------------代码缩进完整配置-----------------------------
set autoindent           " 设置自动缩进
set cindent              " 设置使用C/C++语言的自动缩进方式
set cinoptions=g0,:0,N-s,(0    " 设置C/C++语言的具体缩进方式
set smartindent          " 智能的选择对其方式
filetype indent on       " 自适应不同语言的智能缩进
filetype plugin on       " 载入文件类型插件
set expandtab            " 将制表符扩展为空格(关于制表符和空格,有的语言可能不同,谨慎设置)
set tabstop=4            " 设置编辑时制表符占用空格数
set shiftwidth=4         " 设置格式化时制表符占用空格数
set softtabstop=4        " 设置4个空格为制表符
set smarttab             " 在行和段开始处使用制表符
set nowrap               " 禁止折行
set backspace=2          " 使用回车键正常处理indent,eol,start等
set sidescroll=10        " 设置向右滚动字符数
set nofoldenable         " 禁用折叠代码
" ----------------------代码补全-----------------------------
set wildmenu             " vim自身命名行模式智能补全
set completeopt-=preview " 补全时不显示窗口,只显示补全列表
" ----------------------搜索-----------------------------
set hlsearch            " 高亮显示搜索结果
" set incsearch           " 开启实时搜索功能
 set noignorecase        " 搜索时大小写不敏感
" ----------------------文件缓存-----------------------------
" set nobackup            " 设置不备份
" set noswapfile          " 禁止生成临时文件
set autoread            " 文件在vim之外修改过,自动重新读入
set autowrite           " 设置自动保存
set confirm             " 在处理未保存或只读文件的时候,弹出确认
" ----------------------编码-----------------------------
 set termencoding=utf-8
 set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030
 "---------------------插件安装---------------------------
 call plug#begin('~/.config/nvim/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'} 
Plug 'nvim-tree/nvim-tree.lua'                    
 call plug#end()
"---------------coc配置------------------
set nobackup
set nowritebackup
set updatetime=300
set signcolumn=yes
inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
function! CheckBackspace() abort
      let col = col('.') - 1
      return !col || getline('.')[col - 1]  =~# '\s'
endfunction
if has('nvim')
    inoremap <silent><expr> <c-space> coc#refresh()
else
    inoremap <silent><expr> <c-@> coc#refresh()
endif
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
nnoremap <silent> K :call ShowDocumentation()<CR>
function! ShowDocumentation()
      if CocAction('hasProvider', 'hover')
          call CocActionAsync('doHover')
      else
          call feedkeys('K', 'in')
      endif
endfunction
autocmd CursorHold * silent call CocActionAsync('highlight')
nmap <leader>rn <Plug>(coc-rename)
xmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)
augroup mygroup
      autocmd!
      autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
      autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end
xmap <leader>a  <Plug>(coc-codeaction-selected)
nmap <leader>a  <Plug>(coc-codeaction-selected)
nmap <leader>ac  <Plug>(coc-codeaction-cursor)
nmap <leader>as  <Plug>(coc-codeaction-source)
nmap <leader>qf  <Plug>(coc-fix-current)
nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
xmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)
nmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)
nmap <leader>cl  <Plug>(coc-codelens-action)
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)
if has('nvim-0.4.0') || has('patch-8.2.0750')
    nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
    nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
    inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
    inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
    vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
    vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)
command! -nargs=0 Format :call CocActionAsync('format')
command! -nargs=? Fold :call     CocAction('fold', <f-args>)
command! -nargs=0 OR   :call     CocActionAsync('runCommand', 'editor.action.organizeImport')
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
nnoremap <silent><nowait> <space>a  :<C-u>CocList diagnostics<cr>
nnoremap <silent><nowait> <space>e  :<C-u>CocList extensions<cr>
nnoremap <silent><nowait> <space>c  :<C-u>CocList commands<cr>
nnoremap <silent><nowait> <space>o  :<C-u>CocList outline<cr>
nnoremap <silent><nowait> <space>s  :<C-u>CocList -I symbols<cr>
nnoremap <silent><nowait> <space>j  :<C-u>CocNext<CR>
nnoremap <silent><nowait> <space>k  :<C-u>CocPrev<CR>
nnoremap <silent><nowait> <space>p  :<C-u>CocListResume<CR>
"  代码格式化:逗号+l
nnoremap<leader>l ggVG<C-u><Plug>(coc-format-selected)<CR>
" 全选: 逗号+m
nnoremap<leader>m ggVG
"------------------nvim-------------------------------------
" 打开文件夹快捷键:空格+b
lua require("plugin/nvim-tree")
nnoremap <silent> <space>b :NvimTreeToggle<CR>


相关文章
|
2月前
|
Linux 编译器 开发工具
Linux:详解(yum的使用、vim编辑器命令集合以及gcc/g++编译器的使用)
Linux:详解(yum的使用、vim编辑器命令集合以及gcc/g++编译器的使用)
121 1
|
2月前
|
开发工具 Docker 索引
Docker解决没有vi、vim等命令
Docker解决没有vi、vim等命令
63 0
|
2月前
|
人工智能 搜索推荐 Unix
vim的使用介绍以及命令大全(系统性学习day3)
vim的使用介绍以及命令大全(系统性学习day3)
|
3月前
|
Linux 开发工具
【Linux】vim正常模式命令集
【Linux】vim正常模式命令集
【Linux】vim正常模式命令集
|
3月前
|
Linux 开发工具 C++
vim底行模式各命令汇总
vim底行模式各命令汇总
|
3月前
|
开发工具
vim命令模式各命令汇总
vim命令模式各命令汇总
|
3月前
|
开发工具
vim命令编辑完文件后,按ESC键退出编辑模式,无法进入命令模式解决方案
vim命令编辑完文件后,按ESC键退出编辑模式,无法进入命令模式解决方案
154 0
|
2月前
|
编译器 Python
Python正则表达式的7个使用典范(推荐)
Python正则表达式的7个使用典范(推荐)
25 0
|
2月前
|
Python
Python实现正则表达式匹配。
【2月更文挑战第11天】【2月更文挑战第30篇】Python实现正则表达式匹配。
|
2月前
|
Python
请解释Python中的正则表达式以及如何使用它们进行文本处理。
请解释Python中的正则表达式以及如何使用它们进行文本处理。
10 0