Vim is a powerful text
editor used in CLI (command line interface). Linux uses a lot of configuration
files, you'll often need to edit them and vim is a great tool to do so. The
question that may be asked is : is there an easier way to edit text files in
cli? Absolutely yes , there are many other editors like nano or joe, but the
fact is that, in numerous utilities , the configuration files are merely based
on vim. Some Linux Administrators prefer not to use vim because of numerous
commands that are available In this short tutorial I am going to make it easy
for you to work with vim.
Editing shortcuts in command mode
Modes in vim
and how to Switch between them
Vim has a particular
working method, there are two main modes: the command mode and the insert mode.
when you enter vim , the command mode is enabled and in order to switch to
insert mode in which you can add text to your document, you have quite a few
option: "i"
(insert text where the cursor is) or "o" (insert text at the beginning of
the following line) there is also “a” (insert text one character form
cursor’s current state) ,when you enter “insert” mode -- INSERT – will appear
on the bottom left side of your screen .
Once you are done adding text to the document,
you can easily press Esc to jump back to command mode. Now in command
mode there are few shortcuts that can make your life much easier to work with
text.
Editing shortcuts in command mode
dd:it can act as delete or cut , when you press d
twice in command mode all of the current line will move to buffer and it awaits
your next command.
yy:this command will copy the text to buffer and
prepare it to be pasted by the next command.
p:this is actually the paste command which
pastes the buffer.
V:one fun part of command mode is v command which stands for visual and may come in
handy when you intend to highlight lines or words in your document.just press v to start visual and select lines with the help of arrow keys. after selecting the text in this mode you may copy, cut, paste text with y , d , p
just remember that with d you can either cut or delete selected text.
Management commands
Management commands
Few times during my work , I notice that I have mistakenly deleted a line or have the last commands undone . in this case we can use shortcuts to undo and redo our last command .because if you are working on a configuration file , even a tiny mistake may end up failing a service.
now let’s get started with “u” which stands for undo , next one is Ctrl+r(^r) for redo but whenever you realize that you have no idea where your error have been committed , you can easily get out of the vim without saving the changes with :q! which means quit and do not complain about anything. It was simple ,wasn’t it?!
now let’s get started with “u” which stands for undo , next one is Ctrl+r(^r) for redo but whenever you realize that you have no idea where your error have been committed , you can easily get out of the vim without saving the changes with :q! which means quit and do not complain about anything. It was simple ,wasn’t it?!
Vim has so many commands which seem difficult to remember and it makes no sense to try to memorize them just start with fundamentals and you will see it is not that hard, moreover command mode in vim can be very interesting and fast in finding and replacing words consider following command:
:%s/old-text/new-text/g
:%s/old-text/new-text/g
%s – specifies all lines. Specifying the range as ‘%’ means do substitution in the entire file.
g – specifies all occurrences in the line. With the ‘g’ flag , you can make the whole line to be substituted. If this ‘g’ flag is not used then only first occurrence in the line only will be substituted.
note that if instead of %s you use s ,the command will only applies to the current line and not on the entire text file.



Comments
Post a Comment