一、安裝vim
命令行中輸入:sudo apt-get install vim
(ps:它會顯示讓你輸入密碼,不過你輸入的密碼不會回顯)
查看安裝是否成功輸入: vim -v
二、配置vim
第一步:在終端創建.vimrc文件命令為: $vi ~/.vimrc
(ps:表示手動設置一個配置文件 :vimrc , 這里把.vimrc文件創建在當前用戶的根目錄下)
第二步:在.vimrc文件中添加配置內容:
set shortmess=atI " 啟動的時候不顯示那個援助烏干達兒童的提示 winpos 5 5 " 設定窗口位置 set lines=40 columns=155 " 設定窗口大小 set nu " 顯示行號 set go= " 不要圖形按鈕 syntax on " 語法高亮 autocmd InsertLeave * se nocul " 用淺色高亮當前行 autocmd InsertEnter * se cul " 用淺色高亮當前行 set showcmd " 輸入的命令顯示出來,看的清楚些 set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} "狀態行顯示的內容 set laststatus=1 " 啟動顯示狀態行(1),總是顯示狀態行(2) set foldmethod=manual " 手動折疊 set background=dark "背景使用黑色 set nocompatible "去掉討厭的有關vi一致性模式,避免以前版本的一些bug和局限 " 顯示中文幫助 if version >= 603 set helplang=cn set encoding=utf-8 endif set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936 set termencoding=utf-8 set encoding=utf-8 set fileencodings=ucs-bom,utf-8,cp936 set fileencoding=utf-8 autocmd BufReadPost * \ if line("'\"") > 0 && line("'\"") <= line("$") | \ exe "normal g'\"" | \ endif let mapleader="," nnoremap <space> : vnoremap <space> : nnoremap <C-h> <C-W>h nnoremap <C-j> <C-W>j nnoremap <C-k> <C-W>k nnoremap <C-l> <C-W>l inoremap <C-h> <Esc><C-W>h inoremap <C-j> <Esc><C-W>j inoremap <C-k> <Esc><C-W>k inoremap <C-l> <Esc><C-W>l let OpenDir=system("pwd") nmap <silent> <leader>cd :exe 'cd ' . OpenDir<cr>:pwd<cr> let g:Tlist_Auto_Update=1 let g:Tlist_Process_File_Always=1 let g:Tlist_Exit_OnlyWindow=1 let g:Tlist_Show_One_File=1 let g:Tlist_WinWidth=25 let g:Tlist_Enable_Fold_Column=0 let g:Tlist_Auto_Highlight_Tag=1 let g:NERDTreeWinPos="right" let g:NERDTreeWinSize=25 let g:NERDTreeShowLineNumbers=1 let g:NERDTreeQuitOnOpen=1 if has("cscope") set csto=1 set cst set nocsverb if filereadable("cscope.out") cs add cscope.out endif set csverb endif let g:OmniCpp_DefaultNamespaces=["std"] let g:OmniCpp_MayCompleteScope=1 let g:OmniCpp_SelectFirstItem=2 if has("gdb") set asm=0 let g:vimgdb_debug_file="" run macros/gdb_mappings.vim endif let g:LookupFile_TagExpr='"./tags.filename"' let g:LookupFile_MinPatLength=2 let g:LookupFile_PreserveLastPattern=0 let g:LookupFile_PreservePatternHistory=1 let g:LookupFile_AlwaysAcceptFirst=1 let g:LookupFile_AllowNewFiles=0 source $VIMRUNTIME/ftplugin/man.vim let g:snips_author="Du Jianfeng" let g:snips_email="cmdxiaoha@163.com" let g:snips_copyright="SicMicro, Inc" function! RunShell(Msg, Shell) echo a:Msg . '...' call system(a:Shell) echon 'done' endfunction nmap <F2> :TlistToggle<cr> nmap <F3> :NERDTreeToggle<cr> nmap <F4> :MRU<cr> nmap <F5> <Plug>LookupFile<cr> nmap <F6> :vimgrep /<C-R>=expand("<cword>")<cr>/ **/*.c **/*.h<cr><C-o>:cw<cr> nmap <F9> :call RunShell("Generate tags", "ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .")<cr> nmap <F10> :call HLUDSync()<cr> nmap <F11> :call RunShell("Generate filename tags", "~/.vim/shell/genfiletags.sh")<cr> nmap <F12> :call RunShell("Generate cscope", "cscope -Rb")<cr>:cs add cscope.out<cr> nmap <leader>sa :cs add cscope.out<cr> nmap <leader>ss :cs find s <C-R>=expand("<cword>")<cr><cr> nmap <leader>sg :cs find g <C-R>=expand("<cword>")<cr><cr> nmap <leader>sc :cs find c <C-R>=expand("<cword>")<cr><cr> nmap <leader>st :cs find t <C-R>=expand("<cword>")<cr><cr> nmap <leader>se :cs find e <C-R>=expand("<cword>")<cr><cr> nmap <leader>sf :cs find f <C-R>=expand("<cfile>")<cr><cr> nmap <leader>si :cs find i <C-R>=expand("<cfile>")<cr><cr> nmap <leader>sd :cs find d <C-R>=expand("<cword>")<cr><cr> nmap <leader>zz <C-w>o nmap <leader>gs :GetScripts<cr> autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()" func SetTitle() if &filetype == 'sh' call setline(1,"\#########################################################################") call append(line("."), "\# File Name: ".expand("%")) call append(line(".")+1, "\# Author: jianqiao") call append(line(".")+2, "\# mail: 123456789@qq.com") call append(line(".")+3, "\# Created Time:".strftime("%c")) call append(line(".")+4,"\#########################################################################") call append(line(".")+5, "\#!/bin/bash") call append(line(".")+6, "") else call setline(1, "/*************************************************************************") call append(line("."), " > File Name: ".expand("%")) call append(line(".")+1, " > Author: jianqiao") call append(line(".")+2, " > Mail: 123456789@qq.com ") call append(line(".")+3, " > Created Time: ".strftime("%c")) call append(line(".")+4," ************************************************************************/") call append(line(".")+5, "") endif if &filetype == 'cpp' call append(line(".")+6, "#include <iostream>") call append(line(".")+7, "") call append(line(".")+8, "using namespace std;") call append(line(".")+9, "") call append(line(".")+10, "int main(void){") call append(line(".")+11, "") call append(line(".")+12, " return 0;") call append(line(".")+13, "}") endif if &filetype == 'c' call append(line(".")+6, "#include <stdio.h>") call append(line(".")+7, "") call append(line(".")+8, "int main(void){") call append(line(".")+9, "") call append(line(".")+10," return 0;") call append(line(".")+11, "}") endif autocmd BufNewFile *normal G endfunc inoremap ( ()<Esc>i>
set ts=4
set expandtab
set autoindent
set nu
set nocompatible
set number
set autoindent
set smartindent
set showmatch
set ruler
set incsearch
set tabstop=4
set shiftwidth=4
set softtabstop=4
set cindent
set nobackup
set clipboard+=unnamed
三、重啟終端 創建、編寫、編譯、運行C、C++程序
(ps:以創建hello.cpp和hello.c為例)
1、在終端使用命令 $vi hello.c ($vi hello.cpp) 創建hello.c / hello.cpp文件,並進入vim界面;
2、鍵入i,進入輸入模式
3、編寫hello.c / hello.cpp程序
4、先按Esc鍵退出輸入模式;然后按 : 鍵,進入命令模式;輸入wq表示保存並退出。
5、編譯hello.c / hello.cpp
5.1、對於C程序:在終端使用命令 $gcc hello.c
5.2、對於C++程序:用$g++ hello.cpp可編譯,編譯后均生成a.out的可執行文件
6、在終端使用命令 $ ./a.out 表示運行程序
7、在終端顯示“helloworld!”即成功
小坑點:
修改ubuntu18.04界面大小:
1、使用CTRL+alt+t打開終端
2、使用命令xrandr 查看相應的屏幕分辨率的相關參數
3、使用命令 xrandr -s 相應分辨率(如1916x889)即可改為相應的顯示
參考:
https://blog.csdn.net/qq_15725099/article/details/88536748
https://blog.csdn.net/weixin_34144848/article/details/92129254