Difference between revisions of "Linux Terminal - Text Editors"
(→Vim) |
(→Emacs) |
||
(12 intermediate revisions by the same user not shown) | |||
Line 15: | Line 15: | ||
Now type some text, for example "Hello World". You can save your file by holding down control and pressing o. When you do this, nano prompts you to ask what the name of the file should be; you can leave it alone as hello.txt and press enter. Your file is now saved! You can type more into your hello.txt file if you wish. Use the arrow keys on your keyboard to move around inside of your text file. When you are done editing, use control-o to write out (save) your file, and then use control-x to exit nano. | Now type some text, for example "Hello World". You can save your file by holding down control and pressing o. When you do this, nano prompts you to ask what the name of the file should be; you can leave it alone as hello.txt and press enter. Your file is now saved! You can type more into your hello.txt file if you wish. Use the arrow keys on your keyboard to move around inside of your text file. When you are done editing, use control-o to write out (save) your file, and then use control-x to exit nano. | ||
+ | |||
+ | The following is how to get around in the <code>nano</code> editor. | ||
+ | * arrow keys - are used to move around inside your text file. | ||
+ | * enter or return - move to the next line / create a new line. | ||
+ | * ctrl-x - exit nano. | ||
+ | * ctrl-o - save the file (write Out). | ||
=Text-based Editors= | =Text-based Editors= | ||
Line 37: | Line 43: | ||
==Emacs== | ==Emacs== | ||
− | Emacs is a text-based editor that is normally installed on most linux systems. It can also be downloaded for Windows | + | Emacs is a text-based editor that is normally installed on most linux systems and on Mac OS X. It can also be downloaded for Windows. |
===The Basics=== | ===The Basics=== | ||
Line 84: | Line 90: | ||
==Jove== | ==Jove== | ||
− | + | Ask the internet for a cheat sheet or tutorial. Jove was created to be similar to emacs in its UI but with a much smaller program file, so many/most of the shortcuts listed for emacs above also work for Jove. The command on the CS server is <code>jove</code>. | |
− | + | ||
− | + | ==Sample Quiz== | |
+ | ''An example quiz over this material. After watching the video and trying the commands out yourself to make sure you understand.'' | ||
+ | |||
+ | Fill in the blank. | ||
+ | * In the nano editor - | ||
+ | ** Shortcut to exit: | ||
+ | ** Shortcut to write out: | ||
+ | * In the emacs editor - | ||
+ | ** Shortcut to exit: | ||
+ | ** Shortcut to save: | ||
+ | * In the vim editor - | ||
+ | ** Type this to enter insert mode: | ||
+ | ** Type this to leave insert mode: | ||
+ | ** Type this to quit: | ||
+ | ** Type this to write out: | ||
+ | |||
+ | ==Sample HW== | ||
+ | The following might be used by your instructor as part of a HW assignment to verify you have tried each of these editors. | ||
+ | |||
+ | Create a directory to store files for this HW. If you are given this assignment in one of your courses you will be told which directory to use. We refer to this directory as HW_dir. | ||
+ | * Open your terminal and browse to the directory /u1/junk | ||
+ | # Use the editor nano to open shakespeare.txt and take a screenshot of your terminal with nano open. Save the file as nano-shakespeare.jpg and use a file transfer program to transfer from your computer into your HW_dir in your account on the server. | ||
+ | # Exit nano. Use the editor emacs to open shakespeare.txt and take a screenshot of your terminal with emacs open. Save the file as emacs-shakespeare.jpg and use a file transfer program to transfer from your computer into your HW_dir in your account on the server. | ||
+ | # Exit emacs. Use the editor vim to open shakespeare.txt and take a screenshot of your terminal with vim open. Save the file as vim-shakespeare.jpg and use a file transfer program to transfer from your computer into your HW_dir in your account on the server. | ||
=GUI Editors= | =GUI Editors= | ||
− | GUI / graphical editors can be easier to use because you can point-and-click on menus rather than needing to remember short-cut keys. Some GUI editors also have other features that are nice (auto-completing some parts of your code if the right plug-in is installed, being able to run your code from within the editor, etc.). Some of those features are also available in text-based editors (vim and emacs in particular) as well, but rely on yet-more-shortcuts. | + | GUI / graphical editors can be easier to use because you can point-and-click on menus rather than needing to remember short-cut keys. Some GUI editors also have other features that are nice (auto-completing some parts of your code if the right plug-in is installed, being able to run your code from within the editor, etc.). Some of those features are also available in text-based editors (vim and emacs in particular) as well, but rely on yet-more-shortcuts. |
− | + | [[Linux - System Setup]] has information about installing some of the most popular/common GUI text editors. | |
− | |||
− |
Latest revision as of 17:17, 20 August 2022
This page is a part of the Linux and CS Systems - Getting Started. This page assumes you have your computer setup to connect to the CS server, or have the appropriate software installed on your computer to run commands. Go back to the Linux and CS Systems Getting Started main page if you don't have our system setup yet.
While you are logged into the CS lab machines or CS server, you need to use a text editor to edit files. When you are first getting started, you can use nano to edit files. nano is relatively easy to use and shows on the screen what control keys need to be pressed to do different things (e.g., ctrl-x to exit). Once you are comfortable using the shell/terminal and nano, you should transition to using one of the editors highlighted below (vim, emacs, atom, kate) which have more features to make you more productive.
Contents
First Editor - Nano
One thing programmers need to do is edit text files. The text files could be your programs, or they could be data files. There are many different text editors that are normally installed on Linux systems. Some of the most popular/common are - vim, emacs, nano.
nano is among the easiest text editors to use, so let's get you started using nano. You first need to be logged into Linux and open a terminal. Type the following
nano hello.txt
You will now be running the nano editor and editing a file named hello.txt. Note that the interface is completely text-based. There is nothing for you to click on. Instead of clicking on menus, you have control codes that you can type to save, close, etc. The bottom of the screen lists the control codes that you can use. Each control code is typed by holding down the control key on your keyboard and then pressing a letter.
For example, hold down control and press x to Exit nano. You can type nano hello.txt
again to open the nano editor again.
Now type some text, for example "Hello World". You can save your file by holding down control and pressing o. When you do this, nano prompts you to ask what the name of the file should be; you can leave it alone as hello.txt and press enter. Your file is now saved! You can type more into your hello.txt file if you wish. Use the arrow keys on your keyboard to move around inside of your text file. When you are done editing, use control-o to write out (save) your file, and then use control-x to exit nano.
The following is how to get around in the nano
editor.
- arrow keys - are used to move around inside your text file.
- enter or return - move to the next line / create a new line.
- ctrl-x - exit nano.
- ctrl-o - save the file (write Out).
Text-based Editors
There are a variety of other text-based editors that are normally installed on most Linux (and Mac) systems, and can be used when logged in to the CS server. With all of these, since they are text-based you type keyboard shortcuts to open and save, search, etc. For each of the text-based editors below we give enough shortcuts to get started, and you can search online for more (e.g., search for vim shortcuts, or vim tutorial, or vim cheat sheet).
Vim
Minimal set of commands/info to get started.
- Vim normally opens in "normal mode", where you can use the arrow keys on your keyboard to navigate your text file but where typing text does not get inserted into the file.
- To change the text of the file you normally enter "insert mode" by simply pressing i. In insert mode, typing text puts it into the file, you can use the arrow keys to navigate the file, and backspace works. To exit insert mode, press ESC.
- To save your file, make sure you are not in insert mode (press ESC if so) and type
:w
and then enter.:w
writes your changes to the file. - To quit vim, make sure you are not in insert mode (press ESC if so) and type
:q
and then enter. - When you are in "normal mode" you really need to be careful what you type. Make sure to only use the arrow keys and then press
i
to enter insert mode to type text. You need to be careful because when you are in normal mode, any text you type will be checked against vim's list of commands. The following are commonly used commands (type any of these while in "normal mode" to try them out).dd
- delete the current line of text.0
- move to beginning of the line.$
- move to end of the line./
- performs a search for the text typed next (e.g., /hello and then enter, to search for next occurrence of hello).n
- repeats last search (e.g., /hello and then enter, to search for hello, then n to search for next hello).G
- goto last line of the file.1G
- goto first line of file (home).
For more, ask the internet for a Vim cheat sheet or Vim tutorial.
Emacs
Emacs is a text-based editor that is normally installed on most linux systems and on Mac OS X. It can also be downloaded for Windows.
The Basics
When you open a file in emacs, you are editing it much the same as you would with Notepad or Word (but you cannot use your mouse if you are using it in the terminal). You use the arrow keys to move around in the file, type to add to the file, and use backspace (aka delete on Mac) to delete.
When using emacs in the terminal there is no menu to click on. You use keyboard shortcuts to save, close, etc. Most of the basic shortcuts are listed like this in the list below: ctrl-s. That means to hold down the control key and press s. Some of the shortcuts are listed like this: ESC x hexl-mode. That means to press (and release) the ESC key, then press (and release) the x key, then type hexl-mode, then press enter (aka return on Mac).
The most important shortcuts for starting out in Emacs are...
- Save file: ctrl-x ctrl-s
- Close/exit: ctrl-x ctrl-c
- Undo: ctrl-shift-_ (and press repeatedly to undo more things)
- Auto-indent current line: tab
- Search for string: ctrl-s to search forward in the file, ctrl-r to search reverse in the file.
- Repeat last search: ctrl-s repeatedly
More Commands
- If more than one file is open, switch between them: ctrl-x b
- Switch to "split screen" mode: ctrl-x 2
- Switch windows in split screen mode: ctrl-x o
- Switch back to "normal screen" mode: ctrl-x 1
- Open shell/terminal: esc x shell
- Delete character just right of cursor: ctrl-d
- Kill from cursor to end of line: ctrl-k
- Go to the end of the line: ctrl-e
- Go to end of file: esc <
- Go to begin of file: esc >
- Skip to end of current statement: esc C-f. When editing C/C++ files, skips to the end of the current statement - which will skip past a { ... } block if the cursor is in the right place.
- Skip to beginning of current statement: esc C-b. Like last one, but goes backwards in the file.
- Copy: ctrl-spacebar to set beginning of copy region, then go to where you want the end of the copy region, then type esc w.
- Yank/paste what was just copy/killed: ctrl-y
- Cycle through yank buffer: after a ctrl-y, type esc y repeatedly to cycle through things that have been copy/killed.
- Create new file or open existing one: ctrl-x ctrl-f
- Close file but don't close emacs: ctrl-x k
- Search/replace: esc x replace-string
- Spell-check: (not used much on programming files, but anyway) - esc x ispell
- View raw binary/hex data: esc x hexl-mode.
- Switch back to "normal" mode: Instead of hexl-mode, use one of the following depending on the type of file - c-mode, text-mode, html-mode.
- Esc x recover-this-file: If emacs was killed while you were editing (because of, for example, putty getting closed), then you can type this to try to recover what you had worked on.
For more - see the internet, there is a lot you can do in emacs.
Jove
Ask the internet for a cheat sheet or tutorial. Jove was created to be similar to emacs in its UI but with a much smaller program file, so many/most of the shortcuts listed for emacs above also work for Jove. The command on the CS server is jove
.
Sample Quiz
An example quiz over this material. After watching the video and trying the commands out yourself to make sure you understand.
Fill in the blank.
- In the nano editor -
- Shortcut to exit:
- Shortcut to write out:
- In the emacs editor -
- Shortcut to exit:
- Shortcut to save:
- In the vim editor -
- Type this to enter insert mode:
- Type this to leave insert mode:
- Type this to quit:
- Type this to write out:
Sample HW
The following might be used by your instructor as part of a HW assignment to verify you have tried each of these editors.
Create a directory to store files for this HW. If you are given this assignment in one of your courses you will be told which directory to use. We refer to this directory as HW_dir.
- Open your terminal and browse to the directory /u1/junk
- Use the editor nano to open shakespeare.txt and take a screenshot of your terminal with nano open. Save the file as nano-shakespeare.jpg and use a file transfer program to transfer from your computer into your HW_dir in your account on the server.
- Exit nano. Use the editor emacs to open shakespeare.txt and take a screenshot of your terminal with emacs open. Save the file as emacs-shakespeare.jpg and use a file transfer program to transfer from your computer into your HW_dir in your account on the server.
- Exit emacs. Use the editor vim to open shakespeare.txt and take a screenshot of your terminal with vim open. Save the file as vim-shakespeare.jpg and use a file transfer program to transfer from your computer into your HW_dir in your account on the server.
GUI Editors
GUI / graphical editors can be easier to use because you can point-and-click on menus rather than needing to remember short-cut keys. Some GUI editors also have other features that are nice (auto-completing some parts of your code if the right plug-in is installed, being able to run your code from within the editor, etc.). Some of those features are also available in text-based editors (vim and emacs in particular) as well, but rely on yet-more-shortcuts.
Linux - System Setup has information about installing some of the most popular/common GUI text editors.