Linux Terminal - Text Editors

From Computer Science
Revision as of 04:38, 13 January 2020 by Jkinne (talk | contribs) (First Editor - Nano)
Jump to: navigation, search

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.

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.

  • [video demo of nano]

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

Basic shortcuts

  • coming soon

[video demo]

Emacs

Emacs is a text-based editor that is normally installed on most linux systems. It can also be downloaded for Windows and Mac OS X.

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.

[video demo coming soon]

Jove

Basic shortcuts

  • coming soon

[video demo]

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. The following are a few graphical editors which are available on the CS lab machines; some of these can also be installed on Windows and Mac.

  • Atom - download video demos ([on cs] [on Windows] [on Mac])
  • Emacs - also has a GUI interface - download video demos ([on cs] [on Windows] [on Mac])
  • Kate - download video demos' ([on cs] [on Windows] [on mac])