HUGO

  • 新闻
  • 文档
  • 主题
  • 作品展示
  • 社区
  • GitHub
    • English
    • 中文

What's on this Page

  • 测试安装
  • hugo 命令
  • 草案,未来和过期内容
  • LiveReload
    • 禁用LiveReload
  • 部署您的网站
GETTING STARTED

基本用法

Hugo的CLI是全功能的,但简单易用,即使对那些谁也非常的命令行经验有限的工作。

The following is a description of the most common commands you will use while developing your Hugo project. See the Command Line Reference for a comprehensive view of Hugo’s CLI.

测试安装

Once you have installed Hugo, make sure it is in your PATH. You can test that Hugo has been installed correctly via the help command:

hugo help

The output you see in your console should be similar to the following:

hugo is the main command, used to build your Hugo site.

Hugo is a Fast and Flexible Static Site Generator
built with love by spf13 and friends in Go.

Complete documentation is available at https://gohugo.io/.

Usage:
  hugo [flags]
  hugo [command]

Available Commands:
  check       Contains some verification checks
  config      Print the site configuration
  convert     Convert your content to different formats
  env         Print Hugo version and environment info
  gen         A collection of several useful generators.
  help        Help about any command
  import      Import your site from others.
  list        Listing out various types of content
  new         Create new content for your site
  server      A high performance webserver
  version     Print the version number of Hugo

Flags:
  -b, --baseURL string         hostname (and path) to the root, e.g. https://spf13.com/
  -D, --buildDrafts            include content marked as draft
  -E, --buildExpired           include expired content
  -F, --buildFuture            include content with publishdate in the future
      --cacheDir string        filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
      --cleanDestinationDir    remove files from destination not found in static directories
      --config string          config file (default is path/config.yaml|json|toml)
      --configDir string       config dir (default "config")
  -c, --contentDir string      filesystem path to content directory
      --debug                  debug output
  -d, --destination string     filesystem path to write files to
      --disableKinds strings   disable different kind of pages (home, RSS etc.)
      --enableGitInfo          add Git revision, date and author info to the pages
  -e, --environment string     build environment
      --forceSyncStatic        copy all files when static is changed.
      --gc                     enable to run some cleanup tasks (remove unused cache files) after the build
  -h, --help                   help for hugo
      --i18n-warnings          print missing translations
      --ignoreCache            ignores the cache directory
  -l, --layoutDir string       filesystem path to layout directory
      --log                    enable Logging
      --logFile string         log File path (if set, logging enabled automatically)
      --minify                 minify any supported output format (HTML, XML etc.)
      --noChmod                don't sync permission mode of files
      --noTimes                don't sync modification time of files
      --path-warnings          print warnings on duplicate target paths etc.
      --quiet                  build in quiet mode
      --renderToMemory         render to memory (only useful for benchmark testing)
  -s, --source string          filesystem path to read files relative from
      --templateMetrics        display metrics about template executions
      --templateMetricsHints   calculate some improvement hints when combined with --templateMetrics
  -t, --theme strings          themes to use (located in /themes/THEMENAME/)
      --themesDir string       filesystem path to themes directory
      --trace file             write trace to file (not useful in general)
  -v, --verbose                verbose output
      --verboseLog             verbose logging
  -w, --watch                  watch filesystem for changes and recreate as needed

Use "hugo [command] --help" for more information about a command.

hugo 命令

The most common usage is probably to run hugo with your current directory being the input directory.

This generates your website to the public/ directory by default, although you can customize the output directory in your site configuration by changing the publishDir field.

The command hugo renders your site into public/ dir and is ready to be deployed to your web server:

hugo
0 draft content
0 future content
99 pages created
0 paginator pages created
16 tags created
0 groups created
in 90 ms

草案,未来和过期内容

Hugo allows you to set draft, publishdate, and even expirydate in your content’s front matter. By default, Hugo will not publish:

  1. Content with a future publishdate value
  2. Content with draft: true status
  3. Content with a past expirydate value

