Computer courses · Tools for technicians · Tech support Call 859-878-2615 WhatsApp Send a message

Home · Blog

Step-by-step guide

How to write source code in plain text: TextEdit, file extensions, Terminal and Vim (Mac)

By Gabriel ·

Short answer: source code must be written and saved as plain text. If you type code in a word processor or in TextEdit's default rich text mode, the file is saved as .rtf with hidden formatting codes, and the program will not work. Switch TextEdit to plain text (Format > Make Plain Text, Shift-Command-T), or create the file directly in the Terminal with Vim, and turn on file extensions so you can see whether a file is .rtf, .txt or .py.

This is lesson 3 of my Python course (2021). In this guide you will write the same line of code three ways on a Mac: first in TextEdit with formatting (the mistake), then in TextEdit as plain text, and finally in the Terminal with Vim to create a real Python file called car.py. Along the way you will learn how to show hidden file extensions, use ls and cat to check what is really inside a file, avoid the classic Vim :qw mistake, and keep in mind why companies protect their source code.

DifficultyVery easy
Time15-20 minutes
Tools & requirementsA Mac with TextEdit and Terminal (both built in), Vim (included with macOS), Optional: a file manager that shows extensions, such as muCommander (Mac) or Total Commander (Windows), On Windows: Notepad and File Explorer work for the same idea
In this article
  1. Make a folder for your code and open a file manager that shows extensions
  2. Open TextEdit and notice the formatting toolbar
  3. Type a line of code in rich text (the mistake to see once)
  4. Save it and see that it becomes an RTF file
  5. Check the real extension in the file manager
  6. Check the extension from the Terminal with ls
  7. Switch TextEdit to plain text and type the code again
  8. Save the plain text file as .txt with UTF-8
  9. Compare both files with ls and cat
  10. Create a Python file in the Terminal with Vim
  11. Press i for insert mode and type your code
  12. Save and quit with Esc and :wq
  13. Confirm the file with ls and cat
  14. Show all filename extensions in Finder
  15. Check the result: three files, three formats
  16. Why source code has to be plain text
  17. What is source code and why do companies protect it?
  18. Follow the rules of your programming language
  19. Vim cheat sheet for beginners
  20. Troubleshooting: my code file does not work
  21. Tips for writing your first source code files
  22. Frequently asked questions
  23. Watch the video

Step 1Make a folder for your code and open a file manager that shows extensions

muCommander file manager open on a Mac next to an empty Finder window for the folder named coding text, where the practice source code files will be saved
  • Create a folder for your practice files. In the video it is a folder called coding text on the Desktop, empty at the start.
  • Open a dual-pane file manager such as muCommander on Mac; on Windows Total Commander does the same job (it is in the style of the old Norton Commander).
  • Why: these tools show the full file name with its extension, which Finder and File Explorer hide by default. You need to see the extension to know what kind of file you created.

Step 2Open TextEdit and notice the formatting toolbar

New TextEdit document on a Mac in rich text mode with the formatting toolbar showing Helvetica font, Regular style, size 12, bold, italic, underline and alignment buttons above the ruler
  • Open TextEdit (the text app included with macOS) and choose New Document.
  • Look at the top of the window: a toolbar with font, size, bold, italic, alignment and a ruler. That means the document is in rich text mode.
  • Rule of thumb: if you see a formatting toolbar, whatever you type will be saved with formatting, and that is not what code needs.

Step 3Type a line of code in rich text (the mistake to see once)

TextEdit rich text document with the line Print "this is a blue car" typed and selected, showing how a formatted document looks before it is saved
  • Type a simple instruction, for example print "this is a blue car". TextEdit may capitalize it to Print automatically.
  • In rich text you can also change the font, size or color. Every change adds hidden formatting to the file.
  • This is only an experiment so you can compare the files later; do not write real code this way.

Step 4Save it and see that it becomes an RTF file

TextEdit save dialog on a Mac saving into the coding text folder with the File Format menu set to Rich Text Document, which creates an .rtf file
  • Choose File > Save, name it (the video uses format) and save it in the coding text folder.
  • Look at File Format: it says Rich Text Document, so the extension will be .rtf (Rich Text Format).
  • Common mistake: saving code from Word, Pages or rich TextEdit. The file looks fine on screen, but programs that read it get the formatting codes too, and things do not work.

Step 5Check the real extension in the file manager

muCommander file list on a Mac showing the file format.rtf with its full .rtf extension next to the .DS_Store file in the coding text folder
  • In Finder the file just appears as format, with no extension visible, but it still has one.
  • In muCommander the same file shows as format.rtf. That is the point of using it.
  • Finder and Windows hide extensions to keep things simple for regular users. If you are a technician or you are learning to program, you need to see everything.

Step 6Check the extension from the Terminal with ls

