[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Low cost] Wasabi object storage construction and operation service

[Low cost] Wasabi object storage construction and operation service

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Global support for over 200 countries] eSIM Marketplace “beSIM”

[Global support for over 200 countries] eSIM Marketplace “beSIM”

[Suitable for local companies in China] China cloud / server construction, operation and maintenance

[Suitable for local companies in China] China cloud / server construction, operation and maintenance

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

【Linux初心者向け】できた!viコマンドで編集と保存

こんにちは!
株式会社ビヨンド四国オフィスのペルシャ猫、いのうえです。

今回はファイルを編集する「vi」コマンドについて!
オプション多くて検証に苦戦しました。

viコマンドってなんだ?

「vi」コマンドとは、「エディタを起動してー!」と命令するコマンドです。
そもそも「vi」がUnix系OSで使用されるテキストエディタのことを指しています。
同時にディレクトリにファイルが存在しない場合は、ファイル作成もしてくれます。
入社当初は、「エディタってそもそもなに???」って思ってましたが
Windowsでいうとこの「メモ帳」と考えるとわかりやすいですね。

viコマンドでファイルを作成し、編集しよう

まずは、「test」ディレクトリにviコマンドで「haruka」というファイル名を指定し、編集します。

[コード][root@test-aws-harukainoue test]# pwd
/root/テスト[/コード]
[root@test-aws-harukainoue test]# vi haruka

上記コマンドを実行すると「haruka」のノーマルモードに移動します。
この時はまだファイルは作成されていません。

-rw------ 1 root root 12288 Jun 23 01:21 .haruka.swp

このようなSWAPファイルが一時的に作成されます。
SWAPファイルとは、アプリケーションがクラッシュしてデータが消えてしまうことを防ぐために
viが開かれた時に作成され、viで編集後に自動で削除される一時的な記録ファイルのことです。
これにより、もしもviがシステムエラーで強制終了しても、保存前のデータが失われることを防ぐことが出来ます。
そのため「:w」が実行されたときに初めて「haruka」というファイルは作成される仕組みになっています。

ファイルの中身をviで編集する場合は、「i」キーを押し、「INSERT」モードにします。
挿入モードになると「haruka」ファイルの中に記述することが出来ます。
適当にプロフィールでも書いてみます。

井上明香(ペルシャ猫)<font></font>
趣味:料理<font></font>
好きなもの:いちごと猫<font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
-- 挿入 --[/コード]</font></font></pre>
「<strong>-- INSERT --</strong>」と表示されている間は、挿入モードになっています。
編集モードを抜ける時は「<strong>Esc</strong>」キーを押します。
<pre><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[コード]:wq[/コード]</font></font></pre>
挿入モードでエディタに内容を記述し、保存すると同時に「haruka」というファイルも作成されました!
viコマンドは、ノーマルモードから挿入モードに移行することにより、文字が打てるようになります。
既存のファイルがviで開かれている場合は上書き保存され
新しいファイルを開いていた場合は、ファイルが新規作成されます。
<pre><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[コード][root@test-aws-harukainoueテスト]# ll</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
合計 4</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
-rw-r--r-- 1 root root 83 6月21日07:42 はるか

:」でコマンドモードに戻り、「wq」で上書き保存が出来ます。 また、保存済みのファイルを閲覧し終了する場合は、「q」 強制的にviを終了するときは「」を付けます。 例えばviで編集したものを変更内容を保存せず、破棄して終了する場合は

[コード]:q![/コード]

を使用します。

知っておくと便利!

■カーソル移動コマンド
矢印キーでも移動できますが知っておくと便利なカーソル移動コマンドをご紹介します。
※「Esc」キーを押下し、編集モードが終了していることを必ず確認して実行してください。

0(ゼロ) カーソルのある行の行頭にカーソルを移動する。
$ カーソルのある行の行末にカーソルを移動する
ぐぐ ファイルの先頭にカーソルを移動する。
G ファイルの末尾にカーソルを移動する。
ファイルの末尾にカーソルを移動する。

■削除コマンド

xもしくは、dl 現在のカーソル位置の文字を削除
DD 現在のカーソルのある行を削除
d$ 現在のカーソル位置から行末までを削除
d0 現在のカーソル位置から行頭までを削除
だわ 単語1つを削除
ディグ 最初の行まで削除
dG 最後の行まで削除

■ヤンク
コピー(ヤンク)したい場合は、yを使用します。
yの後ろにカーソル移動コマンドを指定して使用します。

やあ 現在のカーソル位置の行をヤンク

さいごに

viコマンドについてブログに書こうと思ったら、とにかく苦労しました。
viコマンドについて、分かってるようでわかってなかったんです。
エディタを編集した挿入モードから、コマンドモードにして、エディタを終了する正しい方法について。
それに含め、私のようなLinux初心者にわかる説明はどうすればいいのか・・・
トライアンドエラーを繰り返し、やっとこのブログが誕生しました!!(`・ω・´)

Linuxについて分かりやすく書いたブログもあるので、こちらもぜひ読んでみて下さい!
https://beyondjapan.com/blog/2022/02/linux/

【IT企業に就職するなら知っておけ!】今さら聞けないLinuxとは?(初心者向け編)

日々成長、日々前進。
毎日、私自身をアップデートしていかなければ!!!
最後まで読んでくださって、ありがとうございます。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
27
読み込み中...
27 票, 平均: 1.00 / 127
65,554
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Akika Inoue

Belongs to the System Solutions Department.
He joined Beyond as a founding member of the Shikoku office.
I jumped into the IT industry with no experience. As an education team, we create curriculum and conduct training for new graduates, mid-career, and existing members.
The main business is server operation and maintenance.
Either way, we value your content.
Also belongs to the Web Content Division and YouTube Team.