4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Vim】実践Vimの実践 【第5章 コマンドラインモード】

Last updated at Posted at 2023-09-08

Introduction

実践Vimをまとめるシリーズの第 5 章です。
第 1 章:ドットコマンド

第 2 章:ノーマルモード

第 3 章:挿入モード

第 4 章:ビジュアルモード

本記事のみで学べるよう努めますが、より詳しくかつ体系的に学びたい方は
実践Vimを読みながら進めることをおすすめします。

2023 年 9 月 9 日現在、絶版商品のため Amazon Kindle での購入を推奨します。

実践Vimのサンプルコードです。
記事更新に合わせて章ごとにディレクトリを分割し、参照しやすいようにします。
※公式のサンプルはリンク切れしています。

実際に操作することで、コマンドを効率よく定着させましょう。

本記事が少しでも読者様の学びに繋がれば幸いです!
「いいね」をしていただけると今後の励みになるので、是非お願いします!

環境

Ubuntu22.04
Vim9.0

第 5 章 コマンドラインモード

初めに ed ありき。
ed から ex ができ、ex から vi ができた。
そして。vi から Vim ができた。
(実践 Vim「第 5 章 コマンドラインモード」より)

TIP27: Vim のコマンドラインモード

  • コマンドラインモードでは Ex コマンドや検索パターン、あるいは式を入力するように求められる。

    • :コマンドラインモードに切り替え。
    • Escノーマルモードに切り替え。
  • Exコマンドとはコマンドラインモードで実行するコマンド。

    • 本章ではExコマンドを紹介。
    • EXコマンドは広範囲を対象に、一度で多くの行を変更できる。

TIP28: 連続する行に対してコマンドを実行する

Ex コマンドの多くには、それを実行する行の[range]を指定できる。
この範囲の始点と終点は行番号、マーク、パターンを使って指定可能だ。
(実践 Vim「TIP28」より)

行番号をアドレスとして使用する

  • Exコマンドの例。

    • :printで指定した行をエコーする。
    • 行番号をアドレスとして使用できる。
    chapter_code/5_commandline/ex_mode/practical-vim.html
    <!DOCTYPE html>
    <!--
    ! Excerpted from "Practical Vim",
    ! published by The Pragmatic Bookshelf.
    ! Copyrights apply to this code. It may not be used to create training material,
    ! courses, books, articles, and the like. Contact us if you are in doubt.
    ! We make no guarantees that this code is fit for any purpose.
    ! Visit http://www.pragmaticprogrammer.com/titles/dnvim for more book information.
    -->
    <html>
      <head><title>Practical Vim</title></head>
      <body><h1>Practical Vim</h1></body>
    </html>
    
    • :1で 1 行目に移動する。
    • :pで 1 行目の<!DOCTYPE html>をエコー。
    <!DOCTYPE html>
    
    • :$でファイル末尾に移動する。
    • :pで末尾の</html>をエコー。
    </html>
    
    • 移動とエコーは同時使用も可能。

      • :11pで 11 行目の<head><title>Practical Vim</title></head>をエコー。
      <head><title>Practical Vim</title></head>
      

アドレスで行範囲を指定する

  • アドレスは行範囲でも指定可能。

    • :10,$pで 10 行目から末尾までをエコー。
      <html>
      <head><title>Practical Vim</title></head>
      <body><h1>Practical Vim</h1></body>
      </html>
    
  • 現在行は.で表せる。

    • :.,$pで 現在行から末尾までをエコー。
  • すべての行は%で表せる。

    • :%pですべての行をエコー。1

TIP29: [:t] / [:m]コマンドで行をコピー/移動

:copy コマンド(とその短縮形の:t)を使うと、1 行以上の行を
ドキュメント内のある場所から別の場所にコピーできる。
一方、:move コマンドでは、ドキュメント内の別の場所へ移動できる。
(実践 Vim「TIP29」より)

[:t]で行コピー

:copyコマンド(:t)で行を別の場所にコピーする。

chapter_code/5_commandline/ex_mode/shopping-list.todo
Shopping list
    Hardware Store
        Buy new hammer
    Beauty Parlor
        Buy nail polish remover
        Buy nails
  • :2で 2 行目に移動する。
  • :6copy.(:6t.)で下の行に 6 行目の内容をコピーする。
chapter_code/5_commandline/ex_mode/shopping-list.todo
Shopping list
    Hardware Store
        Buy nails
        Buy new hammer
    Beauty Parlor
        Buy nail polish remover
        Buy nails

:tコマンドの使用例

