使用方法 {{{
tags: use, usage, use vim, use method
2024-07-28 01:40
本文件使用标记折叠方法组织内容
例子 {{{
折叠的使用方法 {{{
"--------------------------------------------------------------------------------
tags: fold, use fold
:set foldmethod
查看折叠方法
:set foldmethod*marker
通过帮助系统查看有关折叠的快捷键:
:h z
在 normal 模式下使用:
zM 递归的折叠所有部分
zR 展开所有折叠的部分(包括递归)
zm 增加一层(:h foldlevel)折叠
zr 减去一层折叠
zc 打开光标下的折叠
za 打开/关闭光标下的折叠
zj 去到下一个折叠上
zk 去到上一个折叠上
}}} ### 折叠的使用方法
获取指定目录下的所有文件的路径 {{{
"--------------------------------------------------------------------------------
tags: path, dir
windows {{{
-
CMD
:r! dir /b /s src/
-
选中读取的内容,依据文件的后缀进行排序
:'<,'>sort /.*\.h/ r
开头为 "
的行排在一起
:'<,'>g/"Plug/move 165
}}} #### windows
}}} ### 获取指定目录下的所有文件的路径
}}} ## 例子
查找替换 : substitute, find, replace {{{
"--------------------------------------------------------------------------------
2024-07-28 01:16
替换多个文件中指定文本
被替换者: test_123
替换者: new_123
在同一个目录`<dir>`中的所有 `h文件` 中
:cd <dir>
:args *.h
:argdo %s/test_123/new_123/ge | update
上面一行命令的解释:
argdo 对所有在 args 中的文件执行后面的命令
%s/被替换者/替换者/ge
g 整个文件的所有符合条件者都被替换
e 没有找到的情况,不报错
| 用于执行多个命令
update 保存对文件执行的操作(只对已经修改的缓冲有效)
替换换行符
"--------------------------------------------------------------------------------
tags: newline, dos, crlf, \r\n, \n
:%s/\n/ /g
查找:包含 .md 的目标被排除
"--------------------------------------------------------------------------------
^\w\+.*\.\(md\)\@!
解释:找到所有文件类型不为 md 的行
examples:
abc.md 排除
abc.txt 包含
test.cpp 包含
}}} ### 查找替换
option 的设置
"--------------------------------------------------------------------------------
set imd
是一个布尔选项,上面一行表示开启
set noimd
上面一个表示取消
缓冲设置
创建一个临时缓存
"--------------------------------------------------------------------------------
setlocal buftype=nofile
setlocal noswapfile
file temp
缓存不可保存,可以退出,在退出程序时,内容将丢失。
pwsh 使用方法
--------------------------------------------------------------------------------""
找到指定目录下的所有 cpp 文件,然后从中找到包含字符串 include 的文件
r!get-childitem -path $linp\cpp\basic\*.cpp | select-string -pattern "include"
set shell=pwsh
set shellcmdflag=-Command
引用自:https://juliankay.com/development/setting-up-vim-to-work-with-powershell/
将行移动到指定的标记位置
:move 'a
}}} # 使用方法
查看窗口的大小(宽度,高度),修改窗口大小的方法。
:resize
调整当前窗口(称为 A,其它的为 B,C,...)的高度,
当前列有两个窗口:B 变为一行,A 占据其余高度;
当前列有三个窗口:B,C 变为一行,A 占据其余高度;
据此可以知道,更多窗口的效果;
使用 CTRL-W =
在所有窗口均匀分布;
:resize 10
调整当前窗口的高度为 10;
调整窗口的宽度
:vertical resize 20
窗口宽度调整为 20;
vimrc 基础配置
!
leader
键为空格
" 时间:2024年7月26日
" vim 的配置,用标记折叠的方式组织起来,感觉很好
"# 基础设置(没有任何依赖项) {{{
"tags: basic setting
"查看选项的具体效果,请使用 `:help <option-name>`
"## option 设置 {{{
"autoload-plugin
"packloadall " 加载所有插件
"silent! helptags ALL " 为所有插件加载帮助文档
"noh "清楚高亮显示
set hlsearch "高亮‘/’搜索到的匹配项
filetype plugin on
filetype indent on " 根据文件类型自动缩进
filetype on
set fileencodings=utf-8,gbk
"### gui options {{{
"set showtabline=1
"set guioptions-=T
"set guioptions-=m
set guioptions=
set guifont=AnonymicePro_Nerd_Font_Mono:h16
set laststatus=2
"https://juliankay.com/development/setting-up-vim-to-work-with-powershell/
set shell=pwsh
set shellcmdflag=-Command
"set guifont=Lucida_Console:h18
"set guifont=Courier_New:b:h16
let m_show_gui = v:false
"""Function: (隐藏/显示)菜单、工具栏的显示
function! SwitchGui()
if g:m_show_gui
let g:m_show_gui = v:false
set guioptions-=m
set guioptions-=T
else
let g:m_show_gui = v:true
set guioptions+=m
set guioptions+=T
endif
endfunction
"}}} ## gui options
set textwidth=80 " 限制一行的长度,不错的功能
"set backspace=2 " 修正终端上的退格键 Backspace 的行为
"set softtabstop=4
"set relativenumber "相对行号
"set paste " 开启这个选项,复制进来的文本的换行符就不会乱
"set cindent " C 语言的缩进
set noswapfile
set nobackup
set noundofile
set virtualedit=all
set scrolloff=5
set t_Co=256 " 开启 256 色
set foldlevel=3
set foldmethod=marker
"set cursorline
"set cursorcolumn
set syntax=on " 开启语法高亮
set backspace=3
set sidescroll=10 " 自动的滚动文本,当移动的文本没有显示时
set shiftwidth=4 " 用于自动缩进的空格数量
set expandtab " 将制表符 Tab 展开为空格,这对于 Python 尤其有用
set tabstop=4 " tab 的空格数量
set number " 显示行号
set nowrap
set mouse=a " 开启鼠标,默认是不能使用鼠标的
set autoindent " 开启自动缩进
"set iminsert=2 " 在 gvim 中存在很大的问题——我使用的是 rime 的郑码,进入
"insert mode 会导致输入法变为郑码,但我想用的是英文,因为,我用 vim
"是为了写代码。使用这个选项,不如直接使用 windows 的输入切换方式。
"}}} ## option 设置
"## runtimepath 路径 {{{
"查看当前的 runtimepath
set runtimepath+=~\.vim\UltiSnips
set runtimepath+=~\.vim
"}}} ## rumtimepath 路径
"## mapping {{{
"### leader key {{{
"let mapleader = "\\"
let mapleader = "\<space>"
"let mapleader = " "
nnoremap <LEADER>z ZZ
nnoremap <LEADER>t <c-w>T
nnoremap <LEADER>c :!start cmd<CR>
nnoremap <LEADER>o :tabnew $MYVIMRC<CR>
nnoremap <LEADER>mr :set nornu<CR>
nnoremap <LEADER>mo :browse oldfiles<CR>
nnoremap <LEADER>q :
nnoremap <Leader>g :call SwitchGui()<CR>
nnoremap <Leader>h <C-W>h
nnoremap <Leader>j <C-W>j
nnoremap <Leader>k <C-W>k
nnoremap <Leader>l <C-W>l
"}}} leader key
"normal mode
"--------------------------------------------------------------------------------
"function key
nnoremap <F3> :tabnew<CR>
nnoremap <F7> :w<CR>
nnoremap <F8> <C-U>
nnoremap <F10> :wqa<CR>
"nnoremap <LEADER>mn :set nu<CR>
"nnoremap <LEADER>md :tabnew $HOME/AppData/Local/nvim/pack/plugins/start/me<CR>
"insert mode
""--------------------------------------------------------------------------------
inoremap <F2> <CTRL-E>
inoremap <F9> <Esc>
inoremap <c-q> <Esc>:
"inoremap <c-s> <esc>:w<cr>
"inoremap <C-L> <C-X><C-L>
"inoremap <C-D> <C-X><C-D>
"inoremap <C-F> <C-X><C-F>
"inoremap <C-]> <C-X><C-]>
"g-prefix mode
"-------------------------------------------------------------------------------
"使用 :h g
"查看默认的映射
nnoremap go :call OnlyTab()<CR>
nnoremap cx :call CopyLine()<CR>
nnoremap <M-o> <C-W>o
inoremap <M-j> <ESC>
"nnoremap <Space> <C-d>
nnoremap <M-w> <C-w>
nnoremap <M-d> <C-d>
nnoremap <M-u> <C-u>
inoremap <M-n> <C-n>
inoremap <M-p> <C-p>
nnoremap <c-q> :
"nnoremap <c-v> <c-q> "与终端中的快捷键冲突
nnoremap <C-n> :bel vnew<cr>
nnoremap <c-s> :w<cr>
"}}} ## mapping
"## autocmd : au {{{
au! BufNewFile *.cpp,*.h,*.hpp setlocal foldmethod=syntax
au! BufRead *.cpp,*.h,*.hpp setlocal foldmethod=syntax
au! BufRead *.ixx set filetype=cpp
au! BufRead *.md set wrap
"}}} ## autocmd
"## abbreviations {{{
"iab ce std::cout << << std::endl;<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>
"iab ff for(int i = 0; i < n; ++i)<CR>{<CR><SPACE><SPACE><SPACE><SPACE>
"iab rr std::random_device rd;<CR>std::uniform_int_distribution dist(0, 10);
"使用插件就好
"}}} ## abbreviations
"## colorscheme {{{
"tags: cs, color
set tgc
"hi Normal guibg=NONE
" 在 windows terminal 上,可以使背景色变为透明。
"}}} ## colorscheme
"## 自定义状态栏 {{{
"set statusline=[%n]%1*%F%=%0*%2*%l,%c\ %p%%%0*\|ascii=%b,hex=%B%{((&fenc==\"\")?\"\":\"\ \|\ \\".&fenc)}\ \|\ \%{$USER}\ @\ %{hostname()}\
"set statusline=[%n]\ %f%m%r%h\ \|\ \ \ \|%=\|\ %l,%c\ %p%%\ \|\ ascii=%b,hex=%b%{((&fenc==\"\")?\"\":\"\ \|\\".&fenc)}\ \|\ \%{$USER}\ @\ %{hostname()}\
"set statusline=[%n]\ %f%m%r%h\ \|\ \ pwd:\ %{CurrentDir()}\ \|%=\|\ %l,%c\ %p%%\ \|\ ascii=%b,hex=%b%{((&fenc==\"\")?\"\":\"\ \|\\".&fenc)}\ \|\ \%{$USER}\ @\ %{hostname()}\
"使用插件就好
"}}} ## 自定义状态栏
"## register {{{
" 清空寄存器
"let @a =""
"}}} ## register
"}}} # 基础设置
"# 自定义的函数(并不是依赖项) {{{
"Function: 向下复制行而不改变光标位置
function! CopyLine()
let l:old_cursor = getcursorcharpos()
execute "normal vYp"
call setcursorcharpos(l:old_cursor[1]+1, l:old_cursor[2])
endfunction
"Function: 使窗口位于中间,并打开顶部打开一个预览窗口
function! PlaceholderWindow()
"wincmd T
let winid = win_getid()
vnew
vertical resize 55
call win_gotoid(winid)
split
resize 5
call win_gotoid(winid)
endfunction
"Function: C++ 插入预防宏
function! Ifndef()
let name = input("请输入宏名:")
if empty(name) == 1
return
endif
let macro_name = toupper(name) .. "_H_"
let content = [
\ "#ifndef " .. macro_name,
\ "#define " .. macro_name,
\]
call append(line("0"), content)
call append(line("$"), "#endif // " .. macro_name)
endfunction
"Function: 将包含网址的行,变为 markdown 超链接格式
function! MDLink()
execute "normal I[](\<Esc>A)\<Esc>0a"
endfunction
command -nargs=0 MDLink call MDLink()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异