vi /vim-related Linux commands Note: To use vim tool/ Editor , we must install, command: sudo apt-get install vim
vi file_name1.txt ==> to open file & (press insert/i) to do modifications in that file, press (Esc)then type(wq! ) to save the file with modifications.
vi <filename> +13 To go for aparticular line(13 / 72) while opening the file with vi editor
vi *105* (if you don't know the file name exactly but if you remember your the file name half, then you can open that file using vi *half file name*))
If you want to move all the files use the * symbol (* means all)
Ex: If you move all files to another directory /destination path.
mv <space> * <space> Destination_path
or
mv <space> Source_path/* <space> Destination_path
Esc :set nu or Esc :set number ==> command will set line numbers for your code
Esc :set nonu or Esc :set nonumber ==> command will remove line numbers for your code
<Number>yy ==> To copy Number of lines in file
Example: yy ==> To copy single line in file
4yy ==> To copy 4 line in file
p ==> to paste copied lines at once
3p ==> to paste copied lines 3 times
u ==> undo (undo any number of previous actions)
<Number>dd ==> To delete Number of lines in file
Example: dd ==> To delete single line (where your cursor is present)
3dd ==> To delete 3 lines from the cursor position
To delete from specific line to specific line in a file with out opening it.
command : sed <space> -i <space> 'starting line number, ending line numberd' <space> file_name
d means delete
Example : sed -i '567,670d' test_Cfm3Util_5.py
/(forward slash) used to search particular words or data in the file (in vi editors).
/<word> *** make sure that word is case sensitive ***
after that lower case n search downwards from the current cursor position
after that Upper case N search downwards from the current cursor position
<Number> Shift + < ==> To move Number of lines towards left hand side in a file
Example: Shift + < ==> To move single line towards left hand side in the file
4 Shift + < ==> To move Number of lines towards left hand side in the file
<Number> Shift + > ==> To move Number of lines towards right hand side in a file
Example: Shift + > ==> To move single line towards right hand side in the file
4 Shift + > ==> To move Number of lines towards right-hand side of the file
Esc: set paste then paste your content by using p (to paste precisely how the user copied content with indentation)
vi /vim-related Linux commands
Note: To use vim tool/ Editor , we must install, command: sudo apt-get install vim
vi file_name1.txt ==> to open file & (press insert/i) to do modifications in that file, press (Esc)then type(wq! ) to save the file with modifications.
vi <filename> +13 To go for aparticular line(13 / 72) while opening the file with vi editor
vi *105*
(if you don't know the file name exactly but if you remember your the file name half, then you can open that file using vi *half file name*))
If you want to move all the files use the * symbol (* means all)
Ex: If you move all files to another directory /destination path.
mv <space> * <space> Destination_path
or
mv <space> Source_path/* <space> Destination_path
Esc :set nu or Esc :set number ==> command will set line numbers for your code
Esc :set nonu or Esc :set nonumber ==> command will remove line numbers for your code
<Number>yy ==> To copy Number of lines in file
Example: yy ==> To copy single line in file
4yy ==> To copy 4 line in file
p ==> to paste copied lines at once
3p ==> to paste copied lines 3 times
u ==> undo (undo any number of previous actions)
<Number>dd ==> To delete Number of lines in file
Example: dd ==> To delete single line (where your cursor is present)
3dd ==> To delete 3 lines from the cursor position
To delete from specific line to specific line in a file with out opening it.
command : sed <space> -i <space> 'starting line number, ending line numberd' <space> file_name
d means delete
Example : sed -i '567,670d' test_Cfm3Util_5.py
/(forward slash) used to search particular words or data in the file (in vi editors).
/<word> *** make sure that word is case sensitive ***
after that lower case n search downwards from the current cursor position
after that Upper case N search downwards from the current cursor position
<Number> Shift + < ==> To move Number of lines towards left hand side in a file
Example: Shift + < ==> To move single line towards left hand side in the file
4 Shift + < ==> To move Number of lines towards left hand side in the file
<Number> Shift + > ==> To move Number of lines towards right hand side in a file
Example: Shift + > ==> To move single line towards right hand side in the file
4 Shift + > ==> To move Number of lines towards right-hand side of the file
Esc: set paste then paste your content by using p (to paste precisely how the user copied content with indentation)
If you want to move all the files use the * symbol (* means all)
Ex: If you move all files to another directory /destination path.
mv <space> * <space> Destination_path
or
mv <space> Source_path/* <space> Destination_path
Esc :set nu or Esc :set number ==> command will set line numbers for your code
Esc :set nonu or Esc :set nonumber ==> command will remove line numbers for your code
<Number>yy ==> To copy Number of lines in file
Example: yy ==> To copy single line in file
4yy ==> To copy 4 line in file
p ==> to paste copied lines at once
3p ==> to paste copied lines 3 times
u ==> undo (undo any number of previous actions)
<Number>dd ==> To delete Number of lines in file
Example: dd ==> To delete single line (where your cursor is present)
3dd ==> To delete 3 lines from the cursor position
To delete from specific line to specific line in a file with out opening it.
command : sed <space> -i <space> 'starting line number, ending line numberd' <space> file_name
d means delete
Example : sed -i '567,670d' test_Cfm3Util_5.py
/(forward slash) used to search particular words or data in the file (in vi editors).
/<word> *** make sure that word is case sensitive ***
after that lower case n search downwards from the current cursor position
after that Upper case N search downwards from the current cursor position
<Number> Shift + < ==> To move Number of lines towards left hand side in a file
Example: Shift + < ==> To move single line towards left hand side in the file
4 Shift + < ==> To move Number of lines towards left hand side in the file
<Number> Shift + > ==> To move Number of lines towards right hand side in a file
Example: Shift + > ==> To move single line towards right hand side in the file
4 Shift + > ==> To move Number of lines towards right-hand side of the file
Esc: set paste then paste your content by using p (to paste precisely how the user copied content with indentation)
How to delete lines in vi editor or vim editor in linux
How to delete lines in vi editor or vim editor in linux
Delete a single line in vi editor
The command to perform the deletion is dd, which is pressing the letter ‘d’ in quick succession twice. It is just like double-clicking.
- Go to the line that you want to delete in the editor
- Press the ESC key. (This is to make sure that you are in the command mode)
- Press dd to delete the line
Delete multiple lines in vi editor
If you know how many lines you want to delete ahead of time, then deleting multiple lines is just as easy as deleting a single line. You will prefix the dd command with the number of lines you want to remove.
- Go to the first line that you want to delete
- Press the ESC key
- Press the number of lines you want to delete, followed by dd (eg. 4dd or 25dd)
The lines that you want to remove should be in succession or consecutive.
Delete lines in a range in vi editor
You can delete lines with in a range, if you know the start and end line numbers. This is a similar case to the one we mentioned in the previous section. The general syntax for deleting a range of lines is:
:[start number],[end number]d
So, to delete all lines between the lines number 111 and 234, you will
- Hit ESC key
- type :111,234d
- Hit Enter
Delete all lines in vi editor
You can use the same syntax to delete all lines in the file.
There are other ways to empty or truncate a file if you want to, but if you are already in the editor, then this will work just as well.
- Hit ESC key
- type :1,$d
- Hit Enter
The $ character denotes the last line in the file. So the above command will delete lines starting with the first line and ending with the last line.
delete all lines before the current line
Another variation of the range is to delete the lines before the cursor or the current line in the editor. The dot (.) character denotes the current line in the editor.
- Hit ESC key
- type :1,.-1d
- Hit Enter
The above command will remove all lines starting with the first line till the line before the current line.
Delete all lines after the current line
To delete lines after the current line, you can use the dot (.) to denote the current line and dollar ($) to denote the last line.
- Hit ESC key
- type :.+1,$d
- Hit Enter
Delete lines that match a pattern in vi editor
You can also do a search and delete, if you want to delete lines that contain certain words or character sequences. So, if you want to delete all lines in the file that contain the word ‘saloon‘, then
- Hit ESC key
- type :g/saloon/d
- Hit Enter
Delete lines that do not match the pattern
If you want to delete that do not match a specific pattern, then you will need to negate the match before deleting. So, to delete lines that do not have the word ‘saloon’, you will
- Hit ESC key
- type :v/saloon/d
- Hit Enter
Delete all blank lines in a file
To delete all blank lines in a file, you can use a variation of the pattern matching.
- Hit ESC key
- type :g/^$/d
- Hit Enter
Pattern matching in vi editor is a very powerful tool. If you make yourself familiar with the pattern matching commands in vi, then you can use it for all the various different text manipulation that you will come across.
Request: If you find this information useful, please provide your valuable comments.