reed book: CentOS System Administration Essentials

CentOS System Administration Essentials

CentOS System Administration Essentials

You may have some experience with vi, or what is now known as Vim (which is
when simply put—vi improved). All too often, I find that those first experiences
have never been good ones or to be looked back upon with much fondness. Guiding
you through the initially unfathomable regime of vi, we are going to make sure that
you are the master of vi and you leave wanting to use this tool from the gods. vi is
like everything else in the sense that you just need to stick with it in the early days
and keep practicing. Remember how you persevered for many hours riding your
bicycle as a toddler and became a master, despite a few bruised knees? I want you
to persevere with vi too. We will start with a little command-line magic to make the
whole command-line interface (CLI) experience a better one. We will then be ready
to start our black-belt experience in vi.
In this chapter, we will go through the following topics:
• CLI trickery – shortcuts that you will love
• Vim and vi: In this section, you will learn to differentiate between these
twins and meet their graphical cousin
• Getting the .vimrc setup the way you like
• Search and replace: In this section, you will learn how to quickly find and
replace text within files from both inside and outside Vim
• Learning to remove extraneous comments from a file with a few deft
key strokes
Taming vi
[ 10 ]
CLI trickery – shortcuts that you will love
So before we dice into the wonderful world of text editing that is vi, we will warm
up with a few exercises on the keyboard. Linux is my passion, as is automation. I am
always keen to create scripts to carry out tasks so that those tasks become repeatedly
correct. Once the script is created and tested, we will have the knowledge and faith
that it will run in the same way every time and we will not make mistakes or miss
critical steps, either because it gets boring or we are working late on a Friday night
and just want to go home. Scripting itself is just knowing the command line well
and being able to use it at its best. This truth remains across all systems that you will
work with.
On the command line, we may try a little more black magic by executing the
following command:
$ cd dir1 || mkdir dir1 && cd dir1
With this, we have used the cd command to enter the dir1 directory. The double
pipe or vertical bar indicates that we will attempt the next command only if the first
command fails. This means that if we fail to switch to the dir1 directory, we will run
the mkdir dir1 command to create it. If the directory creation succeeds, we then
change into that directory.
The || part denotes that the second command will run only
on the failure of the first. The && part denotes that the second
command will run only if the first command succeeds.
The command history is a little more and hugely better than just an up arrow key!
Consider the following commands:
$ mkdir dir1
$ cd !$
The !$ part represents the last argument, so in this way, the second line evaluates to
the following:
$ cd dir1
In this way, we can rewrite the initial command sequence, by combining both
concepts, to create the following command:
$ cd dir1 || mkdir !$ && cd !$
Chapter 1
[ 11 ]
We can repeat the last command as well as the last argument. More importantly,
we can specify the start characters for the last command. If it was merely the last
command, then the up arrow key would suffice. If we were working on a web server
configuration, we may want to edit the configuration file with vi, start the service,
and then test with a command-line browser. We can represent these tasks using the
following three commands:
# vi /etc/httpd/conf/httpd.conf
# service httpd restart
w3m localhost
Having run these three commands in the correct order, hoping for success,
we may notice that we still have issues and that we need to start re-editing
the configuration file for Apache, the web server. We can now abbreviate the
command list to the following:
# !v
# !s
# !w
The !v command will rerun the last command in my history that begins with a v,
and likewise with s and w. This way, we can appear to be terribly proficient and
working really quickly, thus gaining more time to do what really interests us,
perhaps a short 9 holes?
In a similar fashion to our first glance at the history using the !$ symbols to represent
the last argument, we can use !?73. This would look for 73 anywhere as an argument
or part of an argument. With my current history, this would relate to the date
command we ran earlier. Let's take a look:
$ !?73
With my history, the sequence will expand to and run the following command:
$ date --date "73 days ago"
Looking at my command history from the last command run to the first, we search
for 73 anywhere as a command argument. We make a note that we exclusively look
for 73, meaning we are looking for the character 7 followed by the character 3. We

have to then bear in mind that we would also match 273 or 733 if they exis

No comments:

Post a Comment

Copyright © reed book Urang-kurai