• 打造基于终端命令行的IDE,Termux配置Vim C++开发环境


    Termux配置Vim C++开发环境,打造基于终端命令行的IDE

    主要利用Vim+Coc插件,配置C++的代码提示等功能。

    Termux换源

    打开termux,输入termux-change-repo

    找到mirrors.tuna.tsinghua.edu.cn清华源,空格选中,回车确认

    在这里插入图片描述

    Termux配置ssh

    有了ssh后,就可以方便的在PC或者其它平台上,使用ssh工具远程termux终端

    # 安装
    apt install open-ssh
    # 启动sshd,默认端口为8022
    sshd
    # 关闭sshd
    pkill sshd
    # 查看sshd是否运行
    ps aux | grep sshd
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    默认没有密码,使用passwd命令配置密码

    ssh user@192.168.0.11 -p 8022
    
    • 1

    user用户名可以用whoami命令查看,一般termux用户名为u0_xxxx

    软件包管理简介

    termux使用pkg管理软件包,并且可以使用apt别名

    例如更新仓库和软件:

    pkg update
    apt update
    pkg upgrade
    apt upgrade
    
    • 1
    • 2
    • 3
    • 4

    两个命令都可以,apt命令对使用过Debian的人非常友好。以下全部使用apt

    安装命令就是

    apt install xxx
    
    • 1

    安装基础软件

    • vim:编辑器
    • clang:C++编译器,并且提供了g++别名
    • cmake:管理C++项目配置
    • git:源码仓库工具
    • nodejs:C++开发很少用到nodejs,主要是为vim插件提供运行环境
    • python3:提供环境
    apt install vim clang cmake git nodejs python3
    
    • 1

    Vim基础配置

    主要配置缩进、tab空格、文件编码、行号等,可以根据自己的需求配置

    配置项非常少,很基础

    vim .vimrc
    
    • 1

    编辑.vimrc文件,将以下内容输入

    " vim base config
    set nocompatible
    syntax on
    set showmode
    set showcmd
    set encoding=utf-8
    set t_Co=256
    filetype indent on
    set softtabstop=4
    set tabstop=4
    set shiftwidth=4
    set expandtab
    set autoindent
    set number
    set cursorline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    安装Vim插件

    • VimPlug:用来管理Vim插件,之后的插件都需要用它来安装
    • vim-code-dark:VsCode主题

    VimPlug插件管理

    VimPlug主页提供了安装方法

    复制下面的命令到终端并执行

    curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
        https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    
    • 1
    • 2

    安装完成后编辑.vimrc文件,添加如下代码段

    " plugin
    call plug#begin()
    
    Plug 'xxx'
    
    call plug#end()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    中间的Plug 'xxx',就是代表安装xxx插件,每个插件一行

    每当想安装新的插件时,先编辑vimrc,再重新进入vim命令模式,输入:PlugInstall就会安装插件

    卸载插件时,编辑vimrc,删除插件那一行,然后进入vim命令模式,输入:PlugClean,不在列表里的插件就会被清理

    :PlugUpdate更新插件

    :PlugUpgrade更新VimPlug本身

    VsCode颜色主题

    Vim自己的高亮不好看,我选择了VsCode主题

    Plug 'tomasiser/vim-code-dark'
    
    • 1

    添加上述代码,重新打开vim并运行PlugInstall,出现Finishing … Done!且插件名称后面显示100%时,说明安装成功

    在这里插入图片描述

    再次编辑vimrc,添加如下代码

    colorscheme codedark
    
    • 1

    再次打开vim时,已经变为VsCode主题

    在这里插入图片描述

    Coc代码提示

    参考Coc主页,安装方式如下:

    Plug 'neoclide/coc.nvim', {'branch': 'release'}
    
    • 1

    同样,运行:PlugInstall就可以安装,Coc依赖于NodeJs

    Coc是类似VimPlug的管理工具,具体的语言支持还需要安装语言包

    其插件列表可以在CocWiki看到

    注意,这里的插件指的是Coc插件,他们往往都按照coc-xxx命名,例如coc-clangd、coc-json等

    安装插件需要使用:CocInstall命令,例如:

    CocInstall coc-json coc-tsserver
    
    • 1

    Coc也需要配置,配置很多,我也没看明白,官网给了一个示例,主要是配置快捷键补全等功能。

    对于C++开发环境,需要的Coc插件有

    • coc-clangd:提供C++语言服务支持
    • coc-cmake:提供cmake支持

    coc-clangd

    安装coc-clangd,依赖于clangd,在termux中使用apt install clang来安装

    :ConInstall coc-clangd
    
    • 1

    安装完成后,可以编辑一个cpp文件尝试效果,Tab用来选择候选项,Enter用来确认

    对于多文件项目或者CMake项目,插件需要读取compile_commands.json文件,这个文件需要在编译时生成。

    CMake在构建项目时生成该文件,指令为:

    cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE
    
    • 1
    • -S指定源代码文件夹
    • -B指定输出目录
    • -DCMAKE_BUILD_TYPE设置构建类型
    • -DCMAKE_EXPORT_COMPILE_COMMANDS指定生成compile_commands.json文件

    在这里插入图片描述

    coc-cmake

    依赖cmake lsp

    pip install cmake-language-server
    
    • 1
    :CocInstall coc-cmake
    
    • 1

    然后就可以使用了

    括号补全

    使用auto-pairs插件

    Plug 'jiangmiao/auto-pairs'
    
    • 1

    无需任何配置

    代码格式化

    使用vim-clang-format插件,参考其主页安装

    依赖于clang-format,在Termux下,安装clang就行

    Plug 'rhysd/vim-clang-format'
    
    • 1

    安装完成后,可以参考如下代码或者ClangFormat主页配置格式化风格:

    let g:clang_format#code_style='WebKit'
    
    • 1

    格式化命令为:ClangFormat

    为了方便,把Ctrl+Shift+i映射为该命令,在常规模式下有效:

    nnoremap  :ClangFormat
    
    • 1

    缩进参考线

    indentLine插件

    Plug 'Yggdroot/indentLine'
    
    • 1

    无需配置

    在这里插入图片描述

    最终vimrc源码

    " vim base config
    set nocompatible
    syntax on
    set showmode
    set showcmd
    set encoding=utf-8
    set t_Co=256
    filetype indent on
    set softtabstop=4
    set tabstop=4
    set shiftwidth=4
    set expandtab
    set autoindent
    set number
    set cursorline
    
    " vim plug
    call plug#begin()
    
    Plug 'tomasiser/vim-code-dark'
    Plug 'neoclide/coc.nvim', {'branch': 'release'}
    Plug 'jiangmiao/auto-pairs'
    Plug 'rhysd/vim-clang-format'
    Plug 'Yggdroot/indentLine'
    
    call plug#end()
    
    " vscode theme
    colorscheme codedark
    " Clang Format
    let g:clang_format#code_style='WebKit'
    nnoremap  :ClangFormat
    
    
    " =================================== Coc Config ==================
    " Use tab for trigger completion with characters ahead and navigate
    " NOTE: There's always complete item selected by default, you may want to enable
    " no select by `"suggest.noselect": true` in your configuration file
    " NOTE: Use command ':verbose imap ' to make sure tab is not mapped by
    " other plugin before putting this into your config
    inoremap  
          \ coc#pum#visible() ? coc#pum#next(1) :
          \ CheckBackspace() ? "\" :
          \ coc#refresh()
    inoremap  coc#pum#visible() ? coc#pum#prev(1) : "\"
    
    " Make  to accept selected completion item or notify coc.nvim to format
    " u breaks current undo, please make your own choice
    inoremap   coc#pum#visible() ? coc#pum#confirm()
                                  \: "\u\\=coc#on_enter()\"
    
    function! CheckBackspace() abort
      let col = col('.') - 1
      return !col || getline('.')[col - 1]  =~# '\s'
    endfunction
    
    " Use  to trigger completion
    if has('nvim')
      inoremap   coc#refresh()
    else
      inoremap   coc#refresh()
    endif
    
    " Use `[g` and `]g` to navigate diagnostics
    " Use `:CocDiagnostics` to get all diagnostics of current buffer in location list
    nmap  [g (coc-diagnostic-prev)
    nmap  ]g (coc-diagnostic-next)
    
    " GoTo code navigation
    nmap  gd (coc-definition)
    nmap  gy (coc-type-definition)
    nmap  gi (coc-implementation)
    nmap  gr (coc-references)
    
    " Use K to show documentation in preview window
    nnoremap  K :call ShowDocumentation()
    
    function! ShowDocumentation()
      if CocAction('hasProvider', 'hover')
        call CocActionAsync('doHover')
      else
        call feedkeys('K', 'in')
      endif
    endfunction
    
    " Highlight the symbol and its references when holding the cursor
    autocmd CursorHold * silent call CocActionAsync('highlight')
    
    " Symbol renaming
    nmap rn (coc-rename)
    
    " Formatting selected code
    xmap f  (coc-format-selected)
    nmap f  (coc-format-selected)
    
    augroup mygroup
      autocmd!
      " Setup formatexpr specified filetype(s)
      autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
      " Update signature help on jump placeholder
      autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
    augroup end
    
    " Applying code actions to the selected code block
    " Example: `aap` for current paragraph
    xmap a  (coc-codeaction-selected)
    nmap a  (coc-codeaction-selected)
    
    " Remap keys for applying code actions at the cursor position
    nmap ac  (coc-codeaction-cursor)
    " Remap keys for apply code actions affect whole buffer
    nmap as  (coc-codeaction-source)
    " Apply the most preferred quickfix action to fix diagnostic on the current line
    nmap qf  (coc-fix-current)
    
    " Remap keys for applying refactor code actions
    nmap  re (coc-codeaction-refactor)
    xmap  r  (coc-codeaction-refactor-selected)
    nmap  r  (coc-codeaction-refactor-selected)
    
    " Run the Code Lens action on the current line
    nmap cl  (coc-codelens-action)
    
    " Map function and class text objects
    " NOTE: Requires 'textDocument.documentSymbol' support from the language server
    xmap if (coc-funcobj-i)
    omap if (coc-funcobj-i)
    xmap af (coc-funcobj-a)
    omap af (coc-funcobj-a)
    xmap ic (coc-classobj-i)
    omap ic (coc-classobj-i)
    xmap ac (coc-classobj-a)
    omap ac (coc-classobj-a)
    
    " Remap  and  to scroll float windows/popups
    if has('nvim-0.4.0') || has('patch-8.2.0750')
      nnoremap   coc#float#has_scroll() ? coc#float#scroll(1) : "\"
      nnoremap   coc#float#has_scroll() ? coc#float#scroll(0) : "\"
      inoremap   coc#float#has_scroll() ? "\=coc#float#scroll(1)\" : "\"
      inoremap   coc#float#has_scroll() ? "\=coc#float#scroll(0)\" : "\"
      vnoremap   coc#float#has_scroll() ? coc#float#scroll(1) : "\"
      vnoremap   coc#float#has_scroll() ? coc#float#scroll(0) : "\"
    endif
    
    " Use CTRL-S for selections ranges
    " Requires 'textDocument/selectionRange' support of language server
    nmap   (coc-range-select)
    xmap   (coc-range-select)
    
    " Add `:Format` command to format current buffer
    command! -nargs=0 Format :call CocActionAsync('format')
    
    " Add `:Fold` command to fold current buffer
    command! -nargs=? Fold :call     CocAction('fold', )
    
    " Add `:OR` command for organize imports of the current buffer
    command! -nargs=0 OR   :call     CocActionAsync('runCommand', 'editor.action.organizeImport')
    
    " Add (Neo)Vim's native statusline support
    " NOTE: Please see `:h coc-status` for integrations with external plugins that
    " provide custom statusline: lightline.vim, vim-airline
    set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
    
    " Mappings for CoCList
    " Show all diagnostics
    nnoremap  a  :CocList diagnostics
    " Manage extensions
    nnoremap  e  :CocList extensions
    " Show commands
    nnoremap  c  :CocList commands
    " Find symbol of current document
    nnoremap  o  :CocList outline
    " Search workspace symbols
    nnoremap  s  :CocList -I symbols
    " Do default action for next item
    nnoremap  j  :CocNext
    " Do default action for previous item
    nnoremap  k  :CocPrev
    " Resume latest coc list
    nnoremap  p  :CocListResume
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
  • 相关阅读:
    正雅齿科S8隐形矫治技术,减少或避免拔牙
    【论文阅读】Twin Neural Network Regression
    ubuntu18.04中导出requirements.txt、
    Python爬虫——Urllib库-2
    Golang 基础三
    [CSAWQual 2016]i_got_id(Perl ARGV)
    读《GaitPart: Temporal Part-based Model for Gait Recognition》
    Python语言:元组的使用
    通过FXmlFile构建xml时,注意xml规范
    Docker面试题库
  • 原文地址:https://blog.csdn.net/izwmain/article/details/132717628