1

In a Linux terminal, how can a newline (line feed?) be added without issuing a carriage return/issuing the command?

For example, in my case, I'd like to add several lines to a git commit comment, like so:

$ git commit -m "1. Removed comment blocks 2. updated .gitignore 3. added goto statement to hander --Miklas" 

How do I add these line feeds for a multiple-line comment without actually entering the command?

I've googled around and tried a number of things (shift+return, alt+return, ctrl+return.. etc), but no luck. Tyvm Keith :^)

11
  • 2
    Don't provide the message "inline" at the command-line, and let git commit invoke the standard editor of yours ($EDITOR) so you can edit the message using any formatting you'd like. Commented Dec 2, 2020 at 16:19
  • Just type git commit -m "message<enter><enter>More description"<enter> Commented Dec 2, 2020 at 16:22
  • 1
    And remember that the first line is used as a summary of the commit. If you want to add more details do it as a separate paragraph. Commented Dec 2, 2020 at 16:23
  • 2
    and end of the comment block then don't. Type "something<enter> not "something"<enter>. If you leave " quotation open, the shell should show > and let you type on the next line. Commented Dec 2, 2020 at 16:25
  • 1
    You can also use $'1.Removed comment blocks\n2. updated .gitignore\n3. added goto statement' Commented Dec 2, 2020 at 16:28

2 Answers 2

3

Shells generally come with a support of multiline continuation of commands. If shell "detects" that the previous command is not "complete" and you typed enter, it will print PS2 and let you continue inputting a command. Default PS2="> ".

# I type: # git commit -m "message<enter><enter>description"<enter> $ git commit -m "message > > description" no changes added to commit 

It works also for with shell operators, like for, while, if, case:

$ for i in 1 2 3 > do > echo $i > done 1 2 3 

I found a youtube video that shows the behavior.

You may also use C-ish quoting in bash:

git commit -m $'message\n\ndescription\n' 

I sometimes use process subtitution with printf:

git commit -m "$(printf "%s\n" "message" "" "description")" 

And finally you may type just:

git commit 

that should open a full file editor, by default vim. In that editor type the multiline message you want, save and quit the editor and the file content will be taken as the commit message.

Sign up to request clarification or add additional context in comments.

Comments

0

The following worked on the default Ubuntu (24.04.1) terminal using bash as the shell interpreter.

The Ctrl-V solution

This is the "what you see is what you get" (WYSIWYG) solution that I think you're looking for since you mentioned trying "shift+return, alt+return, ctrl+return.. etc".

Just try Ctrl+V followed by Ctrl+J to get a new line on your terminal.

This should place the cursor on your terminal at the beginning of a new line allowing you to continue introducing text inside your double quoting just like your example shows (an actual multiline block of text as if you're inside a text editor). That combination will pass the equivalent of a line feed character ('\n') to the command.

$ echo "line1 line2 line3" >multiline_from_ctrlvj $ cat multiline_from_ctrlvj line1 line2 line3 $ 

Related to this, entering Ctrl+V followed by TAB will "print" a tabulation on your terminal and send a '\t' character to the shell

$ echo "one two three four" one two three four $ 

Entering Ctrl+V followed by ENTER will print a '^M' on your terminal but will send a carriage return to the shell":

$ echo "this will be overwritten by^MTHIS" THIS will be overwritten by $ 

The ANSI C quoting solution

(already pointed out by @KamilCuk)

If you don't care about how your terminal looks like while typing but just want to insert the newline as part of the argument string, you can always use the ANSI C quoting style as an alternative to the double quoting. The ANSI C quoting style requires a leading $ sign and simple quotes:

$'any text including ANSI C special characters such as '\n' or others'

$ echo $'line1\nline2' >multiline_from_ANSI_C_quoting $ cat multiline_from_ANSI_C_quoting line1 line2 $ 

Notice that the $'...' quoting must be used instead double quoting "...", since something like this won't work as desired (double quoting removes the special meaning of the $ sign for the shell):

$ echo "line1$'\n'line2" line1$'\n'line2 $ 

The command substitution solution

(already pointed out by @KamilCuk)

Whatever you put inside $(...) gets executed in a subshell and its output (not exactly but irrelevant here) is used to replace the entire $(...)construction. That is why this is also an option:

git commit -m "$(printf "%s\n" "message" "" "description")" 

Notice that any double quoting inside $(...) doesn't affect the outer double quoting.

PD: The recommended way to commit

A well documented commit should have a title (single short line) and a body (multiline block). When you use the git commit -m "brief title of the commit" you're just attaching a title to the commit while leaving its body empty. As pointed out by @KamilCuk, you should have your git configured to use a text editor (such as vim). If that's the case, entering just git commit will open the text editor where you must: 1) enter the first line as the commit title; 2) leave the second line empty; 3) start your commit detailed multiline description from the third line; 4) save and quit the text editor; then the commit will be done.

Check your .gitconfig file for something like this to confirm if you have an associated text editor for git:

[core] editor = vim 

Execute something like this to configure a text editor for git:

$ git config --global core.editor "vim" 

2 Comments

$'…' quoting isn't a standard shell feature; you should mention which shells support it.
@TobySpeight Thanks for your reply, and sorry for the confusion. I mentioned the context (shell: Bash; terminal: Ubuntu's default (i.e. GNOME Terminal 3.52.0 on GNOME 46)) only at the beginning of my post, but the entire content assumes and applies to that context. The $'...' quoting is known as ANSI C quoting, and it's documented in the GNU Bash manual (for more details: gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting). I’m not sure how widely this feature is supported across other shell implementations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.