[vim skills] some experience in writing code with vim

insert image description here

I have been using vim on the server for so long since 2015, but I have not been able to use it efficiently. Now I want to systematically start dabbling and practice some vim skills repeatedly, and continue to update the tossing experience in this blog post; - ----- 2021/08/03

In general, efficient text operations can be divided into: text editing, text selection, text copy and paste, replacement and rewriting, repeatable operations, etc.;

The register content is stored in "~/.viminfo" and will still be loaded when vim is restarted next time;

Agreement: If there is a corresponding long command, I only write a short command;


Seven Good Habits of Text Editing

Here are the seven habits of highly effective text editing advocated by Vim author Bram Moolenaar (derived from "The 7 habits of highly effective people"):

  • Move the cursor quickly : Most of the time in text editing is spent browsing, checking for errors or finding the correct position for your editing work, and typing new content or changing existing content is a second priority. Random roaming in the text is very common operation. Therefore, the first priority of efficient editing is to learn how to move quickly and position accurately in the text.
  • Don't type the same thing twice : the most common thing you'll want to do is change one word to another, and if you want to change the word to another everywhere in the file, you might consider using ": s" command, if you want to make the change selectively, and it's best to decide after seeing the context, you can use the "*" command to find another match for the word, and if you decide to change it, use "cw" to use the change These words, and then use the "n" command to go to the next match and use the "." to repeat the previous command. The "." command repeats the last change.
  • Correct mistakes immediately : Typing mistakes are inevitable, auto-completion or correcting through abbreviations is a good way to avoid them;
:abbr pn pinguin
:abbr MS Mandrake Software 
  • Frequent multi-file editing : People often don't just edit one file. There are usually multiple related files. It may be necessary to edit several files at a time after editing a file individually. Or edit several files at the same time. To perform efficient editing, you must Make full use of the editor's ability to edit multiple files at a time. The tag mechanism can be used to jump between multiple files. The usual method is to generate a tag file for the project you are working on. Then you can edit multiple files in this project Jump freely between files, discover function definitions, structs, typedefs, etc.
  • Learn to use multiple tools in combination : the functions of programs can be combined to produce powerful processing power. The philosophy of Unix is ​​to use independent small programs, each small program does a special task, and does it well, combining their Work put together to complete a complex task.
  • Understand text with a structured mind : Chances are that the text you work with often has some internal structure. It's just not supported by the currently available commands, and you may have to go back and build your own macros and scripts to manipulate the text . This is obviously somewhat complicated. The simplest example is to speed up your edit-compile-modify cycle. Vim has its own ":make" command, which compiles your program project, catches compilation errors/warnings and Allows you to jump directly to the program line that caused the error/warning.
  • Stick to good practice and make it a habit : You don't have to learn all the commands an editor has to offer. It's just a waste of your time. Most people only need to learn 10-20% of the commands and they are good enough for their work . But for everyone, the suitable command set is different, which requires you to review what you have done in the past from time to time to see if some repetitive work can be done automatically. If you only perform a special If you don't expect to perform similar operations in the future, don't try to figure it out.

global skills

:sav main.cpp // 文件另存为
:clo // 关闭当前窗口

Text browsing skills

:Shift + w/b/e // 比存粹的w/b/e要快
:12gg // 移动12行
:Ctrl + b // 向上滚动一屏(相当于Page Up)
:Ctrl + f // 向下滚动一屏(相当于Page Down)

text copy paste

:Ctrl + rx // 插入寄存器 x 的内容

In visualization mode:

:aw // 选择当前单词
:ab // 选择被()包裹的区域(含括号)

Text Editor

:ciw // 将光标所在的单词删除,然后进入插入模式

plugin installation

A so-called "Vim package" is a directory that contains one or more plugins. By default, your Vim settings are contained in "~/.vim", which is where Vim looks for plugins at startup; when you start Vim , it first processes your .vimrc file, then scans all directories in "/.vim" for plugins contained in "pack/*/start"; by default there is no such in the "/.vim" directory File structure, set the path to store the package as:

$ mkdir -p ~/.vim/pack/vendor/start
$ ls ~/.vim/pack/vendor/start
commentary  nerdtree

Try installing NERDTree, vim-commentary:

$ git clone --depth 1  https://github.com/preservim/nerdtree.git  ~/.vim/pack/vendor/start/nerdtree
$ git clone https://github.com/tpope/vim-commentary.git ~/.vim/pack/vendor/start/commentary

安装vim-plug,Vundle(vim-plug may be installed by copying the .vim file manually because of the network problem):

$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

The final .vimrc configuration file is as follows:

call plug#begin()
call plug#end()

set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'preservim/NERDTree'
Plugin 'tpope/vim-commentary' 
call vundle#end()
filetype plugin indent on

set tags=/Project_Reality/code_reading/cryptominisat-master/src/tags

macro

Simply use the macro, note that there is no need to input ":", press "qa" to start recording the macro to register a, and then complete a series of operations, press "q" to complete the recording of the macro; replay stage: "n@a
" (where n is the number of replays);
view the macro: ":register a";

Guess you like

Origin blog.csdn.net/hanss2/article/details/119350609