Linux Terminal - Files and Directories and Getting Around and Linux Terminal - Text Editors: Difference between pages

From Computer Science at Indiana State University
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
''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.''
''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.''


'''Basic idea''' The linux terminal is used to type commands to control your computer and files. Everything that you can do graphically on your computer (in Windows with File Explorer, on MacOS with Finder) you can do by typing commands instead.
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.


'''Demo''' See [https://www.youtube.com/watch?v=ynETRS3KGGs&feature=youtu.be this demo video] for a video demonstration of the topics on this page, including running the commands on the ISU CS serverYou should start reading through this page before watching the video.
=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 systemsSome of the most popular/common are - vim, emacs, nano.


=Basic Commands=
nano is among the easiest text editors to use, so let's get you started using nanoYou first need to be logged into Linux and open a terminal.  Type the following
With the terminal you can create, copy, move, and delete files and directories.  The following are the most important commands to do this.
* cd - change directory
* mkdir - make a new directory
* ls - list directory contents (''use option -l to see details'')
* cp - copy files
* rm - remove files (be careful, there is no recycle bin or trash - once you delete, it's gone)
* rmdir - remove directory
* mv - move or rename a file or directory
* nano - simple text editor
 
=Getting Around=
To "get around" in the terminal, use the following keys.
* enter - a command is only executed when you press enter.
* up and down arrows - lets you run a previously typed command again.
* tab - if you are typing a filename, you can press tab to let the terminal auto-complete some part of itThis also works for command names (e.g., type mkdi and then tab).
 
The following are shortcuts for directories.
* . - current directory
* .. - directory one level up from where you are currently
* ~ - your home directory
* / - the top of the entire file system
 
The following are special notes about directory or file names.
* "" - if you have a file name with spaces in it, then you put the file name within "".  So you could do <code>mkdir "Some Directory"</code>
* * - called a wildcard, used to specify all file names that match some pattern.  To list all files that end in ".pdf" you could type <code>ls *.pdf</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, what is the shortcut or key used in the linux terminal for each of the following.
* Press this key to execute a command:
* Keys that let you run a previously typed command again:
* Key to press to auto-complete a filename or command:
* Shortcut for current directory (one character):
* Shortcut for directory one level up from current:
* Shortcut for your home directory (one character):
* Shortcut for the top of the entire file system (one character):
* Characters to put around a filename that has spaces:
* Wildcard character:
 
Fill in the blank, what is the linux command to do each of the following.
* Change directory:
* Make a new directory:
* List directory contents:
* Flag/option to use with previous command to list details of files:
* Copy file(s):
* Remove files:
* Remove directory:
* Move or rename a file or directory:
* A terminal text editor:
 
Give the complete linux terminal command to complete each of the following tasks.  ''Tasks such as these could be asked about.''
* Create a new directory HELLO in your home directory:
* Copy the contents of jkinne's GREAT directory into your home directory:
* Change directory to your home directory:
* Check to see who is logged in:
* List the contest of the directory ~cs151/ including the sizes of the files:
 
=Sample HW=
The following might be used by your instructor as part of a HW assignment to verify that you are able to use the terminal to create and copy files and view the contents of directories.
 
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.
# Copy /var/junk/shakespeare.txt into your HW_dir.
# Copy /proc/cpuinfo into your HW_dir, but make the file name cpuinfo-HW.txt
# Copy /proc/version into your HW_dir, but make the file name version-HW.txt
# Create a file info.txt in your HW_dir.  Inside the file fill in the following information.
#* Name (what you want to be called):
#* Why you are taking this course:
#* Anything you are worried about (wrt this course):
#* Most excited about (wrt this course):
# In the terminal, browse to the directory /net/web, run the command <code>ls -l cs151</code>, and copy the output of that commandGo back to your HW_dir, edit your info.txt file, and paste into the file.
# Copy ~cs151/hello.txt into your HW_dir
 
=Sample Session=
The following is a transcript of the commands and screen output from the youtube video linked at the top of this page.  Blank lines have been put in after each command.  You can read through the transcript and think about what you think the output will be after each command is entered, and check that this is how it shows in the transcript. 
 
Note that a few text files have been edited using the nano text editor.  When the nano text editor was used the editing of the file itself is not part of the transcript.  Note that the transcript is for the user jkinne@isuad@indstate.edu logged in to the CS server.
<pre>
<pre>
cs15100@cs:/u1/class/cs15100> ls
nano hello.txt
Desktop/ proto/
</pre>
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.  


cs15100@cs:/u1/class/cs15100> ls -l
For example, hold down control and press x to Exit nano. You can type <code>nano hello.txt</code> again to open the nano editor again.  
total 8
drwxr-xr-x 2 cs15100 class 4096 Jan 8 21:17 Desktop/
drwxr-xr-x 2 cs15100 class 4096 Jan 9 12:35 proto/


cs15100@cs:/u1/class/cs15100> cd Desktop
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.


cs15100@cs:/u1/class/cs15100/Desktop> ls
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).


cs15100@cs:/u1/class/cs15100/Desktop> cd ..
=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).


