Difference between revisions of "Text Editors"

From Computer Science
Jump to: navigation, search
(Terminal Editors)
Line 4: Line 4:
  
 
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).
 
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).
 +
 +
Video demonstration of text editors in the terminal - [https://cs.indstate.edu/info/videos/text_editors_in_terminal.mp4 nano, vim, emacs editors in the terminal]
  
 
=Terminal-Based Text Editors=
 
=Terminal-Based Text Editors=

Revision as of 18:53, 24 August 2022

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 should know the basics each because you are likely to run across a situation where a program in the terminal is configured to use one of these for its interface (which you won't be able to change while in the middle of some configuration process).

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

Video demonstration of text editors in the terminal - nano, vim, emacs editors in the terminal

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 of the shortcuts listed for emacs above also work for Jove. The command on the CS server is jove. Note that Jove is not installed by default on all Linux and Mac OS systems, so you would likely need to install it on your personal computer if you want to use it there. You can ask the internet for a list of shortcuts.

Graphical Text Editors

Graphical / GUI editors can be easier to use because you can point-and-click on menus rather than needing to remember short-cut keys. Some graphical 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.

Note that while you are using a terminal that is connected to the CS server you can only use terminal-based text editors (those in the section above). vim and emacs both have plenty of feaatures for programmers, but it is good to try out some of the graphical text editors as well.

The following are good options for graphical text editors that are cross-platform - can be installed on Mac, Windows, Linux, and Chromebook. If you already use some other option (e.g., Eclipse, Notepad++, or anything else that is used by programmers) feel free to keep using what you are already using. Note that Word is not a text editor - it does not store files in plain text, which is what is needed for programming.

  • Kate - make sure to download the "release" installer.
  • Submlime - used by Luke May (as of 2022).
  • Visual Studio Code (aka VS Code) - used by Jeff Kinne (as of 2022).
  • Emacs - used by Jeff Kinne (since, forever), click "GNU/Linux", "Windows", or "MacOS" depending on your OS and follow the instructions.
    • For Windows, download is from https://ftp.gnu.org/gnu/emacs/windows/, choose the latest emacs-## folder to click on, then download the zip file at the bottom that ends in "-x86_64.zip", extract the zip to somewhere to keep the program (e.g., C:\Program Files\), then run the file runemacs.exe in the bin directory.
    • For macOS, the easiest install is from https://emacsformacosx.com/
  • Vim - used by Geoff Exoo (since, forever). For Windows, download and run the "self-installing executable". For macOS download and run MacVim.dmg from https://github.com/macvim-dev/macvim/releases/latest

Assignment

Terminal Editors

For the Terminal Editors assignment, you should demonstrate that you are able to use all three of the text editors mentioned above (nano, vim, emacs). You will be connected to the CS server with a terminal, and use each of the editors in turn to edit a file. You need to demonstrate that you are able to open, edit to add some text, save, and close the file.

Your instructor might ask you to visit the CS lab to demonstrate that you are able to do this. The lab assistant will confirm the following.

Pass rating check To earn a pass rating, you must demonstrate to the lab assistant the following.

  • You are connected to the CS server in a terminal.
  • For each of the editors (nano, vim, emacs), you open a text file, edit it, save it, and close.
  • You must also have a cheat sheet showing the shortcuts or commands that are needed for each of the editors to do this. This can be in your notes or something separate.

Note - the shared spreadsheet that GAs use for submitting information to Jeff Kinne's courses is this link, which should work only for the current term's GAs.