1.3. テキストエディタ/Text Editors

1.3.1. テキストエディタとIDE/Text editors and IDEs

ソースコードの編集には、(単機能の)テキストエディタを使うか、IDE (Integrated Development Environment)と呼ぶタイプのGUIアプリケーションを使います。

IDEは大変便利であり、利用できる環境であればぜひ使うべきです。 IDEを使えば、様々な開発用コマンドは、全てIDEがやってくれます。 しかし、IDEだけを使っていると、個々の開発ツールを直接使う方法が身につきません。

スパコンの場合には、IDEの使えない環境での作業が生じるので、IDEではなく、 テキストエディタはマスターしておく必要があります。 IDEに頼らずに個々のツールを使う方法を身に着けておくと、コンパイル作業や プログラムの定型的なテストなどの作業を自動化することもできるようになります。

本格的なプログラム開発を目指すのであれば、一回はIDEなしでの開発を経験して おくことを薦めます。

To edit source code, you can use a uni-functional text editor, or an IDE.

IDEs are extremely convenient. You should try to use one whenever possible. IDEs will invoke all the necessary commands for program development. However, if you rely totally on IDEs, you will not be learning how to use the individual commands by themselves.

For supercomputers, there are situations where you cannot use an IDE, so you need to at least be familiar with text editors. If you learn how to use the individual tools directly, you will also become able to automate common programming tasks such as compiling and testing.

If you are aiming for substantial program development, we recommend you to experience using the individual tools directly, at least once.

1.3.2. 主なテキストエディタ/major text editors

テキストエディタにはいろいろな種類があります。 代表的なものを紹介します。

There are various text editors. A few of the famous ones are the following:

nano

簡単に使えるように機能を抑えたエディタです。 簡単である反面、便利な機能は提供しておらず、作業効率はあまり高くありません。

An easy to learn editor, with limited functionality. It is easy to start using, but lacks powerful features, and is not very productive in the long run.

vi (“vee-eye”)

古くからあるエディタ。使いはじめるだけでもある程度の勉強を必要としますが、 慣れると素早い編集ができ、ベテランプログラマにもファンはたくさんいます。

A very old editor. A certain amount of learning is required up front just to start using, but it allows for swift editing once you get used to it. There are many fans in experienced programmers.

emacs (“ee-max”)

lispで実装された、高度なカスタマイズが可能なエディタ。 viにも増して勉強を必要とします。 独力での入門は難しいでしよう。近くに質問できる人がいると心強いです。

A highly customizable editor that is implemented by lisp. Even more learning than vi is required. It is very helpful to have someone you can ask questions when you learn.

エディタをすぐに使い始めたいのであれば、nanoをお勧めします。 以下にそれぞれのエディタの最小限の使い方を説明します。

If you want to start using an editor quickly, we recommend the nano editor. A minimal introduction to these editors follow.

1.3.3. nanoエディタ初歩/nano editor primer

ファイル名を指定して起動する:

Invoke the editor giving a file name:

$ nano file_name

行番号を「+」オプションで指定すれば、その行にカーソルが置かれた状態でエディタが開く:

You can specify the line number to jump to with a ‘+’ after the file name:

$ nano my_program.c +105

起動すると、画面の下の部分にキー操作のヘルプが表示されます。:

^G ヘルプ    ^O Write Out ^W 移動      ^K 切り取り  ^J 両端揃え  ^C 位置
^X 終了      ^R 読み込み  ^\ 置換      ^U Uncut Text^T スペル確認^_ 行を指定

この表示で、「^」はControlキーを意味します。例えば「^G」は “Control g” のことであり、ヘルプが表示されます。

When invoked, a brief key assignment table will be shown at the bottom of the screen:

^G Get Help  ^O Write Out ^W Where Is  ^K Cut Text  ^J Justify   ^C Cur Pos
^X Exit      ^R Read File ^\ Replace   ^U Uncut Text^T To Spell  ^_ Go To Line

In this table, “^” denotes the control key. For example, “^G” is “control g” and has the function “Get Help”.

特定の変数名や関数名の場所まで移動するには Ctrl-w で検索を使うと速く移動できます。

To quickly move the cursor to some variable name or function name, Ctrl-w “Where Is”, or searching, is helpful.

編集を終了するには、Ctrl-xを入力します。 保存がまだであれば、その時点で保存を確認されます:

変更されたバッファを保存しますか?("No" と答えると変更は破棄されます。)
 Y はい
 N いいえ       ^C 取消

この時の画面が、ゴチャっとしていて、そのせいかあまりよく読まずに操作をする人をよく見かけます。 画面に出ているメッセージをよく読んでから、操作しましょう。

To exit the editor, type Ctrl-x. If you haven’t saved the buffer yet, you will be asked if you want to save at this point:

