User Tools

Site Tools


other:emacs_doc

Using the emacs text editor

TODO

FIXME

  • Add a screenshot
  • One process, multiple windows (FileNew Frame), multiple buffers
  • Emacs server…
  • Define some concepts: frame, buffer, region
  • Automatic backup file
  • TAB key for auto-completion when opening files…
  • runs locally ⇒ no need for an X server
  • available on Linux/Win/Mac
  • highly customizable
  • splitting windows
  • WIN: configuring the Cascadia Code font

Installing emacs

Linux installation

Install the emacs package, if it is not already available

$ sudo apt install emacs
[sudo] password for your_login:
Reading package lists... Done
Building dependency tree
Reading state information... Done
emacs is already the newest version (1:26.3+1-1ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.

Mac installation

Install Aquamacs

Windows installation

Standalone emacs

This is the light and easy option

  • Install emacs with: winget install --id GNU.Emacs
  • If you need to work with remote files (and don't want your emacs to seemingly freeze and consume all the CPU)
    • Install PuTTY, and configure a session for the desired host, so that emacs can use the plink command to connect to remote servers (instead of Windows builtin ssh)
    • use the plink method instead of ssh in the file name: /plink:user@host:/path/to/file
    • Use exactly the same host name (for host) as you have configured in the Host Name field in Putty
      e.g. /plink:your_login@ssh1.lsce.ipsl.fr:/path/to/file
  • You can work with the default settings, but it is highly recommended to start with a nice configuration file

emacs inside WSL

This is the heavyweight option

Launching emacs

Linux launch

  • emacs some_text_file &
  • or just emacs &
  • FIXME error type if there is no X server, and option for forcing text mode (i.e NO GUI)

Windows launch

  • StartEEmacs

Mac launch

FIXME

Using (and learning) emacs

Use C-g (CTRL+g) to abort or interrupt something in emacs

e.g. When you are searching forward with C-s, type C-g to exit the search

All the common functions of emacs are available from the pull-down menus at the top, but the real power comes from knowing (a few of) the keyboard shortcuts!

  • Understanding keyboard shortcuts:
    • C-x C-c means CTRL-x followed by CTRL-c, and will exit emacs
      • Same result as using FileQuit, just faster :-)
    • C-s means CTRL-s, and will Search forward
      • Hint: look for Incremental Search in the Reference Card for more information on searching!
    • C-g means CTRL-G and will abort the current emacs command/action (e.g. exit a forward search started with C-s)
    • C-z will iconify the emacs window!
      • Use C-x u (CTRL-x followed by u) or EditUndo for undoing the previous action!
    • M-f means Alt-f (or Esc followed by f if the keyboard has no Alt key), and will Move forward a word
  • Bonus! Many emacs shortcuts can be used in the shell or in Python, and will also make it possible to use emacs in text mode (if you get lost on a remote server without graphics…)
    • e.g. C-A and C-E to go to the beginning and end of a line
  • Copy-Cut-Paste
    • With a mouse: the usual Linux copy/paste behavior works:
      • Select and Copy text (at the same time) with the left button
      • Paste text with the middle button
      • Delete/Cut text (requires some practice): left-click at the beginning of the text you want to cut, then double-right-click at the end of the text
  • The official Guided Tour of Emacs is a nice web page quickly detailing the major concepts (buffer, region, mark, …) and shortcuts
    You can read this page, and later find some time for following the builtin tutorial (details below)
  • Built-in emacs tutorial, highly recommended! Spend one hour in the tutorial, and save hundreds of hours in the rest of your life!
    • HelpEmacs Tutorial
    • or HelpEmacs Tutorial (choose language)…
  • Reference cards, available in several languages
    • The Reference Card and the Survival Card mostly have the same content, just formatted a bit differently. The Reference Card is easier to read.
    • Emacs 21 Reference Card annotated by JYP (most useful shortcuts for beginners): emacs_jyp.pdf

emacs configuration file

  • The configuration file is officially known as the Emacs Initialization File
  • The configuration file (and most of the emacs Packages) is written in Emacs Lisp
    • You can easily survive without knowing anything about lisp! Just don't forget opening and closing parentheses (( and )) when copy/pasting settings you find on the web
    • The settings changed with the Options menu appear at the end of the configuration file

Location of the configuration file

emacs knows where its configuration file is located, and you can just open it by typing C-x C-f ~/.emacs and then check how the Home folder (~/) has been expanded

Related emacs (configuration) files and folders (e.g. .emacs.d/ folder) are located in the same place as the main configuration file

  • Linux:
    ~/.emacs: i.e. .emacs (dot-emacs) file in the current user's Home directory
  • Windows:
    If you want to access the emacs configuration files and folders, you will need to be able to see hidden folders and files!
    • Windows style path: c:\Users\your_login\AppData\Roaming\.emacs
    • Linux style path: c:/Users/your_login/AppData/Roaming/.emacs
  • Mac
    FIXME

Example configuration file

Keyboard shortcuts defined by the example configuration file

  • C-x C-g: goto line
  • Shift Left_Arrow and Shift Right_Arrow: go to the beginning/end of a line
    Same behavior as C-a and C-e, and the Home and End keyboard keys
  • Shift Up_Arrow and Shift Down_Arrow: go to the beginning/end of the buffer
  • Function keys
    • F1: Undo (lots of undos!!)
    • F2: Search and Replace
    • F3: fill-paragraph (i.e. automatically re-indent text or comments)
    • F12: Repeat complex command (e.g. restart a previous query-replace operation)

Useful shortcuts

  • M-;: comment out selected lines
    This will automatically use the appropriate comment character for the current buffer mode, e.g. # for a Python script

Working with remote text files

Prerequisite

Using the tramp mode

  • If you use the syntax below when opening a file (C-x C-f), emacs will automatically use the tramp mode to:
    • copy the remote file (with scp) to your computer when you open it, so that you can edit the file locally
    • copy the edited text (with scp) file back to its original remote location when you save it (C-x C-s)
    • Remote file on a server that you can reach directly:
      • /ssh:user@server:/path/file
        or /plinkx:jdoe_server:/path/file on Windows with an existing jdoe_server session defined in PuTTY
      • e.g. /ssh:your_login@ssh1.lsce.ipsl.fr:/home/users/your_login/my_script.py
    • Remote file on a server than you can only access through a gateway (e.g. file in a supercomputing center):
  • Hint: if you use the recommended configuration file or have activated the '(recentf-mode t) mode, you can easily access recent files in FileOpen Recent without having to type the full access path again!

Troubleshooting...

The tramp mode is quite complex and relies on connections to one or more remote computers using external programs and protocols. You may have to experiment a bit till you find the correct syntax and inline method to work with a particular remote server. Make sure you have read the prerequisite section!

Buffer*Messages*

  • If you get stuck (emacs is not responding), there is pr
    • Try aborting the current emacs command by typing C-g

Having fun with emacs

  • Use the built-in psychotherapist to solve your scientific problems (with M-x doctor). See also Fun and Games in Emacs
    I am the psychotherapist.  Please, describe your problems.  Each time
    you are finished talking, type RET twice.
    
    My climate model does not work
    
    Why do you say your climate model does not work?
    
    I get wrong results
    
    Is it because of your plans that you say you get wrong results?

emacs documentation





[ PMIP3 Wiki Home ] - [ Help! ] - [ Wiki syntax ]

other/emacs_doc.txt · Last modified: 2023/01/04 16:43 by jypeter