3
2

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 の :set paste でインデント崩れしない貼りつけ

Last updated at Posted at 2022-05-06

vim で コピーしたコード貼りつけすると、インデントが崩れる

vim でコピペしたコードをペーストすると、インデントが崩壊することがある。
以下は「AMIやスナップショットを AWS CLI で一気に削除する」で使用したシェルスクリプトの例だが、

sakujo_AMI.sh
#!/bin/bash
# 設定ファイル
ami_list=sakujo_AMI_list.txt
# 日付時刻を文字列にする
jikan=`date +"%Y-%m%d-%H%M-%S"`

# profile
profile_name=<設定したプロファイル名>
# 意図したIAMユーザか確認
aws sts get-caller-identity --profile $profile_name

while read row
do
  echo ":::::::::::::::::::::::::::delete this ami ${row}" >> log/ami_all_log_$jikan.txt 2>&1
  aws ec2 deregister-image --image-id ${row} --profile $profile_name >> log/ami_all_log_$jikan.txt  2>&1
  if [ $? -ne 0 ]; then
    echo ${row} >> log/ami_error_${jikan}.txt
  else
    echo ${row} >> log/ami_ok_${jikan}.txt
  fi
  echo ":::::::::::::::::::::::::::end" >> log/ami_all_log_$jikan.txt  2>&1
sleep 3
done < ${ami_list}

これをコピーし Vim で貼りつけすると、以下のようにインデントが崩れてしまう。

image.png

:set paste でインデント崩れしないで貼りつける

ノーマルモードから:set pasteで貼りつけモードにすることで、インデント崩れしないで貼り付けできる。

ノーマルモードで:set paste
image.png

i をクリックして編集モードに移行する。

コピーしておいたコードを貼り付ける↓
image.png

escでノーマルモードに戻る↓
image.png

:wqで保存して閉じる↓
image.png

参考

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?