rubyisforfun/manuscript/016.txt
Roman Pushkin c63012f054 Save
2019-01-04 21:36:19 -08:00

80 lines
4.9 KiB
Text

## Text Editors
There is plenty of text editors out there for all operating systems: MacOS, Linux, Windows, but we'll be discussing only _code_ editors. What's the difference between text editors and code editors? Well, often text editors do not save the file in plain format. For example, Microsoft Word saves the file along with meta information like formatting, font settings, images, and so on. But our code should be "plain".
In other words, what we type on keyboard should be saved to file. You are already familiar with minimalistic code editor:
```
$ cat > app.rb
puts "Hello"
```
Type anything from your keyboard and press Ctrl+D (sometimes you need to press Ctrl+D twice). Everything you typed will be saved to `app.rb`. Note that contents of `app.rb` will be replaced in this case, and if you need to _add_ data to the end of file, you need to use `>>` instead of just `>`.
Now you can see what was written to this file the following way:
```
$ cat app.rb
puts "Hello"
```
You can also look at hexadecimal output, followed by raw data:
```
$ cat app.rb | hexdump -C
00000000 70 75 74 73 20 22 48 65 6c 6c 6f 22 0a |puts "Hello".|
0000000d
```
Similar output can be achieved via Far Manager: select the file, press F3, and then toggle hex view with F4:
![Raw contents of app.rb (Far Manager view)](images/016-raw-file.png)
But dealing with raw `cat` command is hard. Once you pressed Enter, there is no way to go back and make changes. You can't use left and right arrow keys to navigate your program. And nobody is using `cat` command for writing programs (except maybe for the purpose of showing your excellence on interview).
There are many convenient code editors that were designed to edit code and help a programmer. If editor is simple, it can be used for both editing text and code. Nano is one of the easiest code/text editors and installed on almost any operating system (except Windows):
```
$ nano app.rb
```
![Nano editor, works right in your terminal](images/016-nano.png)
Footer has hints for available commands. For example, you can quit Nano by pressing Ctrl+X. There are much more advanced code editors like Vim and Emacs. Unfortunately, more advanced editors require more time to capture their functionality. You will probably see many "holy wars" on Internet about picking the right code editor. However, picking the right editor itself won't help you acquiring computer language knowledge and programming skills. So it's not very important which code editor you'll be using.
Code editors like cat, nano, vim, emacs are all _console_ editors. It means you run the command from your terminal and you don't need to quit the terminal. It's just a text on your screen. They're quite useful when you work with remote machines. For example, you've logged in to the server and want to edit or view some files. You can do that without quitting the terminal.
But on your own machine you're free to run any programs. And there are some code editors with graphic elements (like icons, different font styles, colors, and so on). Often these editors offer much more useful features (like live error checking, debugging, bookmarks) and make a life of a programmer much easier:
* [VsCode](https://code.visualstudio.com/), Visual Studio Code (don't get confused with "Visual Studio")
* [RubyMine](https://www.jetbrains.com/ruby/) (requires subscription)
* [Atom](https://atom.io/)
* [Sublime Text](https://www.sublimetext.com/) (fixed price)
RubyMine is not just a code editor, but Interactive Development Environment (IDE). Comprehensive tool for writing code with many features. Beginners can pick any of these editors. RubyMine offers great support on first steps, offering hints and tips. Other editors can be configured to offer this kind of support as well. However, we won't be covering this topic in our book.
At the very beginning it's worth using built-in code/text editor in your file manager. For example, you can create new file in Far Manager with Shift+F4 combination. At later steps feel free to use any code editor you like.
Also, you should be able to run your code editors from console:
```
$ code . # Will open VsCode for current directory
$ code ~/tmp # Will open VsCode for tmp directory
```
But sometimes you need to integrate editor with your shell. In VsCode it's achievable with:
1. Cmd+P combination (or Ctrl+P on Linux/Windows). It will open up navigation bar.
2. Type `>` in navigation bar to switch from file navigation to settings navigation.
3. Type `shell` and select "Install code in path" menu item.
![Integrating VsCode with shell](images/016-vscode.png)
Atom editor can be configured the similar way, and can be executed from shell with `atom` command:
```
$ atom .
```
X> ## Exercise
X> Install text editor (VsCode if you don't have any preference). Try to create few files, like `1.txt`, `2.txt`, `3.txt` and so on. In every file put the name of a person you know. Check your results with file manager. Delete files by using text editor UI.