Mac Terminal window in the coding text folder after running the ls command, listing the file format.rtf with its extension
  • Open the Terminal: press Command + Space, type terminal and press Return.
  • Go to your folder (for example cd ~/Desktop/"coding text") and type ls. The command lists the files with their extensions: format.rtf.
  • Tip: ls never hides extensions, so it is a quick way to check what you really saved.

Step 7Switch TextEdit to plain text and type the code again

TextEdit document in plain text mode with no formatting toolbar or ruler and the line Print "this is a computer" typed at the top
  • Create a new TextEdit document and, before typing, press Shift-Command-T (or choose Format > Make Plain Text).
  • The toolbar and ruler disappear. Now whatever you type is pure plain text, with no fonts or styles stored in the file.
  • Type the line again, for example print "this is a computer".
  • Tip: to make this permanent, go to TextEdit > Settings > New Document and choose Plain text as the default format.

Step 8Save the plain text file as .txt with UTF-8

TextEdit save prompt for a plain text document named withoutformat in the coding text folder, with Plain Text Encoding set to Unicode UTF-8 and the option to use .txt if no extension is provided checked
  • Close the window or choose Save. Name it (the video uses withoutformat).
  • Check that Plain Text Encoding is Unicode (UTF-8) and that If no extension is provided, use ".txt" is checked.
  • You will get withoutformat.txt. Later, for Python, you would type the name with .py at the end instead.

Step 9Compare both files with ls and cat

Mac Terminal showing ls listing format.rtf and withoutformat.txt, then cat withoutformat.txt printing only the plain text line Print "this is a computer"
  • In the Terminal press Control + L to clear the screen (your files are not deleted, only the screen is cleaned).
  • Type ls: you now see format.rtf and withoutformat.txt.
  • Type cat with, press Tab to auto-complete the name, and press Return. cat prints the content: only the line you typed, nothing else. That is what a code file must look like.

Note: Run cat format.rtf too and you will see the extra formatting codes around your text. That is exactly what breaks a program.

Step 10Create a Python file in the Terminal with Vim

Vim text editor opened in the Mac Terminal with an empty new file named car.py, the tilde lines on the left and "car.py" [New] at the bottom
  • You can create a code file without any graphical app. Type vim car.py and press Return (vim: V as in Victor, I, M).
  • Vim opens an empty file and shows "car.py" [New] at the bottom. The .py extension makes it a Python file.
  • Vim is a pure text editor: it never adds formatting and never auto-capitalizes what you type.

Step 11Press i for insert mode and type your code

Vim in insert mode in the Mac Terminal with -- INSERT -- shown at the bottom and the line print "this is a blue computer" typed in car.py
  • When Vim opens you cannot type text yet. Press i and -- INSERT -- appears at the bottom.
  • Type your line, for example print "this is a blue computer". Notice that it stays lowercase, exactly as typed.
  • Common mistake: typing before pressing i. In normal mode, letters are commands and can move or delete text.

Note: The video writes print "..." without parentheses, which is old Python 2 syntax. In Python 3 write print("this is a blue computer") so the file runs.

Step 12Save and quit with Esc and :wq

Zoomed view of the bottom of the Vim window in the Mac Terminal with the command :wq typed to write the file and quit Vim
  • Press Esc: the INSERT label disappears and you are back in normal mode.
  • Type a colon :. It appears at the bottom of the window. Then type wq and press Return: w = write (save), q = quit.
  • The order matters. In the video I first typed :qw and got an error; :wq worked.

Caution: :qw gives E492: Not an editor command. Vim is not like a regular editor where you click close and save; if you do not type the command right, it will not work. Use :wq, or :q! to quit without saving.

Step 13Confirm the file with ls and cat

Mac Terminal after quitting Vim: ls lists car.py, format.rtf and withoutformat.txt, and cat car.py prints print "this is a blue computer"
  • Clear the screen with Control + L and type ls: car.py is now in the folder with the other two files.
  • Type cat car.py to see what is inside: just print "this is a blue computer".
  • That is the whole idea: you can do almost anything in the command line without depending on Notepad or any other app. Practice these steps on your own computer.

Step 14Show all filename extensions in Finder

Finder Settings Advanced tab on a Mac with the Show all filename extensions checkbox being selected so .rtf, .txt and .py extensions are visible in Finder
  • In Finder choose Finder > Settings (Preferences on older macOS), click Advanced and check Show all filename extensions.
  • On Windows, File Explorer has the same option: View > Show > File name extensions on Windows 11, or the File name extensions box on the View tab on Windows 10.
  • Tip: also leave Show warning before changing an extension on, so you do not rename a file by accident.

Step 15Check the result: three files, three formats

Finder window of the coding text folder with all extensions visible showing car.py, format.rtf and withoutformat.txt
  • Now Finder shows car.py, format.rtf and withoutformat.txt, with every extension.
  • Only car.py and withoutformat.txt are plain text. The .rtf file is the one to avoid for code.
  • In the video I unchecked the option again at the end; for programming I recommend leaving it on so the system does not hide anything from you.

