How this works
just write the group number or string like :
g/(d{1,4})/
In Parenthesis () define the group
d is for digits
In brackers {1,4} are the range of numbers from 1 to 4
After that just escape all except the search pattern :
g/\(\d\{1,4\}\)/
Now you can search and edit the surrounded enviroment
%s/\(\d\{1,4\}\)/some Edit \1 around the pattern/g
Explanation:
Search numbers of fixed length say 5
/\d\{5\}
match 12345, 123456
As numbers more than 5 digits contain substring of 5 digits, they will
be found too.
word boundary start
\<
word boundary end
\>
Then use below to search numbers of exact 5 digits
/\<\d\{5\}\>
match 12345 but not 123456
Use below to search numbers of 5 or more digits.
/\<\d\{5,\}\>
Use below to search numbers of 5 to 8 digits.
/\<\d\{5,8\}\>
Use below to search numbers of 8 or less digits.
/\<\d\{,8\}\>
Shortcut numbers of 1 or more digits
/\d\+
How to generate a number sequence in file using vi or Vim?¶
Starting with Vim 7.4.754 one can use gCtrl-a, see :help v_g_CTRL-A
Go to line #4, use Ctrl-v to blockwise select the first character, go down 4 lines, press Shifti, enter0 (this is 0, followed by Space) and Esc to exit insert mode.
Now use gv to re-select the previously selected area. Press gCtrl-a to create a sequence.
I start with a 0 here, so I can re-select by gv. If you start with a 1, you need to re-select by hand while omitting the first 1.
Use 2gCtrl-a to use a step count of 2.
Sequencial Pattern
Did you use a plugin or some trick to get the text of each key press to show up when you made that animated gif? – slm
Tool is called screenkey and can be found on gitlab. – rkta
sudo apt install screenkey
Capitalize first letter of each word in a selection using Vim¶
You can use the following substitution:
s/\<./\u&/g
\< matches the start of a word
. matches the first character of a word
\u tells Vim to uppercase the following character in the substitution string (&)
& means substitute whatever was matched on the left-hand side
g means substitute all matches, not only the first
How to search for one of two similar strings in Vim?¶
/planet\(Awesome\|Terrible\)
/\<planet\(Awesome\|Terrible\)\>
\< marks the beginning of a ‘word’ (essentially alphanumerics)
\> marks the end of a ‘word’ (essentially alphanumerics)
Replace all characters in a regex match with the same character in Vim¶
Suppose you opened a file in Vim. Now you want to split the workspace
into multiple windows for better productivity. Let’s go over how to
create a split window in Vim.
There are two ways you can split a Vim workspace - horizontally and/or
vertically.
You can know which window is active based on which window has your
cursor.
If you specify a file path, it will open the file in the newly split
window, otherwise, the newly split window will open the same file.
A shorter command to vsplit is vs (you can specify a file path
with vs as well).
As an alternative way to create a vertical split, you can press the
Ctrl + w key combination, and lastly, press the letter ” v ” (v
for vertical split).
By default, Vim creates splits with a similar width/height. It is good
for my OCD, but not really productive when I have a file that I edit
most of the time and another file that I rarely edit.
So, let’s go over how you can resize the split windows in Vim.
To resize a window, use either of the following methods:
Press Ctrl + w key combination [optionally specify a number] and
then press the ” + ” (plus) symbol to increase the height of
the current window
Press Ctrl + w key combination [optionally specify a number] and
then press the ” - ” (minus) symbol to reduce the height of
current window
Press Ctrl + w key combination [optionally specify a number] and
then press the ” < ” (greater than) symbol to reduce the
width __ of the current window
Press Ctrl + w key combination [optionally specify a number] and
then press the ” > ” (less than) symbol to increase the width
of the current window
Below are the key combinations that you can press to expand a vertically
split window vertically or a horizontally split window horizontally.
Expand vertically - press Ctrl + w and then press the pipe ”
| ” character (the character which gets typed when you press the
backslash key while holding down Shift)
Expand horizontally - press Ctrl + w and then press ” **_** “
This will open the same file in newly split window. To open another file
in new window, use either :e or :open command followed by the
file name to make the window split actually usable.
This is not actually a tip, but when you copy or cut something from a
split window and move to another window and try to paste, it will work.
No additional configuration is necessary.
If you look closely at navigating between active windows, it is similar
to moving your cursor’s direction. Except, there is a need to press
“Ctrl + w” every time and then press either h,j,k,l keys.
Below is what I personally use. Instead of pressing “Ctrl + w” and then
pressing h,j,k,l keys, you can simply press “Ctrl + [h,j,k,l]”.
If you want to use these key combinations, add the following keys to
your ‘vimrc’:
By default, when you create a vertically split window, it will open to
the left. To change this default behavior, add the following line to
your ‘vimrc’.
Similarly, if you create a new horizontally split window, it will open
on the topmost portion of the Vim workspace. To make a new horizontally
split window open on the bottom of current window, add the following
line to your ‘vimrc’.
This article covers how you can create one or more horizontal or
vertical splits in an active Vim workspace. We also go over how you can
resize split windows, close an active split window and how to navigate
between open windows.
Since it is not allowed to affect the buffer list with a :bufdo-argument command (see :help :bufdo), we have to use a more wordy yet fairly straightforward Vim script.
The function below enumerates all existing buffer numbers and deletes those that do not have a name (displayed as [No Name] in the interface) nor any unsaved changes. The latter condition is guaranteed through the invocation of the :bdelete command without the trailing ! sign, in which case modified buffers are skipped.
1 2 3 4 5 6 7 8 9101112
function! DeleteEmptyBuffers()
let [i, n; empty] = [1, bufnr('$')]
while i <= n
if bufexists(i) && bufname(i) == ''
call add(empty, i)
endif
let i += 1
endwhile
if len(empty) > 0
exe 'bdelete' join(empty)
endif
endfunction
If you would like to delete empty buffers completely, including the unloaded ones, consider (with care!) replacing the exe ‘bdelete’ with exe ‘bwipeout’ (see :help :bd, :help :bw).
To test the contents of a buffer, use the getbufline() function. For instance, to be absolutely sure that the buffer contains no text in it, modify the if statement inside the while loop as follows:
Note that bufexists() is changed to bufloaded() here. It is necessary because it is possible to get the contents only of those buffers that are loaded; for unloaded buffers getbufline() returns an empty list regardless of their contents.
Vim has built-in sorting command which we can use to sort text
, also vim supports invoking external command ,thus we also can
use system sort to do so, let’s see some examples.
How to sort in vim alphabetically
:sort
How to sort selected lines in vim
First we can enable line number using command :
:set number
Then to sort selected lines by specifying line number
:2,10 sort
This will sort 2nd line to 10th line.
How to sort descending in vim
:sort!
Note : there is no space between sort and !
How to do unique sort in vim
:sort u
How to sort in vim case-intensive
:sort i
Numeric sort in vim
:sort n
How to sort by column in vim
:%!sort -k2
This uses external system sort utility to sort all lines by the
2nd column , by default the field-separator is space.
To sort by column using specific field separator:
:%!sort -t ':' -k3
Here use : as field separator to sort by column number 3.
In Vim, you can find and replace text using the :substitute (:s) command.
To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the ‘Esc’ key.
The general form of the substitute command is as follows:
:[range]s/{pattern}/{string}/[flags][count]
The command searches each line in [range] for a {pattern}, and replaces it with a {string}. [count] is a positive integer that multiplies the command.
If no [range] and [count] are given, only the pattern found in the current line is replaced. The current line is the line where the cursor is placed.
For example, to search for the first occurrence of the string ‘foo’ in the current line and replace it with ‘bar’, you would use:
:s/foo/bar/
To replace all occurrences of the search pattern in the current line, add the g flag:
:s/foo/bar/g
If you want to search and replace the pattern in the entire file, use the percentage character % as a range. This character indicates a range from the first to the last line of the file:
:%s/foo/bar/g
If the {string} part is omitted, it is considered as an empty string, and the matched pattern is deleted. The following command deletes all instances of the string ‘foo’ in the current line:
:s/foo//g
Instead of the slash character (/), you can use any other non-alphanumeric single-byte character except as a delimiter. This option is useful when you have the ‘/’ character in the search pattern or the replacement string.
:s|foo|bar|
To confirm each substitution, use the c flag:
:s/foo/bar/gc
replace with bar (y/n/a/q/l/^E/^Y)?
Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y, and to scroll up, use CTRL+E.
You can also use regular expressions as a search pattern. The command bellow replaces all lines starting with ‘foo’ with ‘Vim is the best’:
:%s/^foo.*/Vimisthebest/gc
The ^ (caret) symbol matches the beginning of a line and .* matches any number of any characters.
When no range is specified the substitute command operates only in the current line.
The range can be either one line or a range between two lines. The line specifiers are separated with the , or ; characters. The range can be specified using the absolute line number or special symbols.
For example, to substitute all occurrences of ‘foo’ to ‘bar’ in all lines starting from line 3 to line 10 you would use:
:3,10s/foo/bar/g
The range is inclusive, which means that the first and last lines are included in the range.
The dot . character indicates the current line and $ - the dollar sign the last line. To substitute ‘foo’ in all lines starting from the current line to the last one:
:.,$s/foo/bar/
The line specifier can also be set using the ‘+’ or ‘-’ symbol,followed by a number that is added or subtracted from the preceding line number. If the number after the symbol is omitted, it defaults to 1.
For example to substitute each ‘foo’ with ‘bar’ starting from the current line and the four next lines, type:
The substitute command looks for the pattern as a string, not a whole word. If, for example, you were searching for “gnu”, the search find matches where “gnu” is embedded in larger words, such as “cygnus” or “magnum”.
To search for a whole word, type \< to mark the beginning of a word, enter the search pattern, type \> to mark the end of a word:
For example, to search for the word “foo” you would use \<foo\>:
Vim keeps track of all the commands you run in the current session. To browse the history for previous substitute commands, enter :s and use the arrow up/down keys to find a previous substitute operation. To run the command, simply press Enter. You can also edit the command before performing the operation.