How does vim simplify frequently used commands

  question, vim

Problem description

In vim’s command mode, how should complex commands that are frequently used be simplified? Is it enough to design shortcut keys?

I often use a replacement command: 0,$s/t/ /g is to replace the tab key with 4 spaces.

For newly-created files, I have already configured them in the configuration file. However, the tab key often appears in other people’s files.

I have to replace it every time.

My question

  1. I am not very familiar with vim shortcut keys. Can I use shortcut keys? How to set it up? Replace with shortcut keys: 0,$s/t/ /g common commands

  2. Can you switch automatically when vim opens the file, so that you don’t have to set the shortcut keys?

Solution

First of all, I would like to thank the following respondents. I read the help documents on the explanations they gave and finally came up with a method that I think is reasonable.

  1. Shortcut keys are certainly possible, but they need to be remembered. For my current problem, I prefer automatic conversion.

  2. First of all:help retLooking at the documents, what I want to say here is that when replacing,Be careful to replace the\\tCharacter

  3. :help autoLook at the documents, not much more.

  4. Look at the. vimrc file. vimrc file already containsif has("autocmd")Part, write the corresponding commands together.

  5. autocmd BufRead *.cpp,*.h :retThis automatic replacement command, BufRead and BufReadPost, are the same. here you want toNote: For mode options, it is better not to replace any type of file with *, and write out the suffix to prevent erroneous replacement of files in other formats.

  6. This replacement command only replaces the contents in Buf cache. If it is not modified and not saved, it will not be written. If it exits directly, vim will not prompt you to save and tab key still exists.This is not harmful.

Finally, thanks again to the following respondents. Happy New Year.

First of all, let’s throw bricks to attract jade:

  1. Since you already have an operation, it is very simple to set a shortcut key for this operation, for example, this shortcut key is set to normal mode.<Leader>tsnnoremap <Leader>ts :0,$s/t/ /g<CR>, probably isThe mode shortcut key operation for which you want shortcut key mapping.

  2. The second can be usedautocmd BufRead * :%ret! 4<CR>, converts the tab of the entire file into space, each tab is replaced by 4 spaces. ret refers to replace tab, and more content can be:help ret,:help autocmd,:help event,:help BufRead.