Syntax highlighting is my top annoyance in using vi/vim. Syntax highlighting is just a fancy term meaning that the text editor will auto-color parts of a text file according to some rules that makes sense to it, using some default color scheme.
To be precise, only vim, not vi, has syntax highlighting. vi has a 2 color scheme only: background and foreground. Yet, on my Centos 4 system (and many other distros), the vi command is just a soft link to vim.
Syntax highlighting is useful, and usually nothing to complain about. However, I find the default vim color scheme to be an eye-killer for me.
If you are already in vi/m, you can disable it by typing
:syntax off (and press the return key).
To re-enable coloring, type
:syntax on (and press the return key).
If you want to permanently disable syntax highlighting, insert this in your ~/.vimrc file:
syntax off
Note that even with vim, there can be different versions. On my Debian Etch system, the vim is vim.tiny, and it does not support syntax highlighting. So, you don't need to explicitly disable syntax highlighting.
Initially a Linux command-line interface blog, it has evolved to cover increasingly more GUI app topics. Instead of just giving you information like some man page, I illustrate their usage in real-life scenarios.
Saturday, May 31, 2008
Tuesday, May 27, 2008
Use the OR operator in grep to search for words and phrases
grep is a very powerful command-line search program in the Linux world. In this article, I will cover how to use OR in the grep command to search for words and phrases in a text file.
Suppose you want to find all occurrences of the words "apples" and "oranges" in the text file named fruits.txt.
Note that you must use the backslash \ to escape the OR operator (|).
Using the OR operator, you can also search for phrases like "green apples" and "red oranges". You must escape all spaces in a phrase in addition to the OR operator.
You can get away with not escaping the spaces or the | operator if you use the extended regular expression notation.
egrep is a variant of grep that is equivalent to grep -E.
P.S. Additional grep articles from this blog:
Suppose you want to find all occurrences of the words "apples" and "oranges" in the text file named fruits.txt.
$ cat fruits.txt
yellow bananas
green apples
red oranges
red apples
$ grep 'apples\|oranges' fruits.txt
green apples
red oranges
red apples
Note that you must use the backslash \ to escape the OR operator (|).
Using the OR operator, you can also search for phrases like "green apples" and "red oranges". You must escape all spaces in a phrase in addition to the OR operator.
$ grep 'green\ apples\|red\ oranges' fruits.txt
green apples
red oranges
You can get away with not escaping the spaces or the | operator if you use the extended regular expression notation.
$ grep -E 'green apples|red oranges' fruits.txt
green apples
red oranges
egrep is a variant of grep that is equivalent to grep -E.
$ egrep 'green apples|red oranges' fruits.txt
green apples
red oranges
P.S. Additional grep articles from this blog:
Sunday, May 25, 2008
Root edit a file using emacs in the same session
We know that we should always log in using our regular non-root account, and only sudo in when necessary to do things that only root can do. Most of the time, you are logged in as a regular user, and you have your emacs editor open.
Now, you realize that you need to edit a file which is only writable by root (say /etc/hosts.allow).
What you can always do is to open up another emacs session with the right credential, and edit the file there:
This becomes a little tedious, doesn't it?
A nifty little trick is to use tramp, an emacs package for transparent remote editing of files using a secure protocol like ssh. You then use tramp to ssh into localhost as root, and modify the target file.
tramp comes pre-packaged within GNU emacs 22+. This is pretty handy, especially if you have already configured and using it to remotely edit files.
If you are new to tramp, insert the following lines into ~/.emacs (your emacs configuration file), and restart emacs:
The scp method for tramp uses ssh to connect to the remote host. In this case, you are merely connecting to localhost as root. This provides security for you as you edit the file as root.
Note that if you are also using the emacs package recentf (for remembering the most recently opened files), insert the following line as well. Otherwise, when you restart emacs in subsequent sessions, it will prompt you for the root password.
That is it for configuring tramp for use in emacs.
With this setup, you can use the same emacs session you opened as a non-root user to edit a root-only writable file.
To edit the target file, hit Cntl-x followed by Cntl-f, and enter the following before hitting return:
When prompted, enter the password for root.
After you finish editing, save the file as you normally do in emacs.
A final note is that you need to be aware of the side-effects of using tramp to edit a file while the auto backup feature of emacs is enabled. Specifically, make sure that the backup file is saved in an expected safe location. See this article for more details.
Now, you realize that you need to edit a file which is only writable by root (say /etc/hosts.allow).
What you can always do is to open up another emacs session with the right credential, and edit the file there:
$ sudo emacs /etc/hosts.allow
This becomes a little tedious, doesn't it?
A nifty little trick is to use tramp, an emacs package for transparent remote editing of files using a secure protocol like ssh. You then use tramp to ssh into localhost as root, and modify the target file.
tramp comes pre-packaged within GNU emacs 22+. This is pretty handy, especially if you have already configured and using it to remotely edit files.
If you are new to tramp, insert the following lines into ~/.emacs (your emacs configuration file), and restart emacs:
(require 'tramp)
(setq tramp-default-method "scp")
The scp method for tramp uses ssh to connect to the remote host. In this case, you are merely connecting to localhost as root. This provides security for you as you edit the file as root.
Note that if you are also using the emacs package recentf (for remembering the most recently opened files), insert the following line as well. Otherwise, when you restart emacs in subsequent sessions, it will prompt you for the root password.
(setq recentf-auto-cleanup 'never)
That is it for configuring tramp for use in emacs.
With this setup, you can use the same emacs session you opened as a non-root user to edit a root-only writable file.
To edit the target file, hit Cntl-x followed by Cntl-f, and enter the following before hitting return:
/su::/etc/hosts.allow
When prompted, enter the password for root.
After you finish editing, save the file as you normally do in emacs.
A final note is that you need to be aware of the side-effects of using tramp to edit a file while the auto backup feature of emacs is enabled. Specifically, make sure that the backup file is saved in an expected safe location. See this article for more details.
Thursday, May 22, 2008
Delete Windows/DOS carriage return characters from text files
Different operating system may use different characters to indicate the line break. Unix/Linux uses a single Line Feed (LF) character as line break. Windows/DOS uses 2 characters: Carriage Return/Line Feed (CR/LF). MacOS uses CR.
Nowadays, it is a reality that we operate on multiple platforms. If you transfer a text file created on a Windows machine to a Linux machine, the file will contain those extra Carriage Return characters. Some Linux programs run just fine with those characters in their input, but some are less forgiving.
Below are various ways to remove the Carriage Control characters from each line of a text file:
If your system has dos2unix or fromdos installed, then using either one is probably the simplest. Otherwise, tr seems like a safe bet, and it is available on all Linux systems, if you don't mind the extra step of copying the intermediate file. If you absolutely want a one-liner to do the job, then either sed or perl with their in-line modification will satisfy you.
Nowadays, it is a reality that we operate on multiple platforms. If you transfer a text file created on a Windows machine to a Linux machine, the file will contain those extra Carriage Return characters. Some Linux programs run just fine with those characters in their input, but some are less forgiving.
Below are various ways to remove the Carriage Control characters from each line of a text file:
- dos2unix
$ dos2unix input.txt
dos2unix: converting file input.txt to UNIX format ...
dos2unix will convert and overwrite the input file by removing the CR characters.
Be warned that dos2unix is not by default pre-installed in all Linux distributions. If you have a RedHat-based distribution (e.g., Centos), you are safe.
On my Debian Etch system, you need to install a package named fromdos, and even then, dos2unix is just a soft link to another program, fromdos. See next command. - fromdos
fromdos and the corresponding todos reside in a package named tofrodos.
To install,$ apt-get install tofrodos
To run fromdos,$ fromdos input.txt
Note that fromdos will overwrite the input.txt file.
- tr
$ tr -d '\r' < input.txt > output.txt
$ cp output.txt input.txt
\r is the carriage control character.
tr -d removes the specified character (\r in this case) from the standard input.
tr deals with the standard input and standard output only. So, tr cannot write directly to the original input file (input.txt): an intermediate file (output.txt) is needed. - sed
$ sed -i.bak -e 's/\r//g' input.txt
The advantage of sed over tr is that you can do in-line substitution. No need to create an intermediate file. This is done by the -i option.
If you want to make a backup of the original input.txt, you can specify a different file suffix like this:$ sed -i.bak -e 's/\r//g' input.txt
-i.bak will make a backup file by appending the suffix .bak to your original file name, resulting in something like input.txt.bak - perl
$ perl -i.bak -pe 's/\r//g' input.txt
If your system has dos2unix or fromdos installed, then using either one is probably the simplest. Otherwise, tr seems like a safe bet, and it is available on all Linux systems, if you don't mind the extra step of copying the intermediate file. If you absolutely want a one-liner to do the job, then either sed or perl with their in-line modification will satisfy you.
Tuesday, May 20, 2008
Run ifconfig as non-root user for read-only access to network interfaces
It is a frequent scenario that you are logged in to the console of a Linux system, and you need to know its IP address.
If you are the root user, that is easy:
However, if you are not root....
At this point, you are probably ready to give up. Don't: there is always hope.
A not well-publicized fact is that the ifconfig command is executable by anyone: it is just NOT on the default PATH for non-root users.
To find out where ifconfig is:
Is it true that anyone can run ifconfig?
The answer is yes.
To run ifconfig, /sbin needs to be on your PATH, is it?
No, afraid not. No wonder you cannot run the ifconfig command.
It is straight-forward to append that to your PATH.
Let's give ifconfig another try.
To save some typing, you can combine the setting of the PATH, and the ifconfig command as follow:
Now, non-root users are happy.
Note that only non-root users can only get/read interface data, but not set/write it. Setting interface parameters as a non-root user will generate errors:
If you are the root user, that is easy:
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0B:6B:E1:BC:14
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20b:6aff:fed0:bb04/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8100 errors:0 dropped:0 overruns:0 frame:0
TX packets:7727 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5385440 (5.1 MiB) TX bytes:1454259 (1.3 MiB)
Interrupt:177 Base address:0xdc00
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:68 errors:0 dropped:0 overruns:0 frame:0
TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5204 (5.0 KiB) TX bytes:5204 (5.0 KiB)
However, if you are not root....
$ ifconfig
bash: ifconfig: command not found
At this point, you are probably ready to give up. Don't: there is always hope.
A not well-publicized fact is that the ifconfig command is executable by anyone: it is just NOT on the default PATH for non-root users.
To find out where ifconfig is:
$ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
Is it true that anyone can run ifconfig?
$ ls -l /sbin/ifconfig
-rwxr-xr-x 1 root root 66024 Aug 12 2006 /sbin/ifconfig
The answer is yes.
To run ifconfig, /sbin needs to be on your PATH, is it?
$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/cdk4msp/bin:/home/peter/bin
No, afraid not. No wonder you cannot run the ifconfig command.
It is straight-forward to append that to your PATH.
$ export PATH=$PATH:/sbin
$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/cdk4msp/bin:/home/peter/bin:/sbin
Let's give ifconfig another try.
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0B:6B:E1:BC:14
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20b:6aff:fed0:bb04/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8590 errors:0 dropped:0 overruns:0 frame:0
TX packets:8218 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5530730 (5.2 MiB) TX bytes:1509759 (1.4 MiB)
Interrupt:177 Base address:0xdc00
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:68 errors:0 dropped:0 overruns:0 frame:0
TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5204 (5.0 KiB) TX bytes:5204 (5.0 KiB)
To save some typing, you can combine the setting of the PATH, and the ifconfig command as follow:
$ PATH=$PATH:/sbin ifconfig
Now, non-root users are happy.
Note that only non-root users can only get/read interface data, but not set/write it. Setting interface parameters as a non-root user will generate errors:
$ ifconfig eth0 192.168.0.155
SIOCSIFADDR: Permission denied
SIOCSIFFLAGS: Permission denied
Subscribe to:
Posts (Atom)