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

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

ssh user@192.168.0.11 -p 8022

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

软件包管理简介

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

例如更新仓库和软件:

pkg update

apt update

pkg upgrade

apt upgrade

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

安装命令就是

apt install xxx

安装基础软件

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

apt install vim clang cmake git nodejs python3

Vim基础配置

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

配置项非常少,很基础

vim .vimrc

编辑.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插件

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

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

" plugin

call plug#begin()

Plug 'xxx'

call plug#end()

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

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

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

:PlugUpdate更新插件

:PlugUpgrade更新VimPlug本身

VsCode颜色主题

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

Plug 'tomasiser/vim-code-dark'

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

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

colorscheme codedark

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

Coc代码提示

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

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

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

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

其插件列表可以在CocWiki看到

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

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

CocInstall coc-json coc-tsserver

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

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

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

coc-clangd

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

:ConInstall coc-clangd

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

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

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

cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE

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

coc-cmake

依赖cmake lsp

pip install cmake-language-server

:CocInstall coc-cmake

然后就可以使用了

括号补全

使用auto-pairs插件

Plug 'jiangmiao/auto-pairs'

无需任何配置

代码格式化

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

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

Plug 'rhysd/vim-clang-format'

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

let g:clang_format#code_style='WebKit'

格式化命令为:ClangFormat

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

nnoremap :ClangFormat

缩进参考线

indentLine插件

Plug 'Yggdroot/indentLine'

无需配置

最终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

好文阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: 


大家都在找:

ide:ide是什么意思啊

vim:vim编辑器

c++:c++ 字符串转int

coc:coc阵型复制工具

termux:termux命令大全

linux:linux主要应用的设备

编辑器:编辑器微信编辑器

大家都在看: