在C/ c++开发环境中最有用的/使用的vim命令是什么

时间:2022-04-15 16:06:32

Here is the list of mine

这是我的清单

Unlike me -- as I did it for illustrative purposes -- don't paste too many.

与我不同的是——我这样做是为了说明问题——不要粘贴太多。

And most importantly, provide an explanation

最重要的是,给出一个解释

Commands shouldn't be generic but relevant to C++/C environment. ctags & scope are welcome too

命令不应该是通用的,而应该与c++ /C环境相关。ctag & scope也很受欢迎。

gi .....................init insert mode in last insertion position

'0 .....................open last edited file

gf .....................open file under cursor in same window 

Ctrl-w q ...............close current window

:setlocal autoread .....Auto reloads the current buffer..especially useful while viewing log files

for i in range(1,255) | .put='10.0.0.'.i | endfor....   insert range ip's

g; and g, .......................to move (forward, backward) through the changelist

fx Move the cursor forward to the next occurrence of the character x on the current line (obviously, x can be any character you like). This is an extremely useful command. You can type ; to repeat the last f command you gave. 

tx Same as above, but moves the cursor to right before the character, not all the way to it. (It's very useful, really.) 

Fx Move the cursor backward to the next occurrence of the character x on the current line. 
w Move the cursor forward by a word. 
b Move the cursor backward by a word. 
0 Move the cursor to the beginning of the current line. 
^ Move the cursor to the first character on the current line. 
$ Move the cursor to the end of the line 


Visual search ....... you can simply yank the selected text with y and go to search mode 
/, then you can paste the last yanked text with Ctrl+R 0

ci" - cuts the text in current quotes
ciw - cuts the current word. This works just like the previous one except that ( is replaced with w.
C - cut the rest of the line and switch to Insert mode.
zz -- it scrolls the screen to make this line appear in the middle
C - cut the rest of the line and switch to Insert mode.
de - delete from cursor to the end of the word (you can also do dE to delete until the next space)
df[space] -- delete up until and including the next space
bye -- copies current word 
b and e move the cursor word-by-word
capital D (take a deep breath) Deletes the rest of the line to the right 
cd %:h change to current directory
:r! <command> pastes the output of an external command into the buffer.
:%s/foo/bar(&)/g will look for foo, and surround the matched pattern with bar().
:s/.*/PREFIX & SUFFIX/ you want to add a prefix and a suffix simultaneously, you can do something like this:

gd....... keystroke stands for Goto Declaration
gD....... This takes you to the global declaration of the variable under the cursor
------------------
:make error
[make_error]
On pressing RETURN, the cursor moves to line number 6
Now, the command :cn will move the cursor to the line number 4. 
To move back to the previous error, one can use the command :cN and the cursor will move back to the line 6. 
After correcting the error on line 5 and adding "return 1;", one can run :make again and the output will be 


---------
:%!grep sdf | sort -n -k3

1)select the whole content using '%' 
2) pipe it to an external command using '!' 
3) grep onyl the lines containing 'sdf' 
4) sort these lines numerically (-n) on the third field (-k3)


d$ will delete from current position to end of line 
d^ will delete from current backward to first non-white-space character 
d0 will delete from current backward to beginning of line 
dw deletes current to end of current word (including trailing space) 
db deletes current to beginning of current word 

:%s/pattern//gn........... For counting the number of times some pattern occurs, use:

CTRL-O   Go to [count] Older cursor position in jump list  
CTRL-I   Go to [count] newer cursor position in jump list


zz - line that has a cursor is in the middle of the screen
zt - line that has a cursor is in the top of the screen
zb - line that has a cursor is in the buttom of the screen

set printoptions=number:y  ...set numbers in a hardcopy 
:hardcopy.... to print the file :w

shift d ...... Deleting from current position to end of line
vim -o ....... allows you to open two windows, split vertically horizontally
vim -O ....... allows you to open two windows, split vertically
CTRL+W CTRL-Q ......to close the current windows
qall.........How do I quit all windows

