In the command mode of vim, how to simplify frequently used complex commands? Is it possible to design shortcut keys?
I often use a replacement command: 0,$s/t/ /g to replace the tab key with 4 spaces.
For the newly created files, I have already made the configuration in the configuration file. It’s just that the tab key often appears in other people’s files.
I have to replace it every time.
I am not very familiar with vim setting shortcut keys. Can I use shortcut keys? How to set it? Use shortcut keys to replace: 0,$s/t/ /g Common commands
Is it possible to automatically convert the file when vim opens it, so that you don’t even need to set the shortcut keys?
First of all, I would like to thank the responders below. Based on their explanations and reading the help documents, I finally came up with a method that I think is reasonable
Shortcut keys are definitely possible, but you need to remember the shortcut keys. For my current problem, I choose automatic conversion.
First :help ret
Check the document. What I want to say here is that when replacing, be careful to replace the \\t
in the string in the program. character
:help auto
Check the documentation, not much to say.
Check the .vimrc file. There is already a if has("autocmd")
section in the vimrc file. Just write the corresponding commands together.
autocmd BufRead *.cpp,*.h :ret
This automatic replacement command, BufRead and BufReadPost are the same. Note here: mode option, it is best not to use it *, specifically what type of file is to be replaced, write the suffix to prevent accidental replacement of files in other formats.
This replacement command only replaces the content in the Buf cache. If there is no modification or saving, it will not be written. If you exit directly without any modification, vim will not prompt you to save. , the tab key still exists. This is harmless.
Let’s start with some tips:
Now that you already have an operation, it is very simple to set a shortcut key for this operation. For example, set the shortcut key to
<Leader>ts
--nnoremap <Leader>ts :0,$s/t/ /g<CR>
, 大概是希望快捷键映射的模式 快捷键 操作
in normal mode like this.The second one can be used
autocmd BufRead * :%ret! 4<CR>
, 将整个文件的 tab 转换成 space,每个 tab 用 4 个 space 替代. ret 指的是 replace tab, 更多内容可以:help ret
,:help autocmd
,:help event
,:help BufRead
.vim has a command called
:retab
(I may have remembered it wrong, but it should be the same), which can convert tabs to spaces. You can check the related help. If you use it frequently, you can bind it to a key.The most awesome editor in the world: Vim 1 (all examples of original animation demonstration) http://www.imooc.com/article/...
You can completely customize the shortcut keys, just play .vimrc well