コマンド 結果
:6t. 6 行目を現在行の下にコピー
:t6 現在行を 6 行目の下にコピー
:t. 現在行をコピー(yyp と同様)
:t$ 現在行をファイル末尾にコピー
:'<,'>t0 ビジュアルな選択範囲をファイル先頭にコピー

[:m]で行移動

:moveコマンド(:m)で行を別の場所に移動する。

  • Hardware Store セクションを Beauty Parlor の下に移動する。
chapter_code/5_commandline/ex_mode/shopping-list.todo
Shopping list
    Hardware Store
        Buy nails
        Buy new hammer
    Beauty Parlor
        Buy nail polish remover
        Buy nails
  • :2で 2 行目に移動する。
  • VjjHardware Store セクションをビジュアルモードで範囲選択する。
  • :'<,'>m$Beauty Parlor の下に移動する。2
chapter_code/5_commandline/ex_mode/shopping-list.todo
Shopping list
    Beauty Parlor
        Buy nail polish remover
        Buy nails
    Hardware Store
        Buy nails
        Buy new hammer
  • @:で直前の Ex コマンドを繰り返す。

TIP30: 選択範囲に対してノーマルモードのコマンドを実行する

連続する行に対してノーマルモードコマンドを実行したいときには、
:normal コマンドを使えばよい。
ドットコマンドやマクロと組み合わせると、繰り返し作業をほんのわずかな手間で行える。
(実践 Vim「TIP30」より)

  • :normalでドットコマンドを繰り返す。
  • 各業の末尾に;を挿入する。
chapter_code/5_commandline/ex_mode/foobar.js
/***
 * Excerpted from "Practical Vim",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material,
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose.
 * Visit http://www.pragmaticprogrammer.com/titles/dnvim for more book information.
***/
var foo = 1
var bar = 'a'
var baz = 'z'
var foobar = foo + bar
var foobarbaz = foo + bar + baz
  • :9var foo = 1(;追加したい行の最初)に移動する。
  • A;<Esc>var foo = 1の末尾に;を挿入してノーマルモードに戻る。
chapter_code/5_commandline/ex_mode/foobar.js
/***
 * Excerpted from "Practical Vim",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material,
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose.
 * Visit http://www.pragmaticprogrammer.com/titles/dnvim for more book information.
***/
var foo = 1;
var bar = 'a'
var baz = 'z'
var foobar = foo + bar
var foobarbaz = foo + bar + baz
  • jVGvar foo = 1の下の行から行の末尾までを選択する。
  • :'<,'>normal .で選択した行の末尾に;を挿入する。3
chapter_code/5_commandline/ex_mode/foobar.js
/***
 * Excerpted from "Practical Vim",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material,
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose.
 * Visit http://www.pragmaticprogrammer.com/titles/dnvim for more book information.
***/
var foo = 1;
var bar = 'a';
var baz = 'z';
var foobar = foo + bar;
var foobarbaz = foo + bar + baz;

TIP31: 直前の Ex コマンドを繰り返す

.コマンドは直前のノーマルモードコマンドを繰り返すのに使えるが、
直前の Ex コマンドを繰り返したいときには.コマンドではなく@:を使う必要がある。
直前の Ex コマンドを取り消す方法を知っておくのはいつだって役に立つ。
ということで、これについて話をしていこう。
(実践 Vim「TIP31」より)

  • .では Vim コマンドラインから実行された変更は繰り返せない。
  • @:で直前の Ex コマンドを繰り返す。
    • 再度繰り返す場合は@@でも可。

TIP32: Ex コマンドでタブ補完

シェルと同じく、プロンプトでは<Tab>キーでコマンドの自動補完が可能だ。
(実践 Vim「TIP32」より)

  • <Tab>で候補のリストを表示できる。

    • 例えば:col + <Tab>を入力する。

      :col
      colder      colorscheme
      
    • リストに沿って:colorと入力し、<Tab>で補完をする。

      :col
      colder      colorscheme
      colorscheme
      
    • 空白を開けて<Tab>で候補のリストを表示する。

    Screenshot from 2023-09-09 09-45-21.png

    • evening<Tab>補完をして<Enter>を押す。

    Screenshot from 2023-09-09 09-49-02.png

TIP33: カーソルにある単語をコマンドプロンプトに挿入

コマンドラインモードにいても、Vim は常にカーソルがどこにあるか、
どのスプリットウィンドウがアクティブかを把握している。
アクティブなドキュメントのカーソル位置にある単語(word もしくは WORD)を
コマンドプロンプトに挿入すると、時間の節約になる。
(実践 Vim「TIP33」より)

  • <Ctrl + r + w>でカーソル位置の単語をコピーして挿入する。
