search

Home  >  Q&A  >  body text

How to simplify frequently used commands in vim

Problem Description

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.

my question

  1. 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

  2. Is it possible to automatically convert the file when vim opens it, so that you don’t even need to set the shortcut keys?

Solution

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

  1. Shortcut keys are definitely possible, but you need to remember the shortcut keys. For my current problem, I choose automatic conversion.

  2. 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

  3. :help auto Check the documentation, not much to say.

  4. Check the .vimrc file. There is already a if has("autocmd") section in the vimrc file. Just write the corresponding commands together.

  5. autocmd BufRead *.cpp,*.h :retThis 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.

  6. 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.

Finally, thank you again to the responders below. Happy New Year.

伊谢尔伦伊谢尔伦2779 days ago676

reply all(4)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 16:36:21

    Let’s start with some tips:

    1. 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.

    2. 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.

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 16:36:21

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 16:36:21

    The most awesome editor in the world: Vim 1 (all examples of original animation demonstration) http://www.imooc.com/article/...

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 16:36:21

    You can completely customize the shortcut keys, just play .vimrc well

    reply
    0
  • Cancelreply