⌨️ Vim Basics

Create and edit files from the Mac/Linux terminal · การแปลภาษาไทยกำลังจะมา · 在 Mac/Linux 终端中创建和编辑文件

🎯 What this page does for you · เป้าหมาย · 本页能帮你做什么

🇬🇧 English

Vim is a text editor that lives inside your terminal. It is pre-installed on every Mac and almost every Linux machine, so you can create, edit, and save a Python file without leaving the terminal window.

By the end of this page you should be able to open the Terminal, create hello.py with vim, type print("Sawadee from Chiang Mai"), save, quit, and run it with python hello.py. We skip the famous keyboard tricks — learn those later, after vim stops feeling scary.

🇹🇭 ไทย

การแปลกำลังจะมา (Thai translation coming soon)

🇨🇳 中文

Vim 是一款运行在终端里的文本编辑器。Mac 和几乎所有 Linux 系统都默认安装了它,这意味着你可以在终端窗口里创建、修改、保存 Python 文件,整个过程都不用离开终端。

读完本页后,你应该能够打开终端,用 vim 创建 hello.py,输入 print("Sawadee from Chiang Mai"),保存、退出,然后用 python hello.py 运行它。那些有名的快捷键技巧我们先跳过 —— 等你不再害怕 vim 之后再去学也不迟。

1️⃣ Open the Terminal · เปิด Terminal · 打开终端

🇬🇧 English

On a Mac:

  1. Press + Space to open Spotlight.
  2. Type terminal and press Return.

You'll see a window with a blinking cursor. That's the shell. The first command you'll learn is vim.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

Mac 上的步骤:

  1. + Space 打开 Spotlight。
  2. 输入 terminal,然后按 Return

你会看到一个窗口,里面有一个闪烁的光标。这就是 shell(命令行)。你要学的第一个命令就是 vim

2️⃣ Your first file — the whole recipe · ไฟล์แรกของคุณ · 你的第一个文件 — 完整流程

vim hello.py

🇬🇧 English

Vim opens. The screen is empty except for a column of ~ on the left ("no lines yet"). The filename shows at the bottom.

Follow this sequence exactly — don't worry about understanding yet:

  1. Press i. Bottom shows -- INSERT --.
  2. Type print("Sawadee from Chiang Mai")
  3. Press Esc. The -- INSERT -- disappears.
  4. Type :wq and press Return.

You're back in the terminal. Run it:

python hello.py

You should see Sawadee from Chiang Mai printed. You just created a Python file with nothing but the keyboard.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

Vim 打开了。屏幕看起来是空的,只有左侧一列 ~(意思是"这一行还没有内容")。屏幕最底部会显示文件名。

严格按照下面的步骤来 —— 先不用理解为什么:

  1. i。屏幕底部出现 -- INSERT --
  2. 输入 print("Sawadee from Chiang Mai")
  3. Esc。底部的 -- INSERT -- 消失。
  4. 输入 :wq 然后按 Return

你回到了终端。运行它:

python hello.py

你应该会看到屏幕上打印出 Sawadee from Chiang Mai。你只用键盘就创建了一个 Python 文件。

3️⃣ The thing that confuses everyone — modes · โหมด · 让所有人都困惑的那件事 — 模式

press i / 按 i NORMAL ─────────────► INSERT ◄───────────── press Esc / 按 Esc

🇬🇧 English

Vim has two modes. This is the single most important idea, and the only thing that makes vim feel weird at first.

  • Normal mode — what you start in. Pressing letters does commands. Press x and a character disappears.
  • Insert mode — what you go into after i. Keys type text like any other editor.

Rule of thumb: if your typing isn't appearing on screen, you forgot i. If letters do weird things, you forgot Esc.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

Vim 有两种模式。这是最重要的一个概念,也是 vim 一开始让人觉得别扭的唯一原因。

  • 普通模式(Normal) —— 你打开 vim 时所在的模式。按字母键执行的是命令。按 x,一个字符就被删掉。
  • 插入模式(Insert) —— 按 i 之后进入的模式。这时键盘和普通编辑器一样。

判断口诀:按了键但屏幕没出现字 —— 忘了按 i。按字母键发生奇怪的事 —— 忘了按 Esc

4️⃣ The commands you actually need · คำสั่งที่ต้องรู้ · 你真正需要的命令