All three of these can be overridden during both local development and deployment by adding the following flags to hugo and hugo server, respectively, or by changing the boolean values assigned to the fields of the same name (without --) in your configuration:

  1. --buildFuture
  2. --buildDrafts
  3. --buildExpired

LiveReload

Hugo comes with LiveReload built in. There are no additional packages to install. A common way to use Hugo while developing a site is to have Hugo run a server with the hugo server command and watch for changes:

hugo server
0 draft content
0 future content
99 pages created
0 paginator pages created
16 tags created
0 groups created
in 120 ms
Watching for changes in /Users/yourname/sites/yourhugosite/{data,content,layouts,static}
Serving pages from /Users/yourname/sites/yourhugosite/public
Web Server is available at http://localhost:1313/
Press Ctrl+C to stop

This will run a fully functioning web server while simultaneously watching your file system for additions, deletions, or changes within the following areas of your project organization:

  • /static/*
  • /content/*
  • /data/*
  • /i18n/*
  • /layouts/*
  • /themes/<CURRENT-THEME>/*
  • config

Whenever you make changes, Hugo will simultaneously rebuild the site and continue to serve content. As soon as the build is finished, LiveReload tells the browser to silently reload the page.

Most Hugo builds are so fast that you may not notice the change unless looking directly at the site in your browser. This means that keeping the site open on a second monitor (or another half of your current monitor) allows you to see the most up-to-date version of your website without the need to leave your text editor.

Hugo injects the LiveReload <script> before the closing </body> in your templates and will therefore not work if this tag is not present..

禁用LiveReload

LiveReload works by injecting JavaScript into the pages Hugo generates. The script creates a connection from the browser’s web socket client to the Hugo web socket server.

LiveReload is awesome for development. However, some Hugo users may use hugo server in production to instantly display updated content. The following methods make it easy to disable LiveReload:

hugo server --watch=false

Or…

hugo server --disableLiveReload

The latter flag can be omitted by adding the following key-value to your config.toml or config.yml file, respectively:

disableLiveReload = true
disableLiveReload: true

部署您的网站

After running hugo server for local web development, you need to do a final hugo run without the server part of the command to rebuild your site. You may then deploy your site by copying the public/ directory to your production web server.

Since Hugo generates a static website, your site can be hosted anywhere using any web server. See Hosting and Deployment for methods for hosting and automating deployments contributed by the Hugo community.

Running hugo does not remove generated files before building. This means that you should delete your public/ directory (or the publish directory you specified via flag or configuration file) before running the hugo command. If you do not remove these files, you run the risk of the wrong files (e.g., drafts or future posts) being left in the generated site.

See Also

  • 外部学习资源
  • 使用Hugo模块
  • 快速开始
  • 关于 Hugo
    • 概述
    • Hugo的安全模型
    • Hugo and GDPR
    • 什么是Hugo
    • Hugo 特征
    • 静态的好处
    • 证书
  • 入门
    • 入门概述
    • 快速开始
    • 安装 Hugo
    • 基本用法
    • 目录结构
    • 配置
    • 外部学习资源
  • Hugo 模块
    • Hugo 模块概述
    • 配置模块
    • 使用Hugo模块
    • 主题组件
  • 内容管理
    • 内容管理概述
    • 组织
    • 捆绑页
    • 内容格式
    • 前面的问题
    • 构建选项
    • 网页资源
    • 图像处理
    • 简码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类
    • 摘要
    • 链接和交叉引用
    • URL管理
    • 菜单
    • 目录
    • 静态文件
    • 注释
    • 多种语言 和 国际化
    • 语法高亮
  • 模板
    • 模板概述
    • 介绍
    • 模板查找顺序
    • 自定义输出格式
    • 基本模板和模块
    • 列表页面模板
    • 首页模板
    • 章节模板
    • 分类模板
    • 单页模板
    • 内容视图模板
    • 数据模板
    • 部件模板
    • 简码模板
    • 本地文件模板
    • 404页
    • 菜单模板
    • 分页
    • RSS 模板
    • 网站地图模板
    • Robots.txt
    • 内置模板
    • 另类模板
    • 模板调试
  • 函数
    • 函数快速参考
    • .AddDate
    • .Format
    • .Get
    • .GetPage
    • .HasMenuCurrent
    • .IsMenuCurrent
    • .Param
    • .Render
    • .RenderString
    • .Scratch
    • .Unix
    • absLangURL
    • absURL
    • after
    • anchorize
    • append
    • apply
    • base64
    • chomp
    • complement
    • cond
    • countrunes
    • countwords
    • dateFormat
    • default
    • delimit
    • dict
    • echoParam
    • emojify
    • eq
    • errorf and warnf
    • fileExists
    • findRE
    • first
    • float
    • ge
    • getenv
    • group
    • gt
    • hasPrefix
    • highlight
    • htmlEscape
    • htmlUnescape
    • hugo
    • humanize
    • i18n
    • in
    • index
    • int
    • intersect
    • isset
    • jsonify
    • lang.Merge
    • lang.NumFmt
    • last
    • le
    • lower
    • lt
    • markdownify
    • Math
    • md5
    • merge
    • ne
    • now
    • os.Stat
    • partialCached
    • path.Base
    • path.Dir
    • path.Ext
    • path.Join
    • path.Split
    • plainify
    • pluralize
    • print
    • printf
    • println
    • querify
    • range
    • readDir
    • readFile
    • ref
    • reflect.IsMap
    • reflect.IsSlice
    • relLangURL
    • relref
    • relURL
    • replace
    • replaceRE
    • safeCSS
    • safeHTML
    • safeHTMLAttr
    • safeJS
    • safeURL
    • seq
    • sha
    • shuffle
    • singularize
    • slice
    • slicestr
    • sort
    • split
    • string
    • strings.HasSuffix
    • strings.Repeat
    • strings.RuneCount
    • strings.TrimLeft
    • strings.TrimPrefix
    • strings.TrimRight
    • strings.TrimSuffix
    • substr
    • symdiff
    • templates.Exists
    • time
    • title
    • transform.Unmarshal
    • trim
    • truncate
    • union
    • uniq
    • upper
    • urlize
    • urls.Parse
    • where
    • with
    • 图片函数
  • 变量
    • 变量概述
    • 网站变量
    • 简码变量
    • 页面变量
    • 页面方法
    • 分类变量
    • 文件变量
    • 菜单项属性
    • Hugo 变量
    • Git的变量
    • 网站地图变量
  • Hugo 管道
    • Hugo 管道概述
    • Hugo 管道简介
    • SASS / SCSS
    • PostCSS
    • 资产压缩
    • Asset 捆绑
    • 指纹和SRI
    • 来自模板资源
    • 从字符串资源
  • CLI
  • 故障排除
    • 疑难解答
    • FAQ
    • 构建性能
  • 工具
    • 开发工具概述
    • 迁移
    • 入门套件
    • 前端
    • 编辑器插件
    • 搜索
    • 其他的项目
  • 托管和部署
    • 托管和部署概述
    • Hugo 部署
    • 使用Nanobox的主机无关部署
    • AWS Amplify托管
    • Netlify托管
    • Render托管
    • Firebase托管
    • GitHub托管
    • GitLab托管
    • KeyCDN托管
    • Bitbucket托管
    • 使用Wercker部署
    • 使用rsync部署
  • 贡献
    • 贡献Hugo
    • 开发
    • 文档
    • 主题
  • 保养
“基本用法” was last updated: March 24, 2020: 添加翻译 (d6d8ad2)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • Discuss Source Code
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 
 

Hugo Sponsors

Logo for Forestry.io
Logo for Linode
Logo for eSolia
 

The Hugo logos are copyright © Steve Francia 2013–2020.

The Hugo Gopher is based on an original work by Renée French.

  • 新闻
  • 文档
  • 主题
  • 作品展示
  • 社区
  • GitHub
  • 关于 Hugo
  • 入门
  • Hugo 模块
  • 内容管理
  • 模板
  • 函数
  • 变量
  • Hugo 管道
  • CLI
  • 故障排除
  • 工具
  • 托管和部署
  • 贡献
  • 保养