Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete the newline from a process output?

Tags:

elisp

I call git get the toplevel dir (according to Is there a way to get the git root directory in one command? ).

(let ((tmpbuffer (get-buffer-create (make-temp-name "git"))))
  (call-process "git" nil tmpbuffer nil "rev-parse" "--show-toplevel")
  (with-current-buffer tmpbuffer
    (with-output-to-string
      (princ (buffer-string))
      (kill-buffer))))

But there's a trailing newline in the string returned. I'm not sure how to get rid of it.

like image 490
Reactormonk Avatar asked Dec 28 '12 19:12

Reactormonk


People also ask

How do I get rid of new line in text file?

Open TextPad and the file you want to edit. Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text. Check the Regular Expression box.

How do you remove the trailing newline in Python?

Use the strip() Function to Remove a Newline Character From the String in Python. The strip() function is used to remove both trailing and leading newlines from the string that it is being operated on. It also removes the whitespaces on both sides of the string.

Does fgets read newline?

The fgets() function stores the result in string and adds a NULL character (\0) to the end of the string. The string includes the newline character, if read.


1 Answers

I think you can do

(replace-regexp-in-string "\n$" "" 
              (shell-command-to-string "git rev-parse --show-toplevel"))
like image 106
slitvinov Avatar answered Oct 09 '22 13:10

slitvinov