Save modified buffer?  (Answering "No" will DISCARD changes.)
 Y Yes
 N No           ^C Cancel

The prompt at this point is somewhat cluttered. Maybe for that reason, I often see students hitting a key without fully reading what is being asked. Read the words on the screen before deciding your action.

1.3.4. viエディタ初歩/vi editor primer

viは、ファイル名を指定して起動する。:

Invoke the editor giving a file name:

$ vi file_name

nanoと同様に、行番号を指定することもできる。:

Just like nano, you can give a line number to place the cursor:

$ vi my_source.cpp +125

1.3.4.1. viのモード/modes in vi

viには、「コマンドモード」と「挿入モード」と呼ぶ2つの状態があり、両者を切り替え ながら使う。一般のエディタでは、例えばカーソル移動にはカーソルキーを使い、通常の “a”,”b”,”c”などのキーを打鍵すれば、そのまま”a”,”b”,”c”が入力される。ところが、 viの場合は、コマンドモードでは一般のキーにカーソル移動などの機能が割り当てられている。 カーソル移動の←↓↑→には”h”,”j”,”k”,”l”というキーが割り当たっている。

また、カーソルの移動単位として、1文字ごとの移動の他、単語単位、行単位、画面の ページ単位の移動キーを定めている。

キー操作

移動単位

移動の向き

キー割当の由来(推測)

h, j, k, l

文字

左 下 上 右

ホームポジションから右手を動かさない

b, w

単語

左 右

Word と Back word

^, $

左(行頭) 右(行末)

正規表現における行頭と行末の記号

C-b, C-f

ページ

上 下

Back と Forward

これを含めて、コマンドモードと挿入モードの主なキー操作を図に示す。

../../_images/vi-modes-ja.png

これらのキー割り当ては、一旦覚えてしまえば、手をキーボードのホームポジションから 殆ど動かさずに大抵の操作ができるようになるため、学習の手間よりも効率を重視する タイプのユーザーには広く支持されている。

vi has 2 modes, “command mode” and “insert mode”, and you switch between these two modes while editing. In most other editors, you will use the cursor keys to move the cursor, and other letter keys such as “a”, “b”, “c” will cause those letters to be typed in as text. However, in vi’s command mode, regular letter keys have functions such as cursor movements assigned to them. For instance the cursor movements ←↓↑→ are assigned to the keys “h”, “j”, “k”, “l”.

../../_images/vi-modes-en.png

Once you memorize these key assignments, you will be able to do most of the editing tasks without ever moving your hands away from the home position. Therefore this method is widely accepted among editor users who prioritize productivity above the learning cost.

1.3.5. emacsエディタ初歩/Emacs editor primer

emacs(発音は”ee-max”)エディタは、lisp言語で実装されたエディタであり、emacs- lispと呼ぶlispの一種によって機能拡張することができます。 Emacsでプログラムを編集する場合には、プログラミング言語に合わせた機能拡張を利用します。 機能拡張次第で様々なキー操作が定義されるので、利用する機能拡張のキー操作を 把握する必要があります。

The emacs editor (pronounced as ee-max) is an editor implemented in a lisp variant named emacs-lisp, and is highly extendable and customizable. When using emacs to edit programs, emacs-lisp extension packages to support that programming language is used. The key assignment depends on those extension package, so you must check the keys for the packages you use.

起動にはファイル名を指定する。:

To invoke emacs, give the file name as an argument:

emacs cfd_main.cpp

起動したら、カーソル移動や削除キーなどはだいたい想像通りに機能します。 しかし、多くの機能は、コントロールキーを使ったシーケンスに割り当たっていて、 少し時間をかけて練習しないと使えません。 入門者は組み込みのチュートリアルをやるとよいでしょう。

Once invoked, simple keys like cursors and delete keys will function as expected. However, many functions are assigned to key sequences that use the control key, and you need to take some time to memorize the commonly used sequences. There is a built-in tutorial for beginners to learn the basics.

重要なキー操作をいくつか示しておきます。

Some of the important key sequences are shown below

F1 t

チュートリアルを開始する

Start the tutorial

Ctrl-x Ctrl-c(コントロールを押したまま、xとcを順次タイプ/Hold down Ctrl while you type x and c)

終了

Exit emacs

Ctrl-x Ctrl-s

保存

Save the current buffer

Ctrl-v

1ページ進む(順方向にスクロール)

Scroll one page towards the end of the buffer

Alt-v

1ページ戻る(逆方向にスクロール)

Scroll one page towards the beginning of the buffer

Ctrl-x 2

ウィンドウを上下に2分割する

Split the current window vertically into two windows

Ctrl-x 1

ウィンドウ分割をやめて、全画面で一つのウィンドウにする。

Close all but the current window