cs15100@cs:/u1/class/cs15100> mkdir hello
==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 <code>:w</code> and then enter.  <code>:w</code> writes your changes to the file.
* To quit vim, make sure you are not in insert mode (press ESC if so) and type <code>:q</code> 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 <code>i</code> 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).
** <code>dd</code> - delete the current line of text.
** <code>0</code> - move to beginning of the line.
** <code>$</code> - move to end of the line.
** <code>/</code> - performs a search for the text typed next (e.g., /hello and then enter, to search for next occurrence of hello).
** <code>n</code> - repeats last search (e.g., /hello and then enter, to search for hello, then n to search for next hello).
** <code>G</code> - goto last line of the file.
** <code>1G</code> - goto first line of file (home).


cs15100@cs:/u1/class/cs15100> ls
For more, ask the internet for a Vim cheat sheet or Vim tutorial.
Desktop/  hello/  proto/


cs15100@cs:/u1/class/cs15100> ls
==Emacs==
Desktop/ hello/ proto/
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.  


cs15100@cs:/u1/class/cs15100> nano hello.txt
===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. 


cs15100@cs:/u1/class/cs15100> ls
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-modeThat 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).
Desktop/ hello/ hello.txt proto/


cs15100@cs:/u1/class/cs15100> ls -l
The most important shortcuts for starting out in Emacs are...
total 16
* ''Save file:'' ctrl-x ctrl-s
drwxr-xr-x 2 cs15100 class 4096 Jan  8 21:17 Desktop/
* ''Close/exit:'' ctrl-x ctrl-c
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:37 hello/
* ''Undo:'' ctrl-shift-_ (and press repeatedly to undo more things)
-rw-r--r-- 1 cs15100 class  60 Jan 10 11:43 hello.txt
* ''Auto-indent current line:'' tab
drwxr-xr-x 2 cs15100 class 4096 Jan  9 12:35 proto/
* ''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


cs15100@cs:/u1/class/cs15100> mv hello.txt Desktop
===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


cs15100@cs:/u1/class/cs15100> ls
* ''Delete character just right of cursor:'' ctrl-d
Desktop/  hello/  proto/
* ''Kill from cursor to end of line:'' ctrl-k


cs15100@cs:/u1/class/cs15100> ls Desktop
* ''Go to the end of the line:'' ctrl-e
hello.txt
* ''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.


cs15100@cs:/u1/class/cs15100> ls
* ''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.
Desktop/  hello/  proto/
* ''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.


cs15100@cs:/u1/class/cs15100> ls /
* ''Create new file or open existing one:'' ctrl-x ctrl-f
bin/  dev/  home/  lib64/      media/  net/  proc/  run/  srv/    swap  tmp/  u2/  var/
* ''Close file but don't close emacs:'' ctrl-x k
boot/  etc/  lib/  lost+found/  mnt/    opt/  root/  sbin/  store/  sys/  u1/  usr/  xsys/


cs15100@cs:/u1/class/cs15100> cd Desktop
* ''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.


cs15100@cs:/u1/class/cs15100/Desktop> ls /u1/class/cs15100
''For more'' - see the internet, there is a lot you can do in emacs.
Desktop/  hello/  proto/


cs15100@cs:/u1/class/cs15100/Desktop> ls ~
==Jove==
Desktophello/  proto/
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>.


cs15100@cs:/u1/class/cs15100/Desktop> cd /u1/junk/kinne
==Sample Quiz==
 
''An example quiz over this materialAfter watching the video and trying the commands out yourself to make sure you understand.''
cs15100@cs:/u1/junk/kinne> ls
cs151Hello            Indianapolis-Weather-Station-USW00093819-1950-2018.csv  shakespeare_1000_lines.txt
GRCh38.p12.genome.fa  interview-questions/                                    shakespeare.txt
hello.txt            shakespeare_1000_lines_tail.txt                        Terre_Haute_airport_93823-03868.csv
 
