Basic usage (基础用法)

Basic usage (基础用法)


Hugo's command line interface (CLI) is fully featured but simple to use, even for those with limited experience working from the command line. Hugo 的命令行界面 (CLI) 功能齐全且易于使用,即使对于那些使用命令行工作经验有限的人而言也是如此。

Test your installation 测试安装 #

After [installing] Hugo, test your installation by running:

安装Hugo后,运行以下命令测试您的安装:

hugo version

You should see something like: 你应该看到类似这样的内容:

hugo v0.123.0-3c8a4713908e48e6523f058ca126710397aa4ed5+extended linux/amd64 BuildDate=2024-02-19T16:32:38Z VendorInfo=gohugoio

Display available commands 显示可用命令 #

To see a list of the available commands and flags:

hugo help

To get help with a subcommand, use the --help flag. For example:

hugo server --help

Build your site 建立您的网站 #

To build your site, cd into your project directory and run:

要构建您的网站,cd请进入您的项目目录并运行:

hugo

The [hugo] command builds your site, publishing the files to the public directory. To publish your site to a different directory, use the [--destination] flag or set [publishDir] in your site configuration.

该hugo命令会构建您的站点,并将文件发布到目录public。要将您的站点发布到其他目录,请使用站点配置中的–destination标志或设置。publishDir

Draft, future, and expired content 草稿、未来和过期内容 #

Hugo allows you to set draft, date, publishDate, and expiryDate in the [front matter] of your content. By default, Hugo will not publish content when:

Hugo 允许你在内容的开头设置draft、date、publishDate和。默认情况下,Hugo 在以下情况下不会发布内容:expiryDate

  • The draft value is true 价值draft是true
  • The date is in the future 是date未来
  • The publishDate is in the future 是publishDate未来
  • The expiryDate is in the past 是expiryDate过去式

You can override the default behavior when running hugo or hugo server with command line flags:

hugo --buildDrafts    # or -D
hugo --buildExpired   # or -E
hugo --buildFuture    # or -F

Although you can also set these values in your site configuration, it can lead to unwanted results unless all content authors are aware of, and understand, the settings.

虽然您也可以在站点配置中设置这些值,但除非所有内容作者都了解并理解这些设置,否则可能会导致不必要的结果。

Develop and test your site 开发并测试您的网站 #

To view your site while developing layouts or creating content, cd into your project directory and run:

hugo server

The [hugo server] command builds your site and serves your pages using a minimal HTTP server. When you run hugo server it will display the URL of your local site:

Web Server is available at http://localhost:1313/ 

While the server is running, it watches your project directory for changes to assets, configuration, content, data, layouts, translations, and static files. When it detects a change, the server rebuilds your site and refreshes your browser using [LiveReload].

Most Hugo builds are so fast that you may not notice the change unless you are looking directly at your browser.

LiveReload #

While the server is running, Hugo injects JavaScript into the generated HTML pages. The LiveReload script creates a connection from the browser to the server via web sockets. You do not need to install any software or browser plugins, nor is any configuration required.

Automatic redirection #

When editing content, if you want your browser to automatically redirect to the page you last modified, run:

hugo server --navigateToChanged

Deploy your site #

When you are ready to deploy your site, run:

hugo

This builds your site, publishing the files to the public directory. The directory structure will look something like this:

public/
├── categories/
│   ├── index.html
│   └── index.xml  <-- RSS feed for this section
├── posts/
│   ├── my-first-post/
│   │   └── index.html
│   ├── index.html
│   └── index.xml  <-- RSS feed for this section
├── tags/
│   ├── index.html
│   └── index.xml  <-- RSS feed for this section
├── index.html
├── index.xml      <-- RSS feed for the site
└── sitemap.xml

In a simple hosting environment, where you typically ftp, rsync, or scp your files to the root of a virtual host, the contents of the public directory are all that you need.

Most of our users deploy their sites using a CI/CD workflow, where a push[^1] to their GitHub or GitLab repository triggers a build and deployment. Popular providers include [AWS Amplify], [CloudCannon], [Cloudflare Pages], [GitHub Pages], [GitLab Pages], and [Netlify].

Learn more in the [hosting and deployment] section.