Vim 템플릿으로 상용구 줄이기

당신이 나 같은 터미널 중독자라면 이것을 좋아할 것입니다. 오늘 우리는 플러그인 없이 상용구 코드 작성을 피할 수 있는 방법을 설명할 것입니다. 우리가 탐색하려는 기능은 Vim에서 템플릿 또는 스켈레톤이라고 합니다.



사진 제공: Jake Givens on Unsplash


스켈레톤과 Vim

Don’t get scared, skeleton files (or templates) are just normal files. They are called that because they will be used as a skeleton when creating a specific file. To create a JSX TypeScript .tsx file, Vim can use a predefined template (skeleton) file and add a React class. How does that look - you must be asking. Let’s see below.

We can define a Skeleton file inside ~/.vim/skeletons/react-typescript.tsx like so:

import React from "react"

interface Props {}

const NewComponent = (props: Props) => {
  return (
    <>
      <div></div>
    </>
  )
}

export default NewComponent

Then, we can add the following line in our .vimrc :

autocmd BufNewFile *.tsx 0r ~/.vim/skeletons/skeletons/typescript-react.tsx

Let’s break it down a bit:

  • autocmd BufNewFile – run the following when we try to create a new file in Vim
  • *.tsx – this is the pattern you want the new file to match
  • 0r – read into the buffer starting at line 0, the first line
  • ~/.vim/skeletons/react-typescript.tsx – the file to read into the new file

And, that is it. Every time you create a new React TypeScript file, there will be sitting a new functional component. Let’s see what other useful templates we can use.

유용한 스켈레톤

I added a couple of skeletons inside my dotfiles on GitHub . 거기에서 자신을 돕고 원하는 것을 사용하십시오. 아래에서도 공유하겠습니다.

Bash 스크립트 스켈레톤

The most common template people suggest is the one for Bash scripts. It can look like this:

#!/usr/bin/env bash

set -eou pipefail

You also need the following line in your .vimrc :

autocmd BufNewFile *.sh 0r ~/.vim/skeletons/skeletons/script.sh

HTML 스켈레톤

I picked this one up from the HTML boilerplate in 2021 .

<!DOCTYPE html>
<html lang="en" class="no-js">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />

    <title>TODO</title>

    <meta name="description" content="Page description" />
    <meta property="og:title" content="Unique page title - My Site" />
    <meta property="og:description" content="Page description" />
    <meta
      property="og:image"
      content="https://pragmaticpineapple.com.com/image.jpg"
    />
    <meta property="og:image:alt" content="Image description" />
    <meta property="og:locale" content="en_US" />
    <meta property="og:type" content="website" />
    <meta name="twitter:card" content="summary_large_image" />
    <meta property="og:url" content="https://pragmaticpineapple.com.com/page" />
    <link rel="canonical" href="https://pragmaticpineapple.com.com/page" />

    <link rel="icon" href="/favicon.ico" />
    <link rel="icon" href="/favicon.svg" type="image/svg+xml" />
    <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
    <link rel="manifest" href="/my.webmanifest" />

    <meta name="theme-color" content="#FF00FF" />
  </head>

  <body></body>
</html>

.vimrc에 다음 줄을 추가하여 새 HTML 파일 생성 시 HTML 상용구를 적용할 수 있습니다.

autocmd BufNewFile *.html 0r ~/.vim/skeletons/skeletons/page.html


블로그 포스트 마크다운 스켈레톤

The following one is my favorite because I’ve been looking into ways on how to scaffold a new blog post. Take a look:

---
title: TODO
description: TODO
slug: TODO
date: 2021-1-1
published: true
tags:
  - TODO
---

I make sure these are only created inside content/blog directories like so:

autocmd BufNewFile *content/blog*.md 0r ~/.vim/skeletons/skeletons/blog-post.md

해골을 보관할 곳

If the current section was taken out of context, we could have been in trouble. But, jokes aside, where can you store template files? It doesn’t really matter. I keep them inside my dotfiles which I have in version control. They are inside ~/Documents/dotfiles/skeletons . Some folks like them in ~/.vim/templates or ~/.vim/skeletons . I like mine in a dotfiles git repo 쉽게 변경하고 GitHub로 푸시할 수 있습니다.

수동 사용

If you are not a fan of Vim automatically populating your files on creation, you can always reach out for the manual approach. To simply fill a file with another file’s content, try out :read command:

:read ~/.vim/skeletons/react-typescript.tsx

The contents of ~/.vim/skeletons/react-typescript.tsx will show below your cursor.

마지막 생각들

That’s all for now. Thanks for tuning in. Consider subscribing to my , where we will explore the possibility of adding some dynamic templates in one of the next blog posts.

I got the idea to write this blog post from VimTricks in their post , 그들에게 외쳐라.

또한 게시물이 유용하다고 생각되면 Twitter에서 친구 및 동료와 공유하는 것을 잊지 마십시오.






니콜라 두자









Vim 스켈레톤에 대한 새로운 게시물 💀아래에서 확인하세요 ⬇️medium.com/@nikolalsvk/re…


오후 18:16 - 2021년 5월 5일









다음 편까지, 건배.

좋은 웹페이지 즐겨찾기