vi

小谈vi编辑器

空扰寡人 提交于 2019-12-05 14:27:52
vi编辑器是程序员,提升逼格,装逼的神器之一。 重要的事情提前说 ,下面就简单说一下vi编辑器的使用: 进去vi编辑器:在命令行输入:vi 创建文件:vi +文件名字+enter 进去编辑状态:i+enter vi的基本概念 基本上vi可以分为三种状态,分别是 命令模式(command mode) 插入模式(Insert mode) 底行模式(last line mode) 1) 命令行模式command mode) 控制屏幕 光标 的移动,字符、字或行的删除,移动复制某区段及进入Insert mode下,或者到 last line mode。 2) 插入模式(Insert mode) 只有在Insert mode下,才可以做文字输入,按[ESC]键可回到命令行模式。 3) 底行模式(last line mode) 将文件保存或退出vi,也可以设置编辑环境,如寻找字符串、列出行号……等。 不过一般我们在使用时把vi简化成两个模式,就是将底行模式(last line mode)也算入命令行模式command mode)。 保存文件 在冒号输入字母[w]就可以将文件保存起来。 离开vi [q]:按[q]就是退出,如果无法离开vi,可以在[q]后跟一个[!]强制离开vi。 [wq]:一般建议离开时,搭配[w]一起使用,这样在退出的时候还可以保存文件,可以在后面再跟[!]强制保存退出。

Delete and redirect lines in Vim to another file

时光总嘲笑我的痴心妄想 提交于 2019-12-05 12:02:12
I want to delete say last 10 lines and move it to another file. What I currently do is: select last 10 lines in visual mode, write these lines by :'<,'>w to other file, and then delete selected lines. Is there any more efficient way I can do this? You can use ex commands instead. e.g. :1,10w file.name Write the first ten lines :$-9,$w file.name Write the last ten lines (the dollar sign denotes last line) With the code below in your .vimrc you can use the command :MoveTo file.name function! MoveLastLines(f) exe '$-9,$w ' . a:f "write last ten lines to the passed filename $-9,$d "delete last ten

Remove full words containing a string in Vim

佐手、 提交于 2019-12-05 11:42:00
I am trying to remove words from a line if they contain a specific string. Here is an example of the text: host-a, host-b, host-c+test, host-d, host-e+test I want to remove anything that contains +test , to result with: host-a, host-b, host-d Likewise, I need to apply this on a line by line basis, not on all lines in the file. It is going to be used within a macro. How can I do this? Something like this, maybe? :%s/\<[-a-z]\++test\>//gc It will ask for each match if you want to replace it. Hint: set hlsearch will show you the matches. If you also want to remove the comma, then: %s/\(,\s\+\)\?

How to detect vi (not vim) in .vimrc?

时光毁灭记忆、已成空白 提交于 2019-12-05 10:51:40
问题 I carry a vimrc to all the machines that I work on and it naturally contains options that are not present in old vi. If I accidentally start a vi session on a machine where vi is not an alias to vim and/or vim is not installed, vi reads vimrc and throws a bunch of annoying errors to let me know that option such and such are unsupported. I know I can just always type "vim" instead of "vi" and set the EDITOR variable to vim (for visudo etc...), but is there a line I can add to the top of the

Vim - delete line in insert mode

。_饼干妹妹 提交于 2019-12-05 10:14:45
Can I delete a line in insert mode? Because currently I have to press <ESC>dd to be able to that, and I would like a quicker way to delete a line in insert mode If the cursor is at the end of the line, you can press <C-U> twice. The first one will clear the text before the cursor, the second one will remove the now empty line (and put the cursor at the end of the previous line). That said, I would not use this often. Insert mode is for inserting ; for all other edits, it's better to exit insert mode and use normal mode dd instead. Most power users quickly move into and out of various modes;

How to repeatedly add text on both sides of a word in vim?

拟墨画扇 提交于 2019-12-05 08:47:23
I have a bunch of local variable references in a Python script that I want to pull from a dictionary instead. So, I need to essentially change foo , bar , and others into env['foo'] , env['bar'] and so on. Do I need to write a regular expression and match each variable name to transform, or is there a more direct approach that I could just repeat with the . command? You can use a macro: type these commands in one go (with spacing just to insert comments) " first move to start of the relevant word (ie via search) qa " record macro into the a register. ienv['<esc> " insert relevant piece ea'] "

VI (VIM): delete/change right to left?

烂漫一生 提交于 2019-12-05 07:48:31
I often use "c-t-$char" change-till-character and "d-t-$char" and would love to be able to do the same thing in reverse. An example: cobbler reposync --only=puppetlabs-6Server-x86_64 If my cursor is at the end of the line and I want to delete backwards to the = char. What brought this on is my wanting to move from BASH emacs mode to vi mode. You can use T and F for that. For example cT= or dF= etc. d-T-$char deletes from the cursor position backwards without deleting $char , d-F-$char with deleting $char 来源: https://stackoverflow.com/questions/9154212/vi-vim-delete-change-right-to-left

Vim replace two words with one another

十年热恋 提交于 2019-12-05 07:22:13
How would one replace all instances of 'foo' with 'bar' and 'bar' with 'foo' in vim? Aside from using a temporary word for the change, you could also use abolish plugin like this: :%SubVert/{foo,bar}/{bar,foo}/g kev Take a look at this: how to write only one pattern to exchange two strings in two-ways in vim :s/foo\|bar/\={'foo':'bar','bar':'foo'}[submatch(0)]/g :%s/foo/bbaarr/g :%s/bar/foo/g :%s/bbaarr/foo/g It must exist an smartest way to do it, but this one will work for sure ! You can do it using temp word. Just be sure that it doesn't exists in the current document. /\<asd123\> :%s/\<foo

Replace word with what's ready to put in vi/vim?

大兔子大兔子 提交于 2019-12-05 06:32:19
For example, I have: rm -f $(NAME).elf $(name).d where the second 'name' is a typo, and should also be 'NAME'. So on 'N', I did y w , and then moved to 'n'. I realised that if I now hit c w to change it, I'll be in insert mode - but I thought ah well, it's only one extra key ( ESC ) and then I can just p the corrected version in. But in that case, p did exactly what u would have done, it put 'name' back. I guess because the 'old text' is loaded into the register on change too, which I didn't realise. Is there a "replace with register" command that can be applied to a word/letter/etc.? after

Show 'space' character via listchars only for leading spaces

那年仲夏 提交于 2019-12-05 04:42:37
Is it possible in Vim to have my editor (when editing .c and .h files), show via listchars , a special character only for leading space characters? I found a separate post that noted, as of version 7.4, Vim now supports highlighting all space characters via listchars . Here's my current listchars variable: set list listchars=tab:>-,trail:.,extends:>,precedes:<,space:. And here is a render of how it appears on my screen: However, I would like it to appear like so (below), where only leading spaces are rendered via listchars , and spaces occurring after indentation-related spaces are not