Help - Search - Members - Calendar
Full Version: How To Not Break Your Config Files
The Planet Forums > System Administration > HOWTOs
RS-Mark
After reading through this forum, I have noticed a dangerous trend. Every post I have read that suggests editing a file, suggests the use of pico. Some even point out where your cursor needs to be in order to save without breaking the file. Pico is a dangerous editor for system files and used by a new admin or used in a sloppy manner can leave line breaks that will even keep your system from booting.

Installed on every RS image is a standard *nix text editor known as vi, vi is easy to use for simple things and much more powerful than pico ever thought of being. But I am not here to proselytize, I am posting this so new admins will not be left in the dark. This is a simple quick reference of how to open a file in vi, edit it, save, and exit; all without the danger of extra line breaks. (typos are your own problem)

To open a file in vi for editing you simply type vi , this will open the file in view mode, you cannot make changes to the file yet.

If it is a short file and you do not need to search for anything in the file, simply use the arrow keys to locate where you would like to make changes and hit i this puts you in insert mode, you can now change text.

If you need to search for text in the file simply hit the / and then type the text you need to search for. If you need to repeat a search hititng / then will repeat the last search. If you are in insert mode, hit and then / to search.

Once you are satisfied that you have made the changes you need to make and are ready to save hit the then : , this will put you in command mode after hitting the and : just type x and hit enter, and the file will save and you will exit vi. If you need to quit without saving all you have to do is type q! instead of x

Vi is not easy to get used to, but with four simple commands I have shown you how to edit files easily. Aside from your own typos, there is no risk of vi adding line breaks that you do not want or even know about to your files. As in everything in life it is better to be safe than sorry.

These are links to sites that will help you learn more about vi should you choose to do so:

Beginner's Basicvi Commands
More advanced vi reference
vi FAQ
A learning vi website

That said I am off for now...
webbcite
here here...vi is MUCH better than pico. I would also encourage any and all to take the time to learn it.

O'Reilly has a "vi Editor Pocket Reference" book that is great and fits behind your pocket protector icon_razz.gif
RS-Mark
I have a copy of the O'Reilly pocket ref, it is invaluable to me. Worth every cent I paid for it. I would reccomend every person that wants to admin thier own server learn at least the basics of vi. (more than what is my post) It is included in every distribution of Linux I can think of, and is on every *nix system I have ever worked on. It is the universal text editor of the *nix world.
secwrd
/me agrees with webbcite


here's a reference you can look at download that was in the client how-to bit on this forum... cant remember who posted it...

p.s. I was gonna change that after a few seconds... icon_razz.gif
secwrd
k... file attach bit don't work....
danielzen
I just had to put a word in for emacs. It was one of the first things I installed. It's a great visual editor whose commands strokes are very similar to pico's, but has tremendous functionality and extensibility. Developed by Richard Stallman founder of the Free Software Federation and the GNU project without which Linux itself would not have been possible.

It does have a tendency to leave backup files that end with a tilde (~) which can be good and bad.

Good because you have a backup in case your edits were wrong.

Bad because certain directorys (like /etc/httpd/conf/virtual and all /etc/httpd/conf/siteX) have all files in them parsed when the apache httpd server is restarted.

I installed the following 2 rpms to use emacs on RH 7.2:

emacs-nox-20.7-41
emacs-20.7-41

and it doesn't conflict with ensim at all, and comes with it's own documentation.

z
RS-Mark
I knew we were gonna get an emacs prophet in this thread. The reasons I do not reccomend emacs to the new admin:

It is not standard on every *nix
It is harder to learn
It is a memory hog

While it is nice for development, it is really overblown for a text editor. Similar IMO to using a .50 caliber rifle to hunt mice. icon_biggrin.gif Just messing with you, emacs is nice, but for what comes installed on every *nix I have ever touched, vi takes the cake.
daveman692
I used to swear by vi, then I got a Mac. For some reason OS X makes vi work really weird. Maybe it is how the terminal does its emulation but vi got so unusable I have had to resort to pico. icon_sad.gif
franky333
I create a directory instead of a file. How do I delete the directory?
NightHawk
QUOTE
Originally posted by franky333
I create a directory instead of a file. How do I delete the directory?

rm -Rf directoryname
Albo
Nice advice RS-Mark. VI is clean! pico is nasty !

Shift + Z + Z (same thing as save and exit)
junky
how about any other good editors? suggestions?
mouse
gotta hand it to ya RS-Mark.. thats the first how to for using VI that made any sense at all and didnt seem to require several years of playing Twister to exit out of it.. Mouse
jaume
VI forever icon_biggrin.gif
perldork
Nitpicker (me) says: icon_smile.gif

On Linux, it is vim, not vi! Yes, two are linked ...

