본문 바로가기

환경설정/c & c++

[VS CODE] C++ Snippet 설정

Snippet 이란?


 

Snippet

스니펫(Snippet)은 프로그래밍 코드 또는 텍스트 조각을 미리 정의된 템플릿 형식으로 저장하고 재사용할 수 있도록 도와주는 도구입니다. 코드 스니펫은 일반적으로 자주 사용하는 코드 블록, 코

tkxxls.tistory.com

VS code Snippet 설정


  1. ctrl + shift + p 를 누른 후, snippets 를 검색한 후, Snippets: Configure User Snippets에 들어갑니다.

  2. cpp.json에 들어갑니다.

  3. 다음과 같이 코드를 작성합니다.

    {
    	"Baekjoon Function": {
            "prefix": "!main",
            "body": [
    			"#include <iostream>",
    			"",
    			"using namespace std;",
    			"",
                "int main()",
    			"{",
    			"	ios_base::sync_with_stdio(false);",
    			"	cin.tie(NULL); cout.tie(NULL);",
                "",
    			"",
    			"",
                "	return 0;",
                "}"
            ],
            "description": "백준을 위한 Main 함수 for C++ by tkxxls"
        }
    }


  4. 저장한 뒤 cpp파일을 생성해 제대로 작동하는지 확인합니다. cpp 파일에서 !main을 입력해봅니다.
    !main 말고 다른 prefix를 사용하고 싶으면 "prefix"의 내용을 바꿔줍니다.

VS code Snippet 단축키 설정


snippet을 단축키로 사용할 수도 있습니다.

 

  1. ctrl+shift+p를 누른 후 Open Keyboard Shortcuts에 들어갑니다.

  2. 오른쪽에 빨갛게 표시한 버튼을 눌러 keybindings.json에 들어갑니다.

  3. 다음과 같이 JSON파일을 작성합니다.
    원하는 단축키로 바꾸고 싶으면 "key" 값을 변경해주면 됩니다.

    [
        {
            "key": "ctrl+1",
            "command": "editor.action.insertSnippet",
            "args": {
                "name": "Baekjoon Function"
            },
            "when": "editorTextFocus && editorLangId == 'cpp'"
        }
    ]

참고 자료


 

Snippets in Visual Studio Code

It is easy to add code snippets to Visual Studio Code both for your own use or to share with others on the public Extension Marketplace. TextMate .tmSnippets files are supported.

code.visualstudio.com

 

'환경설정 > c & c++' 카테고리의 다른 글

[VS CODE] C/C++ launch.json 작성  (0) 2024.05.07
[VS CODE] C/C++ tasks.json 작성  (0) 2023.09.21