Command What it does · หน้าที่ · 作用
i Enter insert mode (start typing). — กำลังแปล 进入插入模式(开始输入文字)。
Esc Leave insert mode, back to normal mode. — กำลังแปล 退出插入模式,回到普通模式。
:w Save (write) the file. Then press Return. — กำลังแปล 保存(write)文件。按 Return
:wq Save and quit. — กำลังแปล 保存并退出。
:q Quit (only if you haven't changed anything). — กำลังแปล 退出(只在没有改动时才行)。
:q! Quit without saving — throw away changes. — กำลังแปล 强制退出,不保存 —— 丢弃修改。
u Undo (in normal mode). — กำลังแปล 撤销(在普通模式下)。
Ctrl + r Redo. — กำลังแปล 重做。

Commands starting with : are called ex commands. When you press :, the cursor jumps to the bottom and you type the rest there. — กำลังแปล : 开头的命令叫做 ex 命令。按下 : 后光标会跳到屏幕最底部,你在那里输入剩下的内容。

5️⃣ Moving the cursor · การเลื่อนเคอร์เซอร์ · 移动光标

Arrow keys work. That's all you need at first. The cooler way (hands stay on the home row): — กำลังแปล 方向键可以用。一开始你只需要知道这个。更地道的方式(双手不离开主键盘行):

Key Direction · ทิศ · 方向
hleft — กำลังแปล
jdown — กำลังแปล
kup — กำลังแปล
lright — กำลังแปล
ggjump to top of file — กำลังแปล 跳到文件开头
Gjump to bottom of file — กำลังแปล 跳到文件末尾
0start of line — กำลังแปล 跳到行首
$end of line — กำลังแปล 跳到行尾

These are normal mode commands. In insert mode they just type letters — press Esc first. — กำลังแปล 这些都是普通模式下的命令。在插入模式下按它们会被当成普通字符 —— 先按 Esc

6️⃣ Editing an existing file · การแก้ไฟล์เดิม · 编辑已有的文件

vim hello.py

🇬🇧 English

Vim opens with your saved content. Move with arrows or h j k l. To change something:

  • Press i to start inserting at the cursor.
  • Press x in normal mode to delete the character under the cursor.
  • Press dd in normal mode to delete a whole line.
  • Press o in normal mode to open a new line below and drop into insert mode.

When done: Esc then :wq.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

Vim 打开后会显示你之前保存的内容。用方向键或 h j k l 移动光标。要修改内容:

  • i 从光标位置开始插入。
  • 在普通模式下按 x 删除光标所在的那一个字符。
  • 在普通模式下按 dd 删除整行。
  • 在普通模式下按 o 在当前行下面新开一行,并直接进入插入模式。

改完之后:Esc,然后 :wq

7️⃣ The escape hatches · ทางออกฉุกเฉิน · 紧急逃生通道

🇬🇧 English

I can't quit. Try:

Press Esc twice Then type :q! and press Return

Esc drops you out of any mode. :q! means "quit and throw away changes." This always works.

I messed up the file. Press u repeatedly in normal mode. Each press undoes one change.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

我退不出去了。试这个:

连按两次 Esc 然后输入 :q! 再按 Return

Esc 能让你跳出任何模式。:q! 的意思是"退出,丢弃改动"。这一招永远有效。

我把文件改坏了。在普通模式下连续按 u。每按一次撤销一步。

8️⃣ If vim feels too much — use nano · ถ้า vim ยากเกินไป — ใช้ nano · 如果你觉得 vim 太难 — 用 nano

nano hello.py

🇬🇧 English

Macs also come with nano, a simpler editor with no modes. The controls are listed at the bottom of the screen all the time.

Type freely. To save: Ctrl + O then Return. To quit: Ctrl + X. The ^ symbols on screen mean Ctrl.

Nano is fine. The reason this page is about vim is that it's installed everywhere — when you SSH into a server or rent a cloud machine, vim will be there and nano sometimes won't.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

Mac 也自带 nano,一款没有模式的简单编辑器。操作命令一直显示在屏幕底部。

直接输入文字就行。保存:Ctrl + O 然后按 Return。退出:Ctrl + X。屏幕上的 ^ 符号代表 Ctrl 键。

用 nano 完全没问题。这一页之所以讲 vim,是因为它无处不在 —— 以后你 SSH 登录到服务器或者租云主机时,vim 一定在那里,但 nano 不一定。

9️⃣ After saving — run your file · หลังบันทึก — รันไฟล์ · 保存之后 — 运行你的文件

python hello.py

🇬🇧 English

If you see command not found: python, try python3. Mac sometimes only has python3, not python.

From here, Python Basics is the natural next step — now that you can make files, the rest of the language is just learning what to put inside them.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

如果出现 command not found: python 这样的错误,改试 python3。Mac 上有时候只装了 python3,没有 python

接下来很自然的下一步是 Python 编程基础 —— 现在你已经会创建文件,剩下的就是学习里面要写什么了。

🔟 Why bother learning vim at all? · ทำไมต้องเรียน vim · 为什么要学 vim?

🇬🇧 English

  • It's everywhere. Every Mac, Linux server, cloud VM, Docker container.
  • When you SSH into a remote machine, you don't have VS Code. You have vim (or sometimes nano).
  • For small edits — fix a typo, add one line — opening VS Code is overkill.
  • Once your hands learn the commands, editing in vim is faster than anything else.

You don't need to love vim. Knowing the eight commands above is enough to make the terminal stop feeling like a wall.

🇹🇭 ไทย

การแปลกำลังจะมา

🇨🇳 中文

  • 它无处不在。每一台 Mac、每一台 Linux 服务器、每一台云虚拟机、每一个 Docker 容器里都有 vim。
  • 当你 SSH 登录到远程机器时,你没有 VS Code,只有 vim(有时候还有 nano)。
  • 小改动 —— 修一个错字、加一行 —— 打开 VS Code 实在大材小用。
  • 一旦你的手熟悉了这些命令,在 vim 里编辑会比任何编辑器都快。

你不需要爱上 vim。只要会用上面这八条命令,终端就不再像一堵墙了。