http://vim.wikia.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches
The traditional approach to find lines not matching a pattern is using the :v command:
:v/Warning/p
A neat trick when working with a large log file where you want to filter out as many irrelevant lines as possible before you get started on your real search is to save the file under a temporary name and delete all non-matching lines there:
:sav junk.log
:v/warning/d
You are now are editing a clone of your original file with all lines not matching "warning" removed and you can edit it at will.This will find everything but the regular expression you have specified. For example, if we want to find all the lines not containing the word 'foo', do:
/^\(\(.*foo.*\)\@!.\)*$
Here is an example: When examining a log file, you might only be interested in lines not containing the word "Warning", so the search command is:
/^\(.*Warning\)\@!.*$
To match all words except 'foo', use:
/\<\(foo\>\)\@!\k\+\>
\< Start-of-word
\(Foo\>\) The atom 'Foo' followed by end-of-word
\@! Match (with zero length) when the previous atom doesn't match.
\k\+ Match one or more Keywords
\> Match end-of-word.
The use of \@! can be very tricky. According to the Vim help files, it is often easier to use \@<! instead. For example, to find all 'bar' strings unless they are part of 'foobar', use the following (non-magic):
/\(foo\)\@<!bar
If we want to find a more complex regular expression on multiple lines, like all the lines which do not begin with 'foo' with 'bar' somewhere else and the word 'magic' at the end of the next line, do:
/^\(\(^foo.*bar.*\n.*magic$\)\@!.\)*$
https://stackoverflow.com/questions/2287440/how-to-do-case-insensitive-search-in-vim
To change any session settings in vi, you use the
:set autoindent
:set shiftwidth=4
you can use the
http://docstore.mik.ua/orelly/unix/upt/ch30_27.htm
Compound searches
f you want to search for the first occurrence of a pattern or string where it follows another regardless of whether both patterns or strings exist on the same line, you can use a compound search by specifying both search commands separated by a semi-colon (
4!!awk '{print "New text",$0}' - not work
http://stackoverflow.com/questions/1527784/what-is-vim-recording-and-how-can-it-be-disabled
How To Record and Play
https://vi.stackexchange.com/questions/93/is-there-a-way-to-count-the-number-of-occurrences-of-a-word-in-a-file
http://stackoverflow.com/questions/22327705/vim-search-replace-meaning-of-s
http://stackoverflow.com/questions/9962201/what-is-a-buffer-in-vim
Basic
set nu - show line number
http://vimdoc.sourceforge.net/htmldoc/scroll.html
vi/vim delete commands and examples | vi vim delete lines to end | alvinalexander.com
Delete all blank lines
:g/^$/d
:g will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d (delete)
x - delete current character
10x
dw - delete current word
dd - delete current line
5dd - delete five lines
d$ - delete to end of line
d0 - delete to beginning of line
:1,.d
delete to beginning of file
:.,$d
delete to end of file
If all lines in the file are to be deleted, this vi command specifies the range of deletion:
:1,$d
vi -r filename is used to recover a file from .swp file
:e "file name"
Copy and paste text with vi or vim | a Tech-Recipes Tutorial
The command ‘Y’ or ‘yy’ copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively:
Y 2Y
10Y
yG
To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:
P p
It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:
yw y$
Lower case p pastes after the cursor position and upper case P pastes before.
Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any other commands prior to pasting as this will empty the buffer.
13 Vi Editor Interview Questions And Answers
/text: it will search for the string. after pressing enter it takes u to that text location.
How to enter from command mode to insertion mode using Vi Editor?
Ans: a,i
What is the difference between ZZ and :wq commands in Vi Editor?
ZZ is the command mode comand in uix to save and quit file.
:wq is the execute command mode command to save and quit file.
How to append a file to current file using Vi Editor?
:r file2
If you are working in file1 and want to append file2, than place the cursor where you want to append the new file and use the following command<
How to go 10 number line in command mode directly ?
:10
Which command is used to replace many characters in Vi Editor?
change command can be used to change a word/line.
cw change word forward
cb change word backward
c$ change from cursor to end of line
cL change from current line to and of screen
cG change from current line to and of file
or if you want to replace all occurence of some specific character
:s/oldText/newText/g
Basic vi Commands
To Exit vi
Moving the Cursor
Inserting or Adding Text
Changing Text
what are the different modes in vi editor?
Command mode
This is the default when you enter vi. In command mode, most letters, or short sequences of letters, that you type will be interpreted as command. If you press Esc when you're in command mode, your terminal will beep at you. This is a very good way to tell when you're in command mode.
Insert mode
In insert mode, whatever you type is inserted in the file at the cursor position. Type a (lowercase letter a, for append) to enter insert mode from command mode; press Esc
to end insert mode, and return to command mode.
Last Line mode
Use line mode to enter line oriented commands. To enter line mode from command mode, type a colon ( : ). Your cursor moves to the bottom of the screen, by a colon prompt. Type a line mode command, then press Enter. Any sensible command from the Unix line editor ex will work, and a few are good to know about. These commands are indicated in this handout by a colon in front of the command. Each time you use a line mode command, you must type a colon to enter line mode, then type the command by the colon prompt at the bottom of the screen, then press Enter when you finish typing the command. (The search commands starting with / and ? work similarly).
The traditional approach to find lines not matching a pattern is using the :v command:
:v/Warning/p
A neat trick when working with a large log file where you want to filter out as many irrelevant lines as possible before you get started on your real search is to save the file under a temporary name and delete all non-matching lines there:
:sav junk.log
:v/warning/d
You are now are editing a clone of your original file with all lines not matching "warning" removed and you can edit it at will.This will find everything but the regular expression you have specified. For example, if we want to find all the lines not containing the word 'foo', do:
/^\(\(.*foo.*\)\@!.\)*$
Here is an example: When examining a log file, you might only be interested in lines not containing the word "Warning", so the search command is:
/^\(.*Warning\)\@!.*$
To match all words except 'foo', use:
/\<\(foo\>\)\@!\k\+\>
\< Start-of-word
\(Foo\>\) The atom 'Foo' followed by end-of-word
\@! Match (with zero length) when the previous atom doesn't match.
\k\+ Match one or more Keywords
\> Match end-of-word.
The use of \@! can be very tricky. According to the Vim help files, it is often easier to use \@<! instead. For example, to find all 'bar' strings unless they are part of 'foobar', use the following (non-magic):
/\(foo\)\@<!bar
If we want to find a more complex regular expression on multiple lines, like all the lines which do not begin with 'foo' with 'bar' somewhere else and the word 'magic' at the end of the next line, do:
/^\(\(^foo.*bar.*\n.*magic$\)\@!.\)*$
https://stackoverflow.com/questions/2287440/how-to-do-case-insensitive-search-in-vim
As well as the suggestions for
\c
and ignorecase
, I find the smartcase
very useful. If you search for something containing uppercase characters, it will do a case sensitive search; if you search for something purely lowercase, it will do a case insensitive search. You can use \c
and \C
to override this::set ignorecase
:set smartcase
/copyright " Case insensitive
/Copyright " Case sensitive
/copyright\C " Case sensitive
/Copyright\c " Case insensitive
Worth noting that for smartcase
to work, you also need set ignorecas
:set ic
To go back to case-sensitive searches use:
:set noic
https://www.ibm.com/developerworks/aix/library/au-vitips.htmlTo change any session settings in vi, you use the
:set
command. To display a list of the options and settings, use the :set all
command.:set autoindent
:set shiftwidth=4
you can use the
:set ignorecase
command. Turn case sensitivity back on using :set noignorecase
. You can also use shorthand versions (:set ic
and :set noic
).http://docstore.mik.ua/orelly/unix/upt/ch30_27.htm
Compound searches
f you want to search for the first occurrence of a pattern or string where it follows another regardless of whether both patterns or strings exist on the same line, you can use a compound search by specifying both search commands separated by a semi-colon (
;
). For example, to search for the first occurrence of the string echo
where it follows the string {file}+1
, you would use /{file}+1/;/echo/
.
You can tell vi to place a bookmark at a point in a file by pressing the M key followed by another alphabetic character that denotes the bookmark reference. Therefore, you have up to 26 bookmarks named a to z. To return to the previous bookmark, press the back tick (
`
) followed by the bookmark reference alphabetic character.
For example, after pressing the MA keys, you would save the current cursor position into a bookmark named a. Whenever you want to return to that cursor position later in the editing session, you simply press the
`A
keys. To flip between the current bookmark and the previous one, you can use the double back tick (``
) command sequence.
You can switch the case of the alpha character underneath your cursor in vi the tilde key (
~
). Doing so shifts from lowercase to uppercase and vice versa. Holding down the key rolls over each character in the line, flipping the case of any alpha characters the editor comes across. You can enter a numeric character prior to the tilde to denote how many alpha characters you want to change.
- shift+`
You probably know that you can execute commands in a shell within vi by typing
:!command
, where command is the UNIX command that you want to execute—for example, :!pwd
to show the present working directory your editing session is in—and then pressing Enter.
However, you can also send a section of your file as the standard input to a UNIX command of your choice and have the same section in your editing buffer replaced by the resulting output. For example, if you wanted to sort the entire file while remaining in the vi session, you would type
Alternatively, you can prefix the shell command with the number of lines you want it to operate on from the current cursor. To do so type a numeric character specifying the number of lines followed by double exclamation marks (:1,$!sort
to instruct vi to pass lines 1 through the end of the file ($
) into the sort
command, replacing the specified section with the output, and then press Enter.!!
) followed by the UNIX command.4!!awk '{print "New text",$0}' - not work
http://stackoverflow.com/questions/1527784/what-is-vim-recording-and-how-can-it-be-disabled
You start recording by q<letter> and you can end it by typing q again.
Recording is a really useful feature of Vim.
It records everything you type. You can then replay it simply by typing @<letter>. Record search, movement, replacement...
http://www.thegeekstuff.com/2009/01/vi-and-vim-macro-tutorial-how-to-record-and-playHow To Record and Play
https://vi.stackexchange.com/questions/93/is-there-a-way-to-count-the-number-of-occurrences-of-a-word-in-a-file
Quincy's answer is fine, but there's an exact way to do this which doesn't require editing the buffer:
:%s/pattern//ng
This will print a message like
3 matches on 2 lines
, and no changes will be made to your buffer.
The
n
flag makes the :substitute
command print the number of matches instead of performing an actual substitution; the g
flag enables reporting of multiple matches per line.
Another thing that might be useful to your use case is to print all lines that match a pattern:
:global/pattern/print
which can be shortened to:
:g/pattern/p
This is one of the simplest uses of the
:global
command (which is mind-bogglingly powerful). It will simply print out all of the lines that match pattern
, and then you press Enter
or type another command to make it go away.
A bit of trivia: This command is the origin of the name
grep
, as it would commonly be described as g/re/p
, where re
stands for "regular expression".:%s/pattern//n
The n
flag in the end tells :s
command to report the number of matches and not actually substitute. Read :h :s_flags
for more details.http://stackoverflow.com/questions/22327705/vim-search-replace-meaning-of-s
%
is the range over which the :s
command (short for :substitute
) will be run. %
itself is short for the range :1,$
, which means Line 1 to the last line in the buffer
.http://stackoverflow.com/questions/9962201/what-is-a-buffer-in-vim
A buffer is the in-memory text of a file ... [which is] loaded into memory for editing. The original file remains unchanged until you write the buffer to the file.
That is, a buffer represents the actual loaded/working data itself.
set nu - show line number
http://vimdoc.sourceforge.net/htmldoc/scroll.html
CTRL-E Scroll window [count] lines downwards in the buffer. Mnemonic: Extra lines.
<PageDown> or *<PageDown>* *CTRL-F* CTRL-F Scroll window [count] pages Forwards (downwards) in the buffer.
CTRL-D Scroll window Downwards in the buffer. The number of lines comes from the 'scroll' option (default: half a screen).
<PageUp> or *<PageUp>* *CTRL-B* CTRL-B Scroll window [count] pages Backwards (upwards) in the buffer.
CTRL-U Scroll window Upwards in the buffer. The number of lines comes from the 'scroll' option (default: half a screen).
CTRL-Y Scroll window [count] lines upwards in the buffer.
6. Scrolling with a mouse wheel *scroll-mouse-wheel*
vi/vim delete commands and examples | vi vim delete lines to end | alvinalexander.com
Delete all blank lines
:g/^$/d
:g will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d (delete)
x - delete current character
10x
dw - delete current word
dd - delete current line
5dd - delete five lines
d$ - delete to end of line
d0 - delete to beginning of line
:1,.d
delete to beginning of file
:.,$d
delete to end of file
If all lines in the file are to be deleted, this vi command specifies the range of deletion:
:1,$d
vi -r filename is used to recover a file from .swp file
:e "file name"
Copy and paste text with vi or vim | a Tech-Recipes Tutorial
The command ‘Y’ or ‘yy’ copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively:
Y 2Y
10Y
yG
To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:
P p
It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:
yw y$
Lower case p pastes after the cursor position and upper case P pastes before.
Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any other commands prior to pasting as this will empty the buffer.
13 Vi Editor Interview Questions And Answers
/text: it will search for the string. after pressing enter it takes u to that text location.
How to enter from command mode to insertion mode using Vi Editor?
Ans: a,i
What is the difference between ZZ and :wq commands in Vi Editor?
ZZ is the command mode comand in uix to save and quit file.
:wq is the execute command mode command to save and quit file.
How to append a file to current file using Vi Editor?
:r file2
If you are working in file1 and want to append file2, than place the cursor where you want to append the new file and use the following command<
How to go 10 number line in command mode directly ?
:10
Which command is used to replace many characters in Vi Editor?
change command can be used to change a word/line.
cw change word forward
cb change word backward
c$ change from cursor to end of line
cL change from current line to and of screen
cG change from current line to and of file
or if you want to replace all occurence of some specific character
:s/oldText/newText/g
Basic vi Commands
To Exit vi
* | :x<Return> | quit vi, writing out modified file to file named in original invocation |
---|---|---|
:wq<Return> | quit vi, writing out modified file to file named in original invocation | |
:q<Return> | quit (or exit) vi | |
* | :q!<Return> | quit vi even though latest changes have not been saved for this vi call |
Moving the Cursor
* | j or <Return> [or down-arrow] | move cursor down one line |
---|---|---|
* | k [or up-arrow] | move cursor up one line |
* | h or <Backspace> [or left-arrow] | move cursor left one character |
* | l or <Space> [or right-arrow] | move cursor right one character |
* | 0 (zero) | move cursor to start of current line (the one with the cursor) |
* | $ | move cursor to end of current line |
w | move cursor to beginning of next word | |
b | move cursor back to beginning of preceding word | |
:0<Return> or 1G | move cursor to first line in file | |
:n<Return> or nG | move cursor to line n | |
:$<Return> or G | move cursor to last line in file | |
Inserting or Adding Text
* | i | insert text before cursor, until <Esc> hit |
---|---|---|
I | insert text at beginning of current line, until <Esc> hit | |
* | a | append text after cursor, until <Esc> hit |
A | append text to end of current line, until <Esc> hit | |
* | o | open and put text in a new line below current line, until <Esc> hit |
* | O | open and put text in a new line above current line, until <Esc> hit |
Changing Text
* | r | replace single character under cursor (no <Esc> needed) |
---|---|---|
R | replace characters, starting with current cursor position, until <Esc> hit | |
cw | change the current word with new text, starting with the character under cursor, until <Esc> hit | |
cNw | change N words beginning with character under cursor, until <Esc> hit; e.g., c5w changes 5 words | |
C | change (replace) the characters in the current line, until <Esc> hit | |
cc | change (replace) the entire current line, stopping when <Esc> is hit | |
Ncc or cNc | change (replace) the next N lines, starting with the current line, stopping when <Esc> is hit | |
Saving and Reading Files
:r filename<Return> | read file named filename and insert after current line (the line with cursor) | |
---|---|---|
:w<Return> | write current contents to file named in original vi call | |
:w newfile<Return> | write current contents to a new file named newfile | |
:12,35w smallfile<Return> | write the contents of the lines numbered 12 through 35 to a new file named smallfile | |
:w! prevfile<Return> | write current contents over a pre-existing file named prevfile |
what are the different modes in vi editor?
Command mode
This is the default when you enter vi. In command mode, most letters, or short sequences of letters, that you type will be interpreted as command. If you press Esc when you're in command mode, your terminal will beep at you. This is a very good way to tell when you're in command mode.
Insert mode
In insert mode, whatever you type is inserted in the file at the cursor position. Type a (lowercase letter a, for append) to enter insert mode from command mode; press Esc
to end insert mode, and return to command mode.
Last Line mode
Use line mode to enter line oriented commands. To enter line mode from command mode, type a colon ( : ). Your cursor moves to the bottom of the screen, by a colon prompt. Type a line mode command, then press Enter. Any sensible command from the Unix line editor ex will work, and a few are good to know about. These commands are indicated in this handout by a colon in front of the command. Each time you use a line mode command, you must type a colon to enter line mode, then type the command by the colon prompt at the bottom of the screen, then press Enter when you finish typing the command. (The search commands starting with / and ? work similarly).