Vim学习笔记

本文记录一下在学习Vim过程中的经验和总结:

Vim删除查找匹配的行

例如,要全局替换掉包含file:的行:

:g/file:/d

参考资料:

  1. vim 删除匹配行_中国风2012的博客-CSDN博客_vim 删除匹配行

Vim复制、粘贴然后替换

以下面代码为例:

我们的目的是将pass替换为if __name__ == "__main__"。步骤如下:
1.在visual模式下选择if __name__ == "__main__",然后在normal模式下按下y复制。

2.在visual模式下选择pass,按下y即可进行替换:

便可得到下述结果:

参考资料:

  1. Copy, delete, then paste in Vim - Super User

Vim移动

1.wbee用于移动到光标所在位置的下一个单词的末尾。
2.WBE。跳转到下一个空格。
3.%。跳到对应的括号。

Vim Tab页切换

1.gt:右边的tab页。已经映射为<leader>l
2.gT:左边的tab页。已经映射为<leader>h

leetcode网页端Vim模式

问题1:按下Esc键会跳出编辑窗口。
解决方案:编辑前使用:imap jj <Esc>进行映射,或者使用Ctrl+C进入normal模式。

参考资料:

  1. VIM Command Mode

Vim配置特定主题

参考资料:

  1. Best vim color schemes and how to install

Vim禁止闪烁

参考资料:

  1. vim - Disable blinking at the first/last line of the file - Stack Overflow
  2. terminal - How to prevent Vim from making a flashy screen effect when pressing ESC or ^[ in normal mode? - Vi and Vim Stack Exchange

git bash Vim主题显示和对应主题不一致

推测原因为终端不支持256 color,见参考资料2。可在.zshrc中添加如下配置:

export TERM=xterm-256color

参考资料:

  1. Reddit - Dive into anything
  2. Configuration wizard missing · Issue #1840 · romkatv/powerlevel10k · GitHub

Vim复制到剪切板 clipboard

参考资料3设置有效

参考资料:
1.What is difference between Vim’s clipboard “unnamed” and “unnamedplus” settings? - Stack Overflow
2. Windows Subsystem Linux - Make VIM use the clipboard? - Super User
3. Vim Wsl Clipboard

git中使用zsh在git仓库中响应过慢

# 当前仓库配置
git config --add oh-my-zsh.hide-status 1
git config --add oh-my-zsh.hide-dirty 1

# 全局配置
git config --global --add oh-my-zsh.hide-status 1
git config --global --add oh-my-zsh.hide-dirty 1

参考资料:

  1. oh-my-zsh slow, but only for certain Git repo - Stack Overflow

VSCode中使用j/k切换IntelliSense suggestions

keybindings.json中添加:

{
	"key": "j",
	"command": "selectNextSuggestion",
	"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus && vim.mode=='Insert'"
},
{
	"key": "k",
	"command": "selectPrevSuggestion",
	"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus && vim.mode=='Insert'"
}

参考资料:

  1. Using vim-like navigation for IntelliSense suggestions in VSCode | by Airy | Medium

Vim映射键

参考资料:

  1. GitHub - VSCodeVim/Vim: Vim for Visual Studio Code
  2. key mapping - Disable or remove default vim keybinding - Unix & Linux Stack Exchange

Vim对文件显示颜色

  • :set syntax=json:临时生效,使得json文件具有颜色。还有:set syntax=html等方式。

参考资料:

  1. Vim doesn’t show a certain file with colors - Stack Overflow
  2. vim - Persistent :set syntax for a given filetype? - Stack Overflow

Vim识别文件并语法高亮

参考资料:

  1. gvimrc - How to make my vim recognize filetype and load the corresponding syntax file - Super User

vim配置行移动 (Moving lines up or down)

主要使用参考资料3
.vimrc中添加:

map <Esc>j <A-j>
map <Esc>k <A-k>
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv

参考资料:

  1. Moving lines up or down
  2. key bindings - How can I easily move a line? - Vi and Vim Stack Exchange
  3. vim - Any mapping in vimrc with Alt isn’t working - Stack Overflow

Vim中删除空行

:g/^$/d

PS:由于vscode中vim插件为vim模拟器,未实现g (global) 功能。故考虑迁移到vscode-neovim (其非模拟器,而是之间调用本地安装的neovim)

参考资料:

  1. vim删除空行和注释 - 碳 - 博客园

Vim使用H/L移动到wrap line的行首/行尾

noremap <silent> H g^ // g^和g0等价,即0和^等价
noremap <silent> L g$

参考资料:

  1. Move cursor by display lines when wrapping | Vim Tips Wiki | Fandom