We get spoiled on Linux ... I have many times gone from Linux to Solaris and gotten frustrated because

Ctl-V
Ctrl-W

don't work icon_smile.gif or when I do

:

I get a control P character instead of my previous :ex command!

Regardless of my nitpicking which is all in good fun, vi and vim are both incredible editors for system administration.
bobk
This is all well and good. But somebody has to step in here and point out to all the newbies that both vi and emacs are instruments of torture, and are classified as such by UN Weapons Inspectors. Save your sanity, learn to run pico -w. It takes about five years less than learning vi. As for emacs, I started it up once to have a look, and I accomplished absolutely nothing. Couldn't enter text, couldn't open a file. Couldn't even figure out how to exit the program, I had to hit the rest button. These programs are evil. The only use for them is so that we admins can tell people to "edit your dot file in vi" and it usually gets them out of your hair, and they're afraid to come back.

icon_smile.gif
perldork
Bob,

No ... pico is too hard for a newbie .. for that they have to know how to use the shell icon_smile.gif ... newbies should run X/KDE and use knotepad!

icon_smile.gif

Just kidding. DON'T do that.

I admit, my first 3 months of doing admin I used pico .. until I tried to edit some system file and screwed it up by inserting control characters ... and then again by messing up sendmail.cf by having both tabs and spaces and messing up a long line due to word wrap ... that was 7 years ago and I haven't looked back since.

Yes, vi and emacs have a learning curve, but if anyone is planning on doing *nix admin on a regular basis, knowing the others, especially vi, is very useful .. emacs is a wonderful operating system icon_wink.gif and is great for development too .. and a ton of other things, but isn't a standard part of *nix installs ... vi is a standard part of every *nix variant out there (vim isn't) .. pico is only standard on Linux.

Nothing wrong with pico, but over time I think most admins grow out of it ... one, they like the modefulness of vi for editing, and two the shortcut keys are great for doing relatively complex tasks quickly under stress ... if they don't like it, if you don't like it, that is fine too .. whatever works best for an admin is what he/she should use .. be it pico, emacs, or good ole' ed!
micxz
I've always been a vi person. Very good advice RS-Mark!!! I thought about this once before reading a howto that said something like: pico /etc/passwd !

some commands:
http://www.cs.wustl.edu/~jxh/vi.html
bobk
Newbies, don't do it, your brain will never be the same. It's too high a price to pay for being cool among the Unix crowd. Remember those commercials, "This is your brain... this is your brain on emacs." And don't fall for that line about how every Unix servers has vi. Mine don't, I rename it to notepad.exe just to mess with the heads of Unix old-timers.

It's pretty funny watching them search through the whole filesystem for it. They just can't believe it's not there.
perldork
icon_razz.gif .. do you rename httpd as IIS.EXE? How about bash? Does that become dosprmpt? And ls -> dir?

Sounds like VMWare with WinME running on your RS Linux box would be your ideal environment icon_biggrin.gif ... haha.

I think it is time for a "HOW-TO install Win98 on your RS box" thread icon_smile.gif ...
mouse
QUOTE
Originally posted by perldork
icon_razz.gif .. do you rename httpd as IIS.EXE?  How about bash?  Does that become dosprmpt?  And ls -> dir?

Sounds like VMWare with WinME running on your RS Linux box would be your ideal environment icon_biggrin.gif  ... haha.

I think it is time for a "HOW-TO install Win98 on your RS box" thread icon_smile.gif ...


Scratches head.... hmmmm WinME on a RS server... interesting... lol.. Mouse
junky
I hae been setting up my RS box for a few days now, thats just cause i am still a noob with linux, but its been fun and i feel really confident with it now, im not scared any more...

When i first started using VI i got trapped in it and never used it again, thats how pico came into my daliy activities, however i finally decided to just learn vi's commands and functions for it and it really is much better than pico (however i like pico's CTRL+W search), if some one hase a good search command for vi / vim i would like to know (ie: *string*).

For those of you starting, just remember that when you are in command mode (it doesnt say --insert-- or anything else at the bottom of the screen) any keys can be considered a command. Just memorize the i command and you should do good. Most importantly to exit, press ESC the :q or :q!

GL
perldork
Hi,

As Mark mentioned above icon_smile.gif, typing

/

in command mode will do a search for you ... e.g.

/foo

Once you have found a match, pressing 'n' will take you to the next match, and 'N' will take you to the previous match ... in vim you can also type, from command mode,

:set hlsearch

Doing this will cause vim to highlight all matches so you can easily see them ... and it will also, in real-time, show you the FIRST string that will match in a different highlighted color .. e.g. if you have:

The food in the box
is the same food that
is in the locker

and your cursor is above the word 'The', and you then type

/the

