Lesson 22 / الدرس 22
Editing a file on a server / تعديل ملف على خادم
There is no graphical editor on the other end of an ssh connection. You need enough of one terminal editor to change a line and save — and enough of the other to escape it.
لا محرر رسومي على الطرف الآخر من اتصال ssh. وتحتاج من محرر طرفية ما يكفي لتغيير سطر وحفظه — ومن الآخر ما يكفي للهروب منه.
Two editors are on essentially every server. nano is simple and shows its own instructions at the bottom of the screen. vim is powerful, is what you get when something else opens an editor for you, and is impossible to leave if nobody has told you how. Learn nano to work; learn three vim commands to escape.
nano, in five keys
nano config.php open it — just type and edit, the arrows work
Ctrl + O write the file out (then Enter to confirm the name)
Ctrl + X exit
Ctrl + W search
Ctrl + K cut the current line
Ctrl + G help
^O where it means Ctrl + O — a notation worth recognising because it appears in many terminal programs. Run the demonstration for the two keys that actually matter.
^O حيث يعني Ctrl + O — وهو ترميز يستحق أن تعرفه لأنه يظهر في برامج طرفية كثيرة. شغّل العرض للمفتاحين المهمين فعلًا.Why vim keeps appearing
You do not have to choose vim to end up in it. git commit without a message opens an editor, and so does crontab -e — and unless EDITOR from lesson 16 says otherwise, that editor is vim. This is why so many people meet it by accident and why :q! is worth memorising before you ever need it.
git commit بلا رسالة يفتح محررًا، وكذلك crontab -e — وما لم يقل EDITOR من الدرس 16 غير ذلك، فذلك المحرر هو vim. ولهذا يقابله كثيرون بالصدفة، ولهذا يستحق :q! الحفظ قبل أن تحتاجه.
The thing that confuses everyone about vim is that it has modes. It starts in command mode, where the letters you press are commands rather than text — which is why typing produces chaos. i enters insert mode and lets you type normally; Esc takes you back. If you set EDITOR to nano in your startup file, none of this will surprise you again.
EDITOR على nano في ملف بدئك، فلن يفاجئك شيء من هذا مرة أخرى.cp config.php config.php.bak. فالخادم الذي يكفّ عن الإجابة بسبب خطأ مطبعي في ملف إعدادات موقف سيئ أن تكون فيه عبر اتصال قد يعتمد هو نفسه على ذلك الخادم. ومع نسخة بجانبه يكون التعافي mv واحدًا. وبدونها تعيد البناء من الذاكرة والموقع معطل.| Instead of editing on the server | Do this |
|---|---|
| A whole file you wrote locally | Edit at home, then scp or rsync it up |
| A one-line change | nano is fine — but take the backup first |
| Anything in version control | Commit locally, then pull on the server |
| A file you are unsure about | Copy it down, look at it at home, send it back |
Editing directly on a server is normal for a quick fix and a poor habit for anything larger. A change made only on the server exists in one place, is in no history, and disappears the next time someone deploys.
export EDITOR="nano" في ملف بدئك من الدرس 18 فيفتح كل برنامج يفتح لك محررًا ذلك المحرر. وهو سطر واحد يستغرق عشر ثوانٍ، ويزيل صنفًا كاملًا من الحيرة من حياتك العملية.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. You are in vim by accident and have made a mess. What gets you out safely?
Esc returns you to command mode and :q! quits without saving. The file on disk is untouched, whatever you did on screen.
:q! يخرج بلا حفظ. والملف على القرص كما هو، مهما فعلت على الشاشة.2. Why does vim keep opening when you did not ask for it?
Setting EDITOR in your startup file — lessons 16 and 18 together — is the one-line fix.
EDITOR في ملف بدئك — الدرسان 16 و18 معًا — هو الإصلاح من سطر واحد.3. What should you do before editing a configuration file on a live server?
A typo can stop the server answering, and you may be reaching it through that very server. A copy beside it turns a crisis into one command.
Score / النتيجة: 0 / 3
Your task / مهمتك
Practise on your own machine, where a mistake costs nothing. Create a small configuration file, take a backup of it, edit it in nano and save. Then open the same file in vim, make a change, and leave with :q! — showing that the file is unchanged afterwards. Report both sessions and the keys you used.
تدرّب على جهازك، حيث لا يكلّف الخطأ شيئًا. أنشئ ملف إعدادات صغيرًا، وخذ نسخة احتياطية منه، وعدّله في nano واحفظ. ثم افتح الملف نفسه في vim، وأجرِ تغييرًا، واخرج بـ:q! — مُظهرًا أن الملف لم يتغير بعدها. وأبلغ عن الجلستين والمفاتيح التي استخدمتها.
- A backup taken before editing نسخة احتياطية أُخذت قبل التعديل
- A real nano edit, saved تعديل nano حقيقي، محفوظ
- A vim session left with :q!, and proof the file is unchanged جلسة vim غادرتها بـ:q!، ودليل أن الملف لم يتغير
- The keys you pressed, listed المفاتيح التي ضغطتها، مسرودة