0 ...First position on line
Ctrl g ...where am I
:set wrapmargin=70
printexpr=system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error

Tab block of code ....select your block of code (with [V]isual line mode normally), then press > or <.
If you want to tab more than once, 2> or 3> to repeat it.

News:

新闻:

 Guys, let's REOPEN THIS QUESTION, and go WIKI-CRAZY!

7 个解决方案

#1


4  

One set of commands I find useful is [[, ]], [], ][. They navigate through curly braces on the first column, so they effectively allow you to walk through functions if you use proper indentation.

我认为有用的一组命令是[[[,]],[],[]。它们在第一列的花括号中导航,因此如果使用适当的缩进,它们可以有效地让您遍历函数。

#2


4  

The ones I overuse are the following :

我过度使用的有以下几点:

  • :AV to vertically split the current window and open the header/source file associated to the respective source/header file (if not yet opened, otherwise we jump to its window instead)
  • :AV垂直分割当前窗口并打开与相应的源/头文件相关联的头/源文件(如果尚未打开,则跳转到它的窗口)
  • :GSp and :GVSp to split the current window and open the requested file (which is somewhere in &path), or jump to the file if it is already opened
  • :GSp和:GVSp拆分当前窗口并打开所请求的文件(在&path中的某处),如果文件已经打开,则跳转到该文件
  • <m-x> to toggle comment on the current line
  • 切换当前行的注释
  • :Make to compile the current project in the background -- NB: a flag needs to be set in order to do so
  • :Make在后台编译当前项目——NB:需要设置标志才能编译
  • <c-x>be to add .begin(), /container_name/.end() in whatever(container_name<curoser_here>)
  • 是在任何(container_name )中添加.begin()、/container_name/.end()
  • #i which will expand into #include
  • #i将扩展到#include
  • :DOX which will add a doxygen comment to the current function prototype -- parameters const-/ref-ness, throw spec, return type are taken into account
  • :DOX将向当前函数原型添加doxygen注释——考虑参数const-/ref-ness、throw spec、return type
  • :GOTOIMPL which will create a default body from the current function declaration (or jump to the already existing body when possible)
  • :GOTOIMPL,它将从当前函数声明中创建一个默认主体(或在可能的情况下跳转到已经存在的主体)
  • <c-w><m-down> : another way to navigate the tags base
  • :另一种导航标签基的方法
  • for/if/... : expand into the relevant code snippet in insert mode (outside of string/comment context)
  • /如果/…:以插入模式展开到相关代码片段(在字符串/注释上下文之外)
  • ,for/,if/.. and ,,for/,,if/... to surround the current selection with the relevant code snippet, the selection will go into the control-statement body (one ,), or its condition (two ,)
  • ,如果/ / . .为/,,,,,如果/……要用相关的代码片段包围当前的选择,选择将进入control-statement body(1,)或其条件(2,)
  • tpl expands into template <<cursor>><+placeholder+>
  • tpl扩展为模板< <游标> ><+占位符+>
  • all the text-object motions with =, d, c, ... + di,/vi,/... that acts on the current parameter
  • 所有的文本对象运动=,d, c,…+ di / vi,/…它作用于当前参数
  • <c-x>v, <c-x>t to extract the selected variable/type (refactoring)
  • v, t提取所选变量/类型(重构)
  • All the bracket opening characters + <m-del> to maintain balanced brackets
  • 所有的括号开头字符+ 以保持括号的平衡

There are many other commands I use when I develop in C++, but less often -- just explore the links I've given.

在使用c++开发时,我还使用了许多其他命令,但很少使用这些命令——只要研究一下我提供的链接就可以了。

#3


3  

Those may be usefull for programming

这些可能对编程有用

= - to intend text. To indent all file g CTRL+V G =

= -意图文本。缩进所有文件g CTRL+V g =

CTRL-P/CTRL-O - to complete text

CTRL-P/CTRL-O -完成文本

}y - to paste block and indent it correctly in a new place. For example code block in clipboard is 2 level indentation and should be pasted in code, where it will be in 3 level indentation.

}y -粘贴块并在新位置正确缩进。例如,剪贴板中的代码块是2级缩进,应该粘贴到代码中,在代码中,它将位于3级缩进。

