Install xsel -
$ sudo apt install xsel
Run
$ xsel -bc
Install xsel -
$ sudo apt install xsel
Run
$ xsel -bc
The ex command g
is very useful for acting on lines that match a pattern. You can use it with the d
command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.
For example, to delete all lines containing "profile" (remove the /d
to show the lines that the command will delete):
:g/profile/d
More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:
:g/^\s*$/d
To delete all lines that do not contain a pattern, use g!
, like this command to delete all lines that are not comment lines in a Vim script:
:g!/^\s*"/d
Note that g!
is equivalent to v
, so you could also do the above with:
:v/^\s*"/d
The next example shows use of \|
("or") to delete all lines except those that contain "error
" or "warn
" or "fail
" (:help pattern):
:v/error\|warn\|fail/d
How to Remove all large files by a file extension name e.g. .mp4 or by file size from Every Commit in git?
How to Delete Lines in Vim / Vi
https://linuxize.com/post/vim-delete-line/
Deleting a Line
The command to delete a line in Vim is dd .
Below are step-by-step instructions to delete a line:
Press the Esc key to go to normal mode.
Place the cursor on the line you want to delete.
Type dd and hit Enter to remove the line.
Pressing dd multiple times will delete multiple lines.
Deleting Multiple Lines
To delete multiple lines at once, prepend the dd command with the number of lines to be deleted. For example, to delete five lines you would do the following:
Press the Esc key to go to normal mode.
Place the cursor on the first line you want to delete.
Type 5dd and hit Enter to delete the next five lines.
Delete a range of lines
The syntax for deleting a range of lines is as follows:
:[start],[end]d
For example, to delete lines starting from 3 to 5 you would do the following:
Press the Esc key to go to normal mode.
Type :3,5d and hit Enter to delete the lines.
You can also use the following characters to specify the range:
. (dot) - The current line.
$ - The last line.
% - All lines.
Here are a few examples:
:.,$d - From the current line to the end of the file.
:.,1d - From the current line to the beginning of the file.
:10,$d - From the 10th line to the end of the file.
Delete All Lines
To delete all line you can use either the % symbol that represents all lines or the 1,$ range:
Press the Esc key to go to normal mode.
Type :%d and hit Enter to delete all the lines.
Deleting Lines Containing a Pattern
The syntax for deleting multiple lines based on a specific pattern is as follows:
:g/<pattern>/d
The global command (g) tells the delete command (d) to delete all lines containing the <pattern>.
To match the lines not matching the pattern, add an exclamation mark (!) before the pattern:
:g!/<pattern>/d
The pattern can be a literal match or regular expression . Below are some examples:
:g/foo/d - Delete all lines containing the string “foo”. It also removes line where “foo” is embedded in larger words, such as “football”.
:g!/foo/d - Delete all lines not containing the string “foo”.
:g/^#/d - Remove all comments from a Bash script. The pattern ^# means each line beginning with #.
:g/^$/d - Remove all blank lines. The pattern ^$ matches all empty lines.
:g/^\s*$/d - Remove all blank lines. Unlike the previous command, this also removes the blank lines that have zero or more whitespace characters (\s*).
alias rm='mv -t /root/MyTrash/'
-t
means -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORYupdate
.bashrc
file by running this command source .bashrc
rm
command that file will be moved to /root/MyTrash
directory