cs15100@cs:/u1/junk/kinne> ls ~
Desktop/  hello/  proto/
 
cs15100@cs:/u1/junk/kinne> ls
cs151Hello            Indianapolis-Weather-Station-USW00093819-1950-2018.csv  shakespeare_1000_lines.txt
GRCh38.p12.genome.fa  interview-questions/                                    shakespeare.txt
hello.txt            shakespeare_1000_lines_tail.txt                        Terre_Haute_airport_93823-03868.csv
 
cs15100@cs:/u1/junk/kinne> cp cs151Hello ~
 
cs15100@cs:/u1/junk/kinne> cd ~
 
cs15100@cs:/u1/class/cs15100> ls
cs151Hello  Desktop/  hello/  proto/
 
cs15100@cs:/u1/class/cs15100> ls -l
total 16
-rw-r--r-- 1 cs15100 class  16 Jan 10 11:46 cs151Hello
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:44 Desktop/
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:37 hello/
drwxr-xr-x 2 cs15100 class 4096 Jan  9 12:35 proto/
 
cs15100@cs:/u1/class/cs15100> nano cs151Hello
 
cs15100@cs:/u1/class/cs15100> ls
cs151Hello  Desktop/  hello/  proto/
 
cs15100@cs:/u1/class/cs15100> rm cs151Hello
rm: remove regular file 'cs151Hello'? y
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello/  proto/
 
cs15100@cs:/u1/class/cs15100> rmdir hello
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  proto/
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  proto/
 
cs15100@cs:/u1/class/cs15100> cd Desktop/
 
cs15100@cs:/u1/class/cs15100/Desktop> ls
hello.txt
 
cs15100@cs:/u1/class/cs15100/Desktop> mv hello.txt ..
 
cs15100@cs:/u1/class/cs15100/Desktop> ls
 
cs15100@cs:/u1/class/cs15100/Desktop> cd ..
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello.txt  proto/
 
cs15100@cs:/u1/class/cs15100> mv hello.txt helloThere.txt
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt  proto/
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt proto/
 
cs15100@cs:/u1/class/cs15100> mv hello.txt helloThere.txt
mv: cannot stat 'hello.txt': No such file or directory
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt  proto/
 
cs15100@cs:/u1/class/cs15100> cp helloThere.txt hello.txt
 
cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt  hello.txt  proto/
 
cs15100@cs:/u1/class/cs15100> nano helloThere.txt
 
cs15100@cs:/u1/class/cs15100> cd Desktop
 
cs15100@cs:/u1/class/cs15100/Desktop> ls
 
cs15100@cs:/u1/class/cs15100/Desktop> ls .
 
jkinne@isuad@indstate.edu@cs:/u1/class/jkinne@isuad@indstate.edu/Desktop> cp ../helloThere.txt .


cs15100@cs:/u1/class/cs15100/Desktop> ls
Fill in the blank.
helloThere.txt
* 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:


cs15100@cs:/u1/class/cs15100/Desktop> cp /u1/class/cs15100/helloThere.txt /u1/class/cs15100/Desktop/
==Sample HW==
cp: overwrite '/u1/class/cs15100/Desktop/helloThere.txt'? n
The following might be used by your instructor as part of a HW assignment to verify you have tried each of these editors.


cs15100@cs:/u1/class/cs15100/Desktop> cd ..
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 /var/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.


cs15100@cs:/u1/class/cs15100> nano hello there.txt
=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.


cs15100@cs:/u1/class/cs15100> nano "hello there.txt"
[[Linux - System Setup]] has information about installing some of the most popular/common GUI text editors.
 
cs15100@cs:/u1/class/cs15100> ls -l
total 20
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:51 Desktop/
-rw-r--r-- 1 cs15100 class    9 Jan 10 11:52 hello\ there.txt
-rw-r--r-- 1 cs15100 class  60 Jan 10 11:43 helloThere.txt
-rw-r--r-- 1 cs15100 class  60 Jan 10 11:50 hello.txt
drwxr-xr-x 2 cs15100 class 4096 Jan  9 12:35 proto/
</pre>

Latest revision as of 12:17, 29 September 2025

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.

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 /var/junk
  1. 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.
  2. 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.
  3. 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.