配置文件中的特殊用法:
autocmd 自动执行命令
autocmd FileType text setlocal textwidth=78 设置 文本文件 文本宽度为78
手动设置文件的高亮配置文件:
在文件开头或者结尾写入 # vim: syntax=make
在VIM中执行shell的时候可以配置shell备置列表如下:
'shell' specifies the program that Vim uses to execute external programs.
'shellcmdflag' argument to pass a command to the shell
'shellquote' quote to be used around the command
'shellxquote' quote to be used around the command and redirection
'shelltype' kind of shell (only for the Amiga)
'shellslash' use forward slashes in the command (only for MS-Windows and alikes)
'shellredir' string used to write the command output into a file
4.搜索
搜索支持正则表达式
/ 向后搜索
? 向前所搜
n 下一个匹配项
N 上一个匹配项
* 从上向下搜索单词
# 从下向上搜索单词
搜索单词: /\<the\> 这样指定了单词的开始和结尾
5.常用套路技巧:
套路:
x delete character under the cursor (short for "dl")
X delete character before the cursor (short for "dh")
D delete from cursor to end of line (short for "d$")
dw delete from cursor to next start of word
db delete from cursor to previous start of word
diw delete word under the cursor (excluding white space)
daw delete word under the cursor (including white space)
dG delete until the end of the file
dgg delete until the start of the file
技巧:
'* 是选区寄存器(用鼠标中键) '+ 是粘贴寄存器(一般是菜单里的复制粘贴选项)
Insert模式和Replace模式可以用Insert键切换
将Doe,John 替换成 John Doe 用如下方法:
:%s/\([^,]*\),\(.*\)/\2 \1/ 其中\1 表示正则表达式中第一个匹配部分,整句的匹配部分用 \0表示
反序排列的方法:
首先到要反序的第一行,用mt标注当前位置,然后到要替换的反序的最后一行,输入命令:'t+1,.g/^/m 't 可以用.范围(global默认是%)然后编辑反射这样就可以挑出想要的内容,还可以不确定范围用0代替 't将对整个文件进行反序
挑选文本的方法:
:map <F4> :.g/^/co 'p<enter> :normal <c-o><enter> 需要首先用mp定义p的位置
6.配色方案:
命令:
:syntax on 打开语法高亮
:syntax clear 清楚当前的语法高亮
:syntax off 关闭语法高亮
:syntax manual 将高亮模式设置为手动
:set syntax=ON 设置以上选项后,用这个命令开启语法高亮
:filetype on 打开文件类型检测
:set filetype 设置当前文件类型
:plugin on 使用文件类型插件
:indent on 打开文件类型自动缩进
:set background 手动配置背景色(VIM一般会自动推断终端的背景色) 此命令必须放置在 :syntax enable 前 否则不起作用
:syntax reset vim重新设置默认颜色
:runtime syntax/colortest.vim 查看配色方案的实例
:colorscheme 选择配色方案
:colorscheme mine 一直使用自己的配色方案
CTRL-L 重绘VIM的屏幕
配色方案文件中的关键字及作用:
配色方案的路径VIMRUNTIME/colors ~/.vim/colors
term attributes in a B&W terminal
cterm attributes in a color terminal
ctermfg foreground color in a color terminal
ctermbg background color in a color terminal
gui attributes in the GUI
guifg foreground color in the GUI
guibg background color in the GUI
可以这样设置:highlight Comment ctermfg=green guifg=green
7.命令记录与回放
q{a-z} 用于开始记录宏,放在寄存器里面的命令可以当成文本粘贴出来,大写字母表示追加
q 结束宏
@{a-z} 执行宏
@@ 用上一次执行宏的寄存器执行宏
8.搜索替换
:[range]substitute/from/to/[flags] 搜索替换的格式 substitute缩写为s,=和+也可以用于分隔符
[range] 默认为当前行
[flags] 默认指替换第一个
g(gloabl) 替换范围内全局
p(print) 显示替换的最后一行
e 告诉VIM未找到匹配时不要报错
c(confirm) 每次替换要求用户确认
y Yes; make this change.
n No; skip this match.
a All; make this change and all remaining ones without further confirmation.
q Quit; don't make any more changes.
l Last; make this change and then quit.
CTRL-E Scroll the text one line up.
CTRL-Y Scroll the text one line down.
9.全局命令
:[range]global/{pattern}/{command} 全局命令的一般形式,其中global可以缩写成g,command必须是:命令,正常模式下的命令可以通过 :normal实现
10.命令范围
任何命令都可以有一个范围
{number} number是行号,指定一行
{number},{number} 指定多行,包含指定的两行,这里的number可以是一个搜索模式, ?abc? 表示向上搜索abc /abc/ 表示向下搜索abc ,当开始行大于结束行的时候它会问你是否交换两个参数,可以在任何number后面添加偏移量,例如: .+1,$-3 或者 ?a?+1,/b/-1等等,还可以在visual模式下选择范围当按下:时会出现 :'<,'> 这时的作用范围即是选区开始的字符到选区结束的字符,要注意的是即使是CTRL-V进入的块选区也是以行为基本单位
. 光标的当前行
1 第一行
$ 最后一行
% 相当于 1,$
'a-z' number还可以用mark表示
`< 上一个选区开始的位置
`> 上一个选区结束的位置
'< 上一个选区开始的行
'> 上一个选区结束的行
5: 就是在:前面加计数器将会在命令栏打开 :.,.+4 可以用任意的计数器
更多嵌入式、Linux学习联系郭老师Q754634522,深圳-广州-郑州-长沙均有学习分点
*.需要注意的:
1. d2e 和 d2w 的区别
2. cw 和 ce 的结果一样,都是c2e的结果
3. ^命令加上计数器没有效果
4. Y和yy 的结果一样,都是yy的结果,如果想用Y的效果只能y$了
5. vnoremap _g y:exe "grep /" . escape(@", '\\/') . "/ *.c *.h"<CR> 这句没看懂 05.2* The example vimrc file explained
6. autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
没看懂 05.2* The example vimrc file explained
7. 关于自定义高亮 查看 mysyntaxfile 和 syntax
8. 关于自定义文件类型 查看 new-filetype|