vi

vim execute command after multiple files loaded

vi -c "au VimEnter * <command>"

vim-execute-command-after-multiple-files-loaded

indent with space

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

tab-key-4-spaces-and-auto-indent-after-curly-braces-in-vim

生成序列 生成自增序列 generate number sequence

Go to line #4, use Ctrl-v to blockwise select the first character, go down 4 lines, press Shift i, enter 0  (this is 0, followed by Space) and Esc to exit insert mode.

Now use gv to re-select the previously selected area. Press g Ctrl-a to create a sequence.

I start with a 0 here, so I can re-select by gv. If you start with a 1, you need to re-select by hand while omitting the first 1.

Use 2g Ctrl-a to use a step count of 2.

how-to-generate-a-number-sequence-in-file-using-vi-or-vim

格式化

gg=G

  1. gg到行首
  2. =格式化
  3. G到行尾

auto-indent-format-code-for-vim

https://stackoverflow.com/questions/1005/getting-root-permissions-on-a-file-inside-of-vi

使用命令而非快捷切换窗口

the equivalent to Ctrl+W L is :winc l.

how-can-i-work-with-splits-in-vim-without-ctrl-w

标记重复行

:syn clear Repeat | g/^\(.*\)\n\ze\%(.*\n\)*\1$/exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' | nohlsearch

how-can-i-mark-highlight-duplicate-lines-in-vi-editor

set file type set file mode 设置文件类型,设置高亮语法

:set ft=sql

参考:what-is-the-between-set-ft-and-setfiletype

insert tab 插入tab

While in insert mode or command mode (the : prompt at the bottom of the editor), type CTRL + V then TAB.

Using CTRL + V signals Vim that it should take the next character literally. Even in insert mode.

UPDATE:

As noted by Herbert Sitz, if gVim is in Windows mode (default), you must use CRTL + Q in place of CTRL + V.

参考:how-can-i-insert-a-real-tab-character-in-vim

按行号删除,复制 copy with line number delete with line number

:n1,n2y
例如: `:2,5y` #复制第2行到第5行

:n1,n2d
例如: `:2,5d` #删除第2行到第5行

参考:https://linoxide.com/linux-how-to/how-to-delete-line-vim-linux/

https://stackoverflow.com/questions/15069613/how-to-copy-by-specifying-line-numbers-in-vi

自动换行 wrap 配置

set wrap
set nowrap

参考:https://stackoverflow.com/questions/2280030/how-to-stop-line-breaking-in-vim

列编辑

ctrl + v

参考:https://superuser.com/questions/208852/how-to-select-a-rectangular-range-in-vim

yank cycle 循环调用剪贴板内容

可以通过调用delete的default register实现
:display #显示delete cycle环的内容
"2P #引用(")display列表中第二条(2)的内容,粘贴到当前位置之前(P)
"2p #引用(")display列表中第二条(2)的内容,粘贴到当前位置之后(p)

参考: https://stackoverflow.com/questions/17013750/vim-pasting-scroll-through-previously-yanked-text

http://vimdoc.sourceforge.net/htmldoc/undo.html#redo-register

删除空行

:g/^$/d

参考:https://stackoverflow.com/questions/1807570/vim-delete-empty-lines-within-a-visual-selected-area

删除匹配行

:g/word/normal 2dd

This finds all instances of word and then executes the command after it. In this case it executes 2dd in normal mode

参考:How to delete all lines matching a pattern and a line after in Vim

多行变单行 单行变多行

:%j#多变单

参考:https://stackoverflow.com/questions/6577508/how-to-merge-multiple-lines-into-one-line-in-vim

:%s/split character/\r/g#单变多

参考:https://stackoverflow.com/questions/71323/how-to-replace-a-character-by-a-newline-in-vim

删除重复行

:sort u

参考:https://stackoverflow.com/questions/351161/removing-duplicate-rows-in-vi

只在选定范围内搜索和替换

用v模式选择以后执行检索是会自动设定检索范围为v模式选择内容

参考:https://stackoverflow.com/questions/773137/find-and-replace-within-selection-in-vi

删除行尾空格

:%s/\s\+$// #通过匹配空格进行删除,主要要用$来限定到行尾,同样可以通过^来限定到行首实现删除行首空格

参考: https://stackoverflow.com/questions/3474709/delete-all-spaces-and-tabs-at-the-end-of-my-lines

buffer技巧

:bd #删除当前buffer,在打开多个buffer的情况下比较实用

how-do-i-close-a-single-buffer-out-of-many-in-vim

macro 宏的操作技巧

:reg #查看所有宏
:let @a = '' #删除所有宏,注意等号两边有空格
q #开始记录宏
qa #开始记录宏,并保存到快捷键a上
@a #执行宏,a为上面命令保存的快捷键
qb #开始记录宏,并保存到快捷键b上
@b #执行宏,b为上面命令保存的快捷键

clear-all-currently-defined-vim-macros

修改读取当前文件的编码格式 reread cuurent file with special encoding

:e ++enc=utf-8

Vimの文字コード関連

tab方式打开多个文件如何在编辑后自动保存 vim自动保存

set autowriteall
set awa

automatically-save-the-current-buffer-when-is-going-to-edit-a-new-file

在vim中打开多个文件,批量打开文件

:args myfile
:args **/myfile

myfile可以支持正则表达式

how-to-open-files-with-pattern-recursively-in-vim

在vi,vim的命令小窗口中粘贴内容 yank text to commond prompt

1.拷贝文本

2.按ctrl+r键

3.然后输入”

how-to-copy-yanked-text-to-vi-command-prompt

批量打开指定类型的文件 遍历打开目录下指定类型的所有文件 批量打开java文件

find . -iname '*.java' -exec vim -p {} +

上面命令,可以用标签的方式打开当前目录及子目录下所有的java文件

why-is-my-find-not-recursive   how-can-i-recursively-find-all-files-in-current-and-subfolders-based-on-wildcard

如何用su权限保存只读文件

:w !sudo tee %
:w – write
!sudo – call shell sudo command
tee – the output of write (:w) command is redirected using tee
% – current file name

how can i save a file i opened in vim as the wrong user

移动光标

j,k,l,h下上右左

:n 跳转到第n行

nG 跳转到第n行

模式

i进入插入模式

a进入插入模式光标定位到当前字符后面

Esc返回命令模式

按冒号进入命令输入窗口

编辑操作

大多数操作前加上数字n都表示重复该操作n次

yy 复制当前行

dd 删除当前行

x 删除光标后字符

p 粘贴,将复制的文本插入到光标所在位置之后

P 粘贴,将复制的文本插入到光标所在位置之前

u 撤销

命令行操作

:set number显示行号

:/word 检索word,然后按n检索一下个匹配项

:?word 向前检索word

:%s/word/newword替换

:n跳转到第n行

技巧

dd+p交换行

yy+p复制行

命令模式下按G跳转到页面最后