Important commands/ways for VIM Editor every DevOps Engineer Should Know
Important commands/ways for VIM Editor every DevOps Engineer Should Know
Vim is a powerful and highly configurable text editor that is included on most Linux and Unix-based systems. It is known for its efficiency and ability to work entirely within the command line. However, mastering Vim can be challenging for beginners. In this blog, we will discuss some of the essential Vim commands that will help you become more productive and efficient in your workflow.
Opening and Saving Files
To open a file in Vim, simply type the following command:
code
$ vim filename.txt
This will open the file in Vim for editing. To save changes made to the file, you can use the :w command:
code
:w
This command saves the changes made to the file. If you want to save the file with a different name or in a different location, you can use the :w filename.txt command:
code
:w filename.txt
Exiting Vim
To exit Vim, you can use the :q command:
code
:q
This will quit Vim. However, if you have made changes to the file, you will be prompted to save the changes before quitting. If you want to force quit Vim without saving changes, you can use the :q! command:
code
:q!
If you have made changes to the file and want to save them before quitting, you can use the :wq command:
code
:wq
This saves the changes and exits Vim.
Navigation
Vim provides several commands for navigation. Here are some of the essential ones:
h: Move the cursor one character to the left.
j: Move the cursor one line down.
k: Move the cursor one line up.
l: Move the cursor one character to the right.
0: Move the cursor to the beginning of the line.
$: Move the cursor to the end of the line.
gg: Move the cursor to the beginning of the file.
G: Move the cursor to the end of the file.
Ctrl + d: Move the cursor half a page down.
Ctrl + u: Move the cursor half a page up.
Editing
Vim provides several commands for editing text. Here are some of the essential ones:
i: Enter insert mode.
Esc: Exit insert mode.
x: Delete the character under the cursor.
dd: Delete the entire line.
yy: Copy the entire line.
p: Paste the copied text.
u: Undo the last command.
Ctrl + r: Redo the last command.
:s/search/replace/g: Search and replace all occurrences of search with replace.
Miscellaneous
Here are some additional Vim commands that can be useful:
:set number: Display line numbers.
:set paste: Enter paste mode.
:set nopaste: Exit paste mode.
:set tabstop=4: Set the tabstop to 4 spaces.
:set expandtab: Use spaces instead of tabs.
:set autoindent: Automatically indent new lines.
:set smartindent: Automatically indent new lines based on the context.
Here are some additional Vim commands that can be useful:
:set number: Display line numbers.
:set paste: Enter paste mode.
:set nopaste: Exit paste mode.
:set tabstop=4: Set the tabstop to 4 spaces.
:set expandtab: Use spaces instead of tabs.
:set autoindent: Automatically indent new lines.
:set smartindent: Automatically indent new lines based on the context.
How to extract domain name from url?
echo http://example.com/index.php | awk -F[/:] '{print $4}'
OUTPUT: example.com
OUTPUT: example.com
How to delete blank lines in Vim Editor?
Code
:g/^$/d
:g will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d (delete)
:g will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d (delete)
How to remove duplicate rows in VIM Editor?
From command line just do:
sort file1 | uniq > file2.new
sort file1 | uniq > file2.new
file1: Source file
file2: Destination file
No comments