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
That is the whole editor for practical purposes. The bar at the bottom lists these, writing ^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.

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.

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.

Instead of editing on the serverDo this
A whole file you wrote locallyEdit at home, then scp or rsync it up
A one-line changenano is fine — but take the backup first
Anything in version controlCommit locally, then pull on the server
A file you are unsure aboutCopy 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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. You are in vim by accident and have made a mess. What gets you out safely?

2. Why does vim keep opening when you did not ask for it?

3. What should you do before editing a configuration file on a live server?

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 المفاتيح التي ضغطتها، مسرودة
How do you want to submit? / كيف تريد التسليم؟