The first 'the' will show up highlighted in one color as you type it. When you press , all the occurences of 'the' in the text will be highlighted.

Also, searches in vi/vim can use shell-style regular expressions ... vim also has extended wildcards too ... in vi and vim you can do things like

/t.e

to match the ... or

/

to only match 'the' as a single word, as opposed to a substring ...

With vim you can use perl-style wildcards too, e.g. to match

the 8 times

I could search for

/thesdst/

Hope that helps and doesn't confuse icon_smile.gif you. That is just a bit of what can be done with searching in vi/vim.
micxz
If you know what line number you need to get to and change then finding that line in VI is easy:

say your looking to edit line 100, use command:

100G

If you want to copy/cut a line and paste it:

get the cursor to the line you want:

yy (for copy)
dd (for cut)

this puts the curent line into the buffer.

Then to paste that line under the current line the cursor is on:

p

or if you want to paste above the current line your on:

P

Hope this helps someone'

Remember you can always quit without saving in VI by using the command:

:q!

That's a colon, lower case Q and a Exclaimation mark.

With saving:

ZZ or :w then :q
dafonso
I have found nothing better for editing config files than 'vi'. The reason is that it's designed to run on slower connections, and as such has really powerful search, and movement functions. This is all at the expense of ease of use. However, searching and replacing is incredibly powerful, and so is doing mass changes to the files, and it's replication functions.

emacs is an OS disguised as a text editor. I find it invaluable for programming, and scripting.

pico is good for writing e-mails, and free-form text. It's really not at all powerful, but it's damn easy to use.

Then you have joe. It was written by someone from WPI, so I had to throw it out there. About as easy to use as pico.
alapo
can't you just do "pico -w filename" so it doesnt insert line breaks? icon_biggrin.gif

Although I do like vi icon_smile.gif.
ehost
I press ESC and then q but it just locked me there. I can't exit without saving
null_vector
Emacs rules all.

php mode
interactive lisp mode
interactive python mode
html mode
c++ mode
c mode
.....

although i do occasionally have to use vim/vi when not at one of my boxes rolleyes.gif
perldork
QUOTE
Originally posted by ehost
I press ESC and then q but it just locked me there. I can't exit without saving


You have to type a colon ":" to get back to command mode .. so, to exit without saving:

:q!

Emacs is great for development but wayy too heavy for sysadmin use in my opinion icon_smile.gif ..
-ToXiC-
Im not really positive...But I think 'pico -w' just takes out the white spaces. Sort of like 'trim()' in php.

Atleast I think thats what it does anyway. I've always used it just because its simple. But when it comes to editing the httpd.conf, or apache.py, I have always used 'vi'. icon_smile.gif
mouse
QUOTE
Originally posted by -ToXiC-
Im not really positive...But I think 'pico -w' just takes out the white spaces. Sort of like 'trim()' in php.  

Atleast I think thats what it does anyway. I've always used it just because its simple. But when it comes to editing the httpd.conf, or apache.py, I have always used 'vi'. icon_smile.gif


lol, no actually it prevents it from wrapping the lines.. Mouse
-ToXiC-
maybe I need to add a "I don't really know what i'm talking about" line in my sig as well. icon_razz.gif
kwiznoz
Is there a way to search for a reference to a filename in a server/cart. Specifically, we have oscommerce installed and somehwhere is is referencing a /store/ directory and it should be /catalog/.

Just wondering if there is a program that can parse through the files to find this reference or am I am I being too lazy icon_razz.gif
perldork
CODE
# cd <os-commerce-base-dir>

# find . -type f | xargs fgrep -l /store
kwiznoz
icon_smile.gif

Huge help, thank you!
mega023
you don't need pico or vi

just install Midnigh Commander

http://www.ibiblio.org/mc/

and you have shell application (linke Norton Commander in DOS, or Total Commander in Win) for all your need (copying, editing...)
Frykshon
I never really used pico, I have always used nano. Only sometimes when I run across a box that doesn't have nano ill use vi. Vi is probably the most useful to know because in my experience thats whats most likely going to be there for you when your in a bind and you've had to boot from an repair cd.
James Jhurani
lol way to resurrect a thread that has been dead for ~2 years!

I actually got so used to typing pico from back in my mandrake linux days(yes, I said it, mandrake!) that I alias pico = nano.

I am not really sure what the original thread starters were talking about with spaces or line breaks causing systems to not boot, but some people prefer to use the -w(on pico/nano) flag to make sure that special chars do not become corrupt.

I'm not a big fan of vi, mainly because of its cryptic key sequences, dd to remove a line, :q! to escape and not save changes, etc...

I agree it is very powerful... I hit a few keys by accident while working with it, and all of a sudden I had two instances of the file open, one on top of the other, which was actually pretty cool! but I prefer simplicity.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.