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.
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.
Assume that you have opened a file in Vim, and you want to split the
screen vertically.
To make a vertical split, enter Normal mode, and run the following
command:
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.