CTRL-X+f to complete file name

CTRL-X+f完成文件名

>>/<< - increase/decrease indentation

> > / < < -增加/减少缩进

% - go the the corresponding open/close bracket

% -进入相应的左/右括号

minibufexpl is good plugin to work at once on several files

minibufexpl是一个很好的插件,可以在多个文件上同时工作

#4


3  

>aB to indent a block. Not frequently used, but a gem nonetheless.

用>aB对块进行缩进。虽然不常用,但却是一颗宝石。

#5


2  

Using different views is usefull for side by side code comparison.
Note each view created by a split can hold a separate file.

使用不同的视图进行并行代码比较是有用的。请注意,由拆分创建的每个视图都可以保存一个单独的文件。

To split the view horizontally

水平分割视图

:split

To split the view vertically

垂直分割视图

:vsplit

To move between split views

在分割视图之间移动

^W<arrow>      (Thats control W) (Arrow Key)

Once you have set up the tags file:

一旦设置好标签文件:

^]             (Move over identifier you want to find: Hit Control ])
:tn            Next Tag
:tp            Previous Tag
:pop           Pop back to the place you where when you hit ^]

#6


1  

You should take a look at SnippetsEmu. A great plugin that will save you a lot of regularly typed words in C.

你应该看看鹬鸵。这是一个很好的插件,它可以为你节省很多C语言的输入。

Note: You're asking about commands and not plugins, but just wanted to mention this one anyway.

注意:您询问的是命令,而不是插件,但无论如何,您只想提到这个命令。

#7


1  

The best I've ever used is when I used a combination of combining running make and Quickfix. I pressed F6 to compile, and then F7 to move backwards through the errors and F8 to move forwards by using the line numbers in the gcc warning / error output. Quicker than alt-tab, press up.

我用过的最好的方法是结合使用跑步make和Quickfix。我按下F6进行编译,然后F7通过错误向后移动,F8通过使用gcc警告/错误输出中的行号向前移动。比alt-tab键更快,按下。

#1


4  

