0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Vimの削除コマンド3選

Last updated at Posted at 2025-04-06

はじめに

普段はIDEをメインで使ってるけど、たまーにVimを触る機会があって、「あれ?削除コマンドなんだっけ?」ってなります😅

というわけで、備忘のためVimのよく使いそうな削除コマンド3つをまとめました。

1. 内容を全部消す 🧹

「全部消して書き換えたい!」ってときのコマンドです。

:%d

使い方:
コマンドモードで:%d と入力してEnterキーを押す

# 実行前
line 1: import something
line 2: def function():
line 3: print("hello")
...

# コマンド実行
:%d

# 実行後
(何もない空のファイル)

ちなみに、% は「ファイル全体」を表していて、d は「削除」のコマンドです。

2. 一行または複数行を削除する ✂️

「この行いらないな」とか「ここの3行消したいな」ってときに使えるやつ。

dd または 5dd (5行の場合)

一行削除

使い方:
コマンドモードで削除したい行にカーソルを移動し、
dd と入力する(Enterキー不要)

# 実行前 (カーソルは2行目にある状態)
line 1: import something
line 2: # 不要 → ここにカーソルがある
line 3: def function():

# ddを入力

# 実行後
line 1: import something
line 2: def function(): → 元の3行目がここに来る

複数行削除

使い方:削除したい範囲の最初の行にカーソルを移動し、
3dd と入力(3行削除する場合。Enterキー不要)

# 実行前 (カーソルは2行目にある状態)
line 1: import something
line 2: # 不要 → ここにカーソルがある
line 3: # 不要
line 4: # 不要
line 5: def function():

# 3ddを入力

# 実行後
line 1: import something
line 2: def function(): → 元の5行目がここに来る

3. 空白行を全て削除する 🧽

コードの整理でめっちゃ便利!空白行がたくさんあって「全部消したい!」ってときに一発で消せるやつです。

:g/^$/d

使い方:

コマンドモードで :g/^$/d と入力してEnterキーを押す

# 実行前
line 1: import something

line 2:

line 3:

line 4: def function():

line 5:

line 6: print("hello")

# コマンド実行
:g/^$/d

# 実行後
line 1: import something
line 2: def function():
line 3: print("hello")

ちなみに、

  • :g - 「global」の意味。ファイル全体に対して何かする
  • /^$/ - 「行頭(^)の直後に行末($)がある行」=空行を検索
  • d - 見つかった行を削除

まとめ

  1. :%d - ファイル内容を全部消す
  2. dd または 5dd - 一行または複数行(例:5行)を削除
  3. :g/^$/d - 空白行を全て削除

この記事が役に立ったらいいね♪やコメントをいただけると嬉しいです。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?