LoginSignup
9
8

More than 5 years have passed since last update.

[Atom]snippets登録で捗る(自動補完)

Last updated at Posted at 2018-07-07

こんなことありません...?

atomの自動補完を使っているとき

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

これを自分好みの自動補完にしたい!!

さっそくやってみる。

困ったときのgoogle先生に聞くと、
atomのファイル>snippetsを選択するといいらしいのでやってみる。
以下のファイルが開く。

snippets.cson
# Your snippets
#
# Atom snippets allow you to enter a simple prefix in the editor and hit tab to
# expand the prefix into a larger code block with templated values.
#
# You can create a new snippet in this file by typing "snip" and then hitting
# tab.
#
# An example CoffeeScript snippet to expand log to console.log:
#
# '.source.coffee':
#   'Console log':
#     'prefix': 'log'
#     'body': 'console.log $1'
#
# Each scope (e.g. '.source.coffee' above) can only be declared once.
#
# This file uses CoffeeScript Object Notation (CSON).
# If you are unfamiliar with CSON, you can read more about it in the
# Atom Flight Manual:
# http://flight-manual.atom.io/using-atom/sections/basic-customization/#_cson

readmeみたいなのでこれを読んでいく。
11行目~によると、

snippets_template.cson
  '何言語でのコマンドか(どの種類ファイルで適用するか)':
     'コマンド名':
       'prefix': '"キーボードで入力するもの(選択候補として出るもの)"'
       'body': '"選択されたとき展開するもの"'

こんな要領で書けばいいらしいので、  ←適当過ぎない?
以下を追加した。

snippets.cson
#cpp snippet
  '.source.cpp':
#iostream
    'cout':
      'prefix':'cout'
      'body':'cout<< <<endl;'

で冒頭述べたものも以下のようになりました(std::などを省きたかった)

さらに...

1.キーボードの位置

補完内容を展開後キーボード入力をどこに置くかを$で定義できる。
tabキーを押すことで$1→$2....→$Nに移動できるようになる。
また${N:"keyword"}とすることで展開後選択される文字列で設定できる。

snippets.cson
#cpp snippet
  '.source.cpp':
#iostream
    'cout':
      'prefix':'cout'
      'body':'cout<<${1:variable}<<endl;'

2.改行を含むスニペット

\nを用いることで改行を含めることができます。
ただしスニペット内に\nを用いるときは工夫が必要。

snippets.cson
#cpp snippet
  '.source.cpp':
#iostream
    'cout':
      'prefix':'main'
      'body':'int main(void){\n return 0;\n}'

おわりに

本記事ではc++についてだけ行いましたが、すべての言語で設定できるようです。

本記事等について何かガバや改善点があれば、お気軽にお願いします。
(初心者なのでガバを生成している可能性が高い)

9
8
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
9
8