One set of commands I find useful is [[, ]], [], ][. They navigate through curly braces on the first column, so they effectively allow you to walk through functions if you use proper indentation.

我认为有用的一组命令是[[[,]],[],[]。它们在第一列的花括号中导航,因此如果使用适当的缩进,它们可以有效地让您遍历函数。

#2


4  

The ones I overuse are the following :

我过度使用的有以下几点:

  • :AV to vertically split the current window and open the header/source file associated to the respective source/header file (if not yet opened, otherwise we jump to its window instead)
  • :AV垂直分割当前窗口并打开与相应的源/头文件相关联的头/源文件(如果尚未打开,则跳转到它的窗口)
  • :GSp and :GVSp to split the current window and open the requested file (which is somewhere in &path), or jump to the file if it is already opened
  • :GSp和:GVSp拆分当前窗口并打开所请求的文件(在&path中的某处),如果文件已经打开,则跳转到该文件
  • <m-x> to toggle comment on the current line
  • 切换当前行的注释
  • :Make to compile the current project in the background -- NB: a flag needs to be set in order to do so
  • :Make在后台编译当前项目——NB:需要设置标志才能编译
  • <c-x>be to add .begin(), /container_name/.end() in whatever(container_name<curoser_here>)
  • 是在任何(container_name )中添加.begin()、/container_name/.end()
  • #i which will expand into #include
  • #i将扩展到#include
  • :DOX which will add a doxygen comment to the current function prototype -- parameters const-/ref-ness, throw spec, return type are taken into account
  • :DOX将向当前函数原型添加doxygen注释——考虑参数const-/ref-ness、throw spec、return type
  • :GOTOIMPL which will create a default body from the current function declaration (or jump to the already existing body when possible)
  • :GOTOIMPL,它将从当前函数声明中创建一个默认主体(或在可能的情况下跳转到已经存在的主体)
  • <c-w><m-down> : another way to navigate the tags base
  • :另一种导航标签基的方法
  • for/if/... : expand into the relevant code snippet in insert mode (outside of string/comment context)
  • /如果/…:以插入模式展开到相关代码片段(在字符串/注释上下文之外)
  • ,for/,if/.. and ,,for/,,if/... to surround the current selection with the relevant code snippet, the selection will go into the control-statement body (one ,), or its condition (two ,)
  • ,如果/ / . .为/,,,,,如果/……要用相关的代码片段包围当前的选择,选择将进入control-statement body(1,)或其条件(2,)
  • tpl expands into template <<cursor>><+placeholder+>
  • tpl扩展为模板< <游标> ><+占位符+>
  • all the text-object motions with =, d, c, ... + di,/vi,/... that acts on the current parameter
  • 所有的文本对象运动=,d, c,…+ di / vi,/…它作用于当前参数
  • <c-x>v, <c-x>t to extract the selected variable/type (refactoring)
  • v, t提取所选变量/类型(重构)
  • All the bracket opening characters + <m-del> to maintain balanced brackets
  • 所有的括号开头字符+ 以保持括号的平衡

There are many other commands I use when I develop in C++, but less often -- just explore the links I've given.

在使用c++开发时,我还使用了许多其他命令,但很少使用这些命令——只要研究一下我提供的链接就可以了。

#3


3  

Those may be usefull for programming

这些可能对编程有用

= - to intend text. To indent all file g CTRL+V G =

= -意图文本。缩进所有文件g CTRL+V g =

CTRL-P/CTRL-O - to complete text

CTRL-P/CTRL-O -完成文本

}y - to paste block and indent it correctly in a new place. For example code block in clipboard is 2 level indentation and should be pasted in code, where it will be in 3 level indentation.

}y -粘贴块并在新位置正确缩进。例如,剪贴板中的代码块是2级缩进,应该粘贴到代码中,在代码中,它将位于3级缩进。

CTRL-X+f to complete file name

CTRL-X+f完成文件名

>>/<< - increase/decrease indentation

> > / < < -增加/减少缩进

% - go the the corresponding open/close bracket

% -进入相应的左/右括号

minibufexpl is good plugin to work at once on several files

minibufexpl是一个很好的插件,可以在多个文件上同时工作

#4


3  

>aB to indent a block. Not frequently used, but a gem nonetheless.

用>aB对块进行缩进。虽然不常用,但却是一颗宝石。

#5


2  

Using different views is usefull for side by side code comparison.
Note each view created by a split can hold a separate file.

使用不同的视图进行并行代码比较是有用的。请注意,由拆分创建的每个视图都可以保存一个单独的文件。

To split the view horizontally

水平分割视图

:split

To split the view vertically

垂直分割视图

:vsplit

To move between split views

在分割视图之间移动

^W<arrow>      (Thats control W) (Arrow Key)

Once you have set up the tags file:

一旦设置好标签文件:

^]             (Move over identifier you want to find: Hit Control ])
:tn            Next Tag
:tp            Previous Tag
:pop           Pop back to the place you where when you hit ^]

#6


1  

You should take a look at SnippetsEmu. A great plugin that will save you a lot of regularly typed words in C.

你应该看看鹬鸵。这是一个很好的插件,它可以为你节省很多C语言的输入。

Note: You're asking about commands and not plugins, but just wanted to mention this one anyway.

注意:您询问的是命令,而不是插件,但无论如何,您只想提到这个命令。

#7


1  

The best I've ever used is when I used a combination of combining running make and Quickfix. I pressed F6 to compile, and then F7 to move backwards through the errors and F8 to move forwards by using the line numbers in the gcc warning / error output. Quicker than alt-tab, press up.

我用过的最好的方法是结合使用跑步make和Quickfix。我按下F6进行编译,然后F7通过错误向后移动,F8通过使用gcc警告/错误输出中的行号向前移动。比alt-tab键更快,按下。