Skip to main content

Vim Editor

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.
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

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?!
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 – 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

Popular posts from this blog

Self-Monitoring Script with Python

Just last night, I faced a curious problem with one of my servers. It seemed to have the network interface up and running but I could not reach to the server with ssh, when I wanted to connect, the putty hanged on the username prompt , the server first closed port 80 and then the mysqld port and finally it closed ssh port that left me with no choice but to restart the server. We are not intending to address an optimization scenario, though the closure of httpd and mysqld port is related to an optimization issue which has haunted me several times.in this scenario, I am going to write and implement an internal monitoring and self-healing mechanism on my server with the help of python and crontab. Which port is closed? Netstat is the utility which comes in handy when we are to know about open/closed ports and we may pipe the output of the netstat to grep in order to filter the output, let’s see it in action # netstat -ntulp|grep ':::80' The command will look for port ...

Configuring Multipath on Centos7

Device Mapper Multipathing (DM-Multipath) is a native multipathing in Linux, Device Mapper Multipathing (DM-Multipath) can be used for Redundancy and to Improve the Performance. It aggregates or combines the multiple I/O paths between Servers and Storage, so it creates a single device at the OS Level. Typically, the storage area network (SAN) topology is set up in a redundant way. That means that the connection your server has to the storage will survive a failure of a controller, disk, network connection, or anything on the SAN. It also means that if you’re connecting to the SAN over multiple connections, the logical unit numbers (LUNs) on the SAN will be presented multiple times. If there are four different paths to your LUNs, on the connected node, you’ll see /dev/sda, /dev/sdb, and /dev/sdc, as well as /dev/sdd, all referring to the same device. As all of the /dev/sd devices are bound to a specific path, you shouldn’t connect to either of them. If the specific path you’re ...

Setting Up ISCSI Target & Initiator

ISCSI stands for  Internet Small Computer Systems Interface, which is an IP-based storage,and works on top of internet protocol by carrying SCSI commands over IP network. iSCSI transports block-level data between an iSCSI initiator(on the Client machine) and an iSCSI target on a storage device (server). Before getting into the Configuration of an iscsi target and initiator, let’s talk a little bit about iscsi Terminology as your acquaintance with the definitions will light up the path for you to understand the subject profoundly. Iscsi Terminology iscsi Target : you already know about iscsi target , it is actually the server that share the block device and to which you log in . the disk is configured as an iscsi target through targetcli utility and becomes available to the clients. iscsi Initiator : the shared device is requested with initiator which resides on the client , the initiator itself is installed through iscsi-initiator-utils on the client machine. I...