chapter_code/5_commandline/ex_mode/loop.js
/***
 * Excerpted from "Practical Vim",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material,
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose.
 * Visit http://www.pragmaticprogrammer.com/titles/dnvim for more book information.
***/
var tally;
for (tally=1; tally <= 10; tally++) {
  // do something with tally
};
  • /tallyで移動する。
  • *で次の単語マッチに移動する。
  • cwで単語削除して挿入モードに入る。
  • counter<Esc>tallycounterに変更してノーマルモードに戻る。
  • <Ctrl + r + w>でカーソル位置の単語をコピーして挿入する。
  • :%s//<Ctrl + r + w>/gで残りのtallycounterに変更する。
    • カーソルはcounterにあるため、<Ctrl + r + w>counterに変換される。
chapter_code/5_commandline/ex_mode/loop.js
/***
 * Excerpted from "Practical Vim",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material,
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose.
 * Visit http://www.pragmaticprogrammer.com/titles/dnvim for more book information.
***/
var counter;
for (counter=1; counter <= 10; counter++) {
  // do something with counter
};

TIP34: 履歴からコマンドを呼び戻す

私達がコマンドラインで入力したコマンドを Vim は覚えていてくれる。
そして、それらをコマンドラインに呼び戻す方法を 2 つ用意している。
1 つは過去のコマンドラインをカーソルキーでスクロールする方法。
もう 1 つはコマンドラインウィンドウで呼び戻す方法だ。
(実践 Vim「TIP34」より)

  • : + でコマンドライン履歴を遡る。4
  • : + で順方向にを表示する。5

TIP35: シェルでコマンドを実行

外部プログラムは、Vim を終了しなくても、カンタンに呼び出せる。
一番いいのは、コマンドの標準入力にバッファの内容を送り込んだり、
外部コマンドの標準出力をバッファ内に取り込んだりできるところだ。
(実践 Vim「TIP35」より)

  • ターミナルで Vim を実行する際に、以下の TIP が効果を発揮する。

シェルでプログラムを実行

  • コマンドラインモードでは、!をコマンドに前置して外部プログラムを実行できる。
:!ls
duplicate.todo	emails.csv  foobar.js  history-scrollers.vim  loop.js  practical-vim.html  shopping-list.todo

続けるにはENTERを押すかコマンドを入力してください
  • !の前置でシェルによる実行を行う。
    • :!ls
  • !がないとVimの組み込みコマンドを呼び出す。
    • :ls6

外部のコマンドを介してバッファの内容をフィルタリングする

  • 範囲を指定して実行すると結果がフィルタリングされる。
  • 2 つ目のフィールドであるlast nameを基準にソートする。
chapter_code/5_commandline/ex_mode/emails.csv
first name,last name,email
john,smith,john@example.com
drew,neil,drew@vimcasts.org
jane,doe,jane@example.com
  • :2,$!sort -t',' -k2でソートする。
    • :2,$で先頭行を残して 2 行目からを範囲にする。
    • !sort -t',',をソート区切りを指定する。
    • -k2で 2 つ目のフィールドを指定する。
chapter_code/5_commandline/ex_mode/emails.csv
first name,last name,email
jane,doe,jane@example.com
drew,neil,drew@vimcasts.org
john,smith,john@example.com

考察

外部コマンドを呼び出す時に使えるコマンド

コマンド 効果
:shell シェル開始(exit で終了)
:!{cmd} シェルで {cmd} 実行
:read !{cmd} シェルで{cmd}実行し、標準出力をカーソル行の下に挿入
:[range]write !{cmd} [range]行を標準出力としてシェルで{cmd}実行
:[range]!{filter} {filter}で[range]行をフィルタリング

最後に

コマンドラインモードは仕組みに慣れるまでは使い方が浮かばないかもしれません。
私自身も:%以外はあまり積極的に使わないので、以下 2 点だけ慣用句的に覚えてみましょう。

  • :%dで全行削除
  • :%yで全行ヤンク

また、本記事で紹介したコマンドを実際に試してみることを強くおすすめします。

最後まで閲覧頂きありがとうございました。
本記事がお役に立てば幸いです!

次回複数ファイルの管理

[]

参考書籍

参考 URL

  1. :%は実務でも使用ケースが多いです。:%dで全行削除など。

  2. 複雑に見えるが、:を押すと選択範囲である'<,'>は自動で記入される。
    是非試してほしいです。

  3. ドットコマンドで試しましたが、ノーマルモードのコマンドであれば同じように使えます。

  4. <Ctrl + p>も同様の処理です。

  5. <Ctrl + n>も同様の処理です。

  6. バッファリストを表示する。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?