jesseduffield/lazygit · error
command output contains newlines: %s
Error message
command output contains newlines: %s
What it means
TemplateFunctionRunCommand executes a shell command from a custom-command template ({{runCommand ...}}) and requires single-line output: trailing newlines are trimmed, then any remaining '\r\n' makes it fail, because the result is interpolated back into a git command line where embedded newlines would break or inject lines.
Source
Thrown at pkg/commands/git_commands/custom.go:36
}
// Only to be used for the sake of running custom commands specified by the user.
// If you want to run a new command, try finding a place for it in one of the neighbouring
// files, or creating a new BlahCommands struct to hold it.
func (self *CustomCommands) RunWithOutput(cmdStr string) (string, error) {
return self.cmd.New(str.ToArgv(cmdStr)).RunWithOutput()
}
// A function that can be used as a "runCommand" entry in the template.FuncMap of templates.
func (self *CustomCommands) TemplateFunctionRunCommand(cmdStr string) (string, error) {
output, err := self.RunWithOutput(cmdStr)
if err != nil {
return "", err
}
output = strings.TrimRight(output, "\r\n")
if strings.Contains(output, "\r\n") {
return "", fmt.Errorf("command output contains newlines: %s", output)
}
return output, nil
}
View on GitHub (pinned to c477a2959b)
Solutions
- Reduce the command to one line of output: add '| head -n1', 'awk NR==1', or make the query exact ('git rev-parse --short HEAD').
- Strip CR if the tool emits CRLF: pipe through 'tr -d "\r"' — the guard specifically looks for '\r\n' pairs.
- For list-like data, run the command in the custom command itself, not via runCommand.
Example fix
# before
command: 'echo {{runCommand "git branch --list main"}}'
# after
command: 'echo {{runCommand "git branch --list main | head -n1 | tr -d "\r""}}' Defensive patterns
Strategy: validation
Validate before calling
// In your own runCommand-style helpers, enforce single-line output yourself:
out, err := runWithOutput(cmd)
if err != nil {
return "", err
}
out = strings.TrimRight(out, "\r\n")
if strings.ContainsAny(out, "\r\n") {
return "", fmt.Errorf("expected single-line output from %q", cmd)
} Prevention
- Use runCommand only for single-value lookups (rev-parse, config get, head -n1).
- Pipe multi-line sources through 'head -n1' / 'awk NR==1' and 'tr -d "\r"' at the call site.
- Never interpolate raw command output into shell strings without newline/quote sanitization.
When it happens
Trigger: A custom command using runCommand whose command prints multiple lines (e.g. 'git branch --list', 'ls', any table output) — after trimming the final newline, interior CRLF pairs still remain and trigger the error.
Common situations: Piping a multi-line git query into a template placeholder; forgetting that the helper is designed for single-value lookups (version strings, hashes, names), not lists.
Related errors
- custom command prompt must have a type of 'input', 'menu', '
- This does not seem to be a git flow branch
- Must specify a remote if specifying a branch
- Expected renamed file
- gui.sidePanels: a side panel must have at least one tab.
AI-assisted analysis of jesseduffield/lazygit@c477a2959b (2026-08-15).
Data as JSON: /api/errors/075db546af072edf.
Report an issue: GitHub.