Text Editors

From Computer Science
Revision as of 17:41, 20 August 2022 by Jkinne (talk | contribs) (Created page with "You should be familiar with the basic commands for terminal-based text editors (those that you can use when using a terminal only; see CS Server - Terminal). At a minimum,...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

You should be familiar with the basic commands for terminal-based text editors (those that you can use when using a terminal only; see CS Server - Terminal). At a minimum, you should be able to use the following to do basic editing of text files: nano, vim, emacs. Below we give you the basics on how to use each.

You might prefer to use graphical text editors, which can be easier to start using and configure. The most popular graphical text editors often have more programming features installed and configured by default than the terminal-based text editors do.

When you have chosen which text editor you plan to use most, you can search online for commonly used shortcuts and other features (search for vim shortcuts, or vim tutorial, or vim cheat sheet).

Terminal-Based Text Editors

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 an editor that has more features that are useful for programming (vim, emacs, or one of the graphical text editors).

Nano - First Editor

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).

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

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. You can ask the internet for a list of shortcuts.

Graphical Text 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.