Why source code has to be plain text

Diagram comparing what is inside a rich text .rtf file, with font size and color codes around the text, and a plain text .txt file containing only the typed line of code

When you give instructions to a computer you do it in plain text. A compiler or interpreter reads every character in the file, so the font names, sizes and style codes that a rich text file stores are read too, and they are not valid code. The result is an error or nothing at all.

  • Word processors (Word, Pages, Google Docs) and TextEdit in rich text mode add formatting. Do not use them for code.
  • Plain text editors (TextEdit in plain text mode, Notepad, Vim, nano) save only the characters you type.
  • Code editors such as Visual Studio Code are also plain text editors, with extras like colors for the syntax and a Run button. The colors are on screen only, they are not saved in the file.

Once the file is saved as plain text with the right extension, the language's tools can translate it for the computer. How that translation works is explained in compiler vs interpreter.

What is source code and why do companies protect it?

Source code is the human-readable text of a program: the instructions programmers write in a programming language. It is usually transformed by a compiler or an assembler into binary machine code that the computer can execute, or run by an interpreter.

A small program can be one or two lines, like the examples in this guide; a complex one can be hundreds or thousands of lines. That is why most companies keep their source code safe: if someone steals it, they can duplicate the software, modify it a little and have their own version without typing all those lines.

Note: keep backups of your own code and do not publish it if you do not want it copied.

Follow the rules of your programming language

Diagram showing that in Python lowercase print works and prints the text, while Print with a capital P fails with NameError name Print is not defined, because Python is case-sensitive

Plain text is only the first rule. Each language also has its own syntax: some do not care about uppercase, others do. Python is case-sensitive, so print works and Print does not, which is one more reason not to use an editor that capitalizes the first letter of a line for you.

Vim cheat sheet for beginners

What you wantKeysMode
Open or create a filevim car.pyTerminal
Start typingiNormal -> Insert
Stop typingEscInsert -> Normal
Save and quit:wq then ReturnNormal
Save only:wNormal
Quit without saving:q!Normal
Wrong order (error):qw -> E492Normal

If you prefer something simpler in the Terminal, nano car.py also creates a plain text file and shows its shortcuts at the bottom (Control + O to save, Control + X to exit).

Troubleshooting: my code file does not work

Vim window in the Mac Terminal showing the red error message E492 Not an editor command qw after typing the save and quit command in the wrong order
ProblemLikely causeFix
Code saved from TextEdit does not runFile is .rtf with formattingFormat > Make Plain Text, save again
File is called script.py.txtEditor added .txt automaticallyShow extensions and rename to .py
Finder shows no extensionExtensions are hiddenFinder > Settings > Advanced > Show all filename extensions
E492: Not an editor command: qwTyped :qw instead of :wqPress Esc, then type :wq
Letters do nothing or move the cursor in VimNot in insert modePress i first
NameError: name 'Print' is not definedCapital PUse lowercase print
SyntaxError on print "..." in Python 3Python 2 style printUse print("...") with parentheses

Tips for writing your first source code files

  • Keep all practice files in one folder (like *coding text*) so ls and cat are easy to use.
  • Turn on file extensions on every computer you use for programming.
  • Use Tab to auto-complete file names in the Terminal; it avoids typing mistakes.
  • Use Control + L to clean the Terminal screen when it gets crowded.
  • Start with a plain text or code editor, not a word processor.
  • New to programming? Read what a programming language is: syntax and semantics first.

Frequently asked questions

Can I write code in Microsoft Word or Pages?

No. Word processors save formatting together with your text, so the file is not plain text and the program will fail. Use a plain text editor or a code editor.

How do I make TextEdit save plain text on a Mac?

Choose Format > Make Plain Text or press Shift-Command-T before saving. To make it the default, go to TextEdit > Settings > New Document and select Plain text.

What is the difference between .rtf and .txt?

An .rtf (Rich Text Format) file stores your text plus formatting such as fonts, sizes and colors. A .txt file stores only the characters you typed.

How do I show file extensions on a Mac?

In Finder choose Finder > Settings, click Advanced and select Show all filename extensions. In the Terminal, ls always shows the full file names.

How do I show file extensions on Windows?

In File Explorer, choose View > Show > File name extensions on Windows 11, or check File name extensions on the View tab on Windows 10.

How do I save and exit in Vim?

Press Esc to leave insert mode, then type :wq and press Return. :q! quits without saving.

What does E492: Not an editor command mean in Vim?

Vim did not recognize the command you typed after the colon, for example :qw instead of :wq. Press Esc and type the command again correctly.

What file extension do Python files use?

Python source code files use .py, for example car.py. They are plain text files that you run with the Python interpreter.

Need help with this?

Call, text or WhatsApp me at 859-878-0554.

Watch the video

3-What is writing the source code (Course Python 2021)

Original video: 3-What is writing the source code (Course Python 2021)

Comments

More articles

WhatsApp Visitors today: — This month: — Total: —