linux使用vim搭建c/c++

1、配置linux下vim的c/c++环境:

安装必要的软件:

sudo apt-get install vim-scripts vim-doc

进入用户文件夹并创建.vimrc

vim ~/.vimrc

把以下代码填入.vimrc中:

" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
"debian.vim

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
set nocompatible

" Vim5 and later versions support syntax highlighting. Uncommenting the
" following enables syntax highlighting by default.
if has("syntax")
syntax on " 语法高亮
endif
colorscheme ron " elflord ron peachpuff default 设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim72/colors目录下

" detect file type
filetype on
filetype plugin on

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"have Vim load indentation rules and plugins according to the detected filetype
filetype plugin indent on
endif

" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.

"set ignorecase " 搜索模式里忽略大小写
"set smartcase " 如果搜索模式包含大写字符,不使用 'ignorecase' 选项。只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用。
set autowrite " 自动把内容写回文件: 如果文件被修改过,在每个 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!:make、CTRL-] 和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令转到别的文件时亦然。
set autoindent " 设置自动对齐(缩进):即每行的缩进值与上一行相等;使用 noautoindent 取消设置
"set smartindent " 智能对齐方式
set tabstop=4 " 设置制表符(tab键)的宽度
set softtabstop=4 " 设置软制表符的宽度
set shiftwidth=4 " (自动) 缩进使用的4个空格
set cindent " 使用 C/C++ 语言的自动缩进方式
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s "设置C/C++语言的具体缩进方式
"set backspace=2 " 设置退格键可用
set showmatch " 设置匹配模式,显示匹配的括号
set linebreak " 整词换行
set whichwrap=b,s,<,>,[,] " 光标从行首和行末时可以跳到另一行去
"set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes) "使用鼠标
set number " Enable line number "显示行号 
"set previewwindow " 标识预览窗口 
set history=50 " set command history to 50 "历史记录50条 

"--状态行设置-- 
set laststatus=2 " 总显示最后一个窗口的状态行;设为1则窗口数多于一个的时候显示最后一个窗口的状态行;0不显示最后一个窗口的状态行 
set ruler " 标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上。 
"--命令行设置-- 
set showcmd " 命令行显示输入的命令 
set showmode " 命令行显示vim当前模式 

"--find setting-- 
set incsearch " 输入字符串就显示匹配点 
set hlsearch

然后重新用vim打开文档就能看到效果了:
在这里插入图片描述
vim中想要右键粘贴代码时,发现右键点击无效,此时只要在刚刚的.vimrc最后加入如下代码:

if has('mouse') 
	set mouse-=a 
endif 

然后保存退出,就会发现鼠标右键又可以用了。
在这里插入图片描述
安装插件除了直接下载插件+配置.vimrc,还可以使用Vundle插件来管理其他插件,首先安装vundle,然后将插件下载到~/.vim/bundle目录下,或者使用Plugin '插件名‘来安装,然后在.vimrc中进行具体配置。

常用的的插件还有自动补齐等,可以参考:https://blog.csdn.net/bc516125634/article/details/88858097

2、使用vimc/c++程序:

创建c/c++文件:

touch main.c   //c
touch main.cpp   //cpp

vim添加程序代码:

vim main.c    //c
vim main.cpp   //cpp

输出文件:

//执行完以后并不是直接输出结果,而是生成一个可执行文件
//test1、test2为输出结果的文件名
gcc main.c -o test1  //c
g++ main.cpp -o test2   //cpp

在这里插入图片描述
运行结果文件:

./test
./test1

在这里插入图片描述

3、gccg++的一些使用:

以下gcc的同样适用g++

//使用-x后面的语言来运行文件,哪怕文件的后缀是乱写的
1、gcc -x language filename
使用:gcc -x  c  l.cqq 
//关掉上面的指定语言运行,从而让程序根据后缀识别
2、gcc -x none filename
使用:gcc -x  c  l.cqq -x none l.cqq  当然这会报错,不存在cqq格式
//只激活预处理,编译,和汇编,即只生成obj文件
3、gcc -c filename
使用:gcc -c main.c
//只激活预处理和编译,编译成汇编代码,将生成.s的汇编代码
4、gcc -S filename
使用:gcc -S main.c
//只激活预处理,这个不生成文件,控制台会输出一大堆的预处理信息
5、gcc -E filename
使用:gcc -E main.c
//自定义文件目标文件名,比如上面的test,如果不加就会叫做a.out
6、gcc -o filename
使用:gcc -o main.c
//使用管道代替编译中临时文件, 在使用非 gnu汇编工具的时候
//会生成exe文件,但是打开运行报错了
7、gcc -pipe -o filename.exe filename.c
使用:gcc -pipe -o main.exe main.c

其余的可以参考:
1、https://www.runoob.com/w3cnote/gcc-parameter-detail.html
2、https://www.cnblogs.com/me115/archive/2011/03/02/1969100.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值