I'm trying to integrate Rust Analyzer with a browser based editor.
My first step is to run Rust Analyzer directly from a terminal and send requests via stdio.
$ rust-analyzer
> Content-Length:207\r\n\r\n{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}
But I got this error:
[ERROR rust_analyzer] Unexpected error: expected initialize request, got Err(RecvError)
expected initialize request, got Err(RecvError)
What am I missing here?
It's just because the four literal characters \ r \ n are not transformed into the two special characters \r and \n when you input them directly in the terminal.
In order to experiment by hand in the terminal, you should transform the line ending.
$ sed -u -re 's/^(.*)$/\1\r/' | rust-analyzer
Content-Length: 207
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}
... then the response is displayed here ...
Note that we press the Enter key here; we do not try to input the \n special character.
And on a second thought, I think that in this case (interactive terminal) you should provide Content-Length: 209 because the json content will be ended with \r\n (two more bytes, ignored as separators).
This way, the next request can be input on the next line.
If you keep 207, then your next request should start on the same line as the json content (right after the last }).
Another solution would be to change the settings of the terminal.
stty -icrnl makes the Enter key produce the \r (control-M) character; you then have to input control-J to produce the \n character.
$ stty -icrnl
$ rust-analyser
Content-Length: 208^M <-- Enter + control-J
^M <-- Enter + control-J
{"jsonrpc":"2.0", ... ,"capabilities":{}}} <-- control-J
... the response is displayed here ...
Related
I'm using the google-http-client for a project at work and when I do some requests I have the following thing printed on my console.
curl -v --compressed -X POST -H 'Accept-Encoding: gzip' -H 'User-Agent: Google-HTTP-Java-Client/1.23.0 (gzip)' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -d '#-' -- 'http://example.com' << $$$
I was wondering what << $$$ mean.
If I try to run this command into a linux terminal seems that << $$$ makes the console to wait for more input. If that's the case, how can I specify to the terminal that I'm done feeding inputs to it?
Later edit: I have found that curl arguments -d #- implies that data will be red from the stdin.
This is a "here-document" with an unusual end marker.
A here-document is a type of redirection, and usually looks like
utility <<MARKER
document
content
goes here
MARKER
That is, it feeds a document delimited by MARKER to the utility on its standard input.
This is like utility <file where file contains the lines in the here-document, except that the shell will do variable expansion and command substitution on the text of the document (this may be prevented by quoting the marker as either \MARKER or 'MARKER' at the start).
The here-document marker can be any word, but $$$ is a highly unusual choice of word for it. As $ has a special meaning in the shell, using $ in the marker is, or may be, confusing to the reader.
If you type
somecommand <<stuff
in the shell, the shell expects you to give the rest of the contents of the here-document, and then the word stuff on a line by itself. That's how you signal end of input in a here-document.
I'm trying to replace each , in the current file by a new line:
:%s/,/\n/g
But it inserts what looks like a ^# instead of an actual newline. The file is not in DOS mode or anything.
What should I do?
If you are curious, like me, check the question Why is \r a newline for Vim? as well.
Use \r instead of \n.
Substituting by \n inserts a null character into the text. To get a newline, use \r. When searching for a newline, you’d still use \n, however. This asymmetry is due to the fact that \n and \r do slightly different things:
\n matches an end of line (newline), whereas \r matches a carriage return. On the other hand, in substitutions \n inserts a null character whereas \r inserts a newline (more precisely, it’s treated as the input CR). Here’s a small, non-interactive example to illustrate this, using the Vim command line feature (in other words, you can copy and paste the following into a terminal to run it). xxd shows a hexdump of the resulting file.
echo bar > test
(echo 'Before:'; xxd test) > output.txt
vim test '+s/b/\n/' '+s/a/\r/' +wq
(echo 'After:'; xxd test) >> output.txt
more output.txt
Before:
0000000: 6261 720a bar.
After:
0000000: 000a 720a ..r.
In other words, \n has inserted the byte 0x00 into the text; \r has inserted the byte 0x0a.
Here's the trick:
First, set your Vi(m) session to allow pattern matching with special characters (i.e.: newline). It's probably worth putting this line in your .vimrc or .exrc file:
:set magic
Next, do:
:s/,/,^M/g
To get the ^M character, type Ctrl + V and hit Enter. Under Windows, do Ctrl + Q, Enter. The only way I can remember these is by remembering how little sense they make:
A: What would be the worst control-character to use to represent a newline?
B: Either q (because it usually means "Quit") or v because it would be so easy to type Ctrl + C by mistake and kill the editor.
A: Make it so.
In the syntax s/foo/bar, \r and \n have different meanings, depending on context.
Short:
For foo:
\r == "carriage return" (CR / ^M)
\n == matches "line feed" (LF) on Linux/Mac, and CRLF on Windows
For bar:
\r == produces LF on Linux/Mac, CRLF on Windows
\n == "null byte" (NUL / ^#)
When editing files in linux (i.e. on a webserver) that were initially created in a windows environment and uploaded (i.e. FTP/SFTP) - all the ^M's you see in vim, are the CR's which linux does not translate as it uses only LF's to depict a line break.
Longer (with ASCII numbers):
NUL == 0x00 == 0 == Ctrl + # == ^# shown in vim
LF == 0x0A == 10 == Ctrl + J
CR == 0x0D == 13 == Ctrl + M == ^M shown in vim
Here is a list of the ASCII control characters. Insert them in Vim via Ctrl + V,Ctrl + ---key---.
In Bash or the other Unix/Linux shells, just type Ctrl + ---key---.
Try Ctrl + M in Bash. It's the same as hitting Enter, as the shell realizes what is meant, even though Linux systems use line feeds for line delimiting.
To insert literal's in bash, prepending them with Ctrl + V will also work.
Try in Bash:
echo ^[[33;1mcolored.^[[0mnot colored.
This uses ANSI escape sequences. Insert the two ^['s via Ctrl + V, Esc.
You might also try Ctrl + V,Ctrl + M, Enter, which will give you this:
bash: $'\r': command not found
Remember the \r from above? :>
This ASCII control characters list is different from a complete ASCII symbol table, in that the control characters, which are inserted into a console/pseudoterminal/Vim via the Ctrl key (haha), can be found there.
Whereas in C and most other languages, you usually use the octal codes to represent these 'characters'.
If you really want to know where all this comes from: The TTY demystified. This is the best link you will come across about this topic, but beware: There be dragons.
TL;DR
Usually foo = \n, and bar = \r.
You need to use:
:%s/,/^M/g
To get the ^M character, press Ctrl + v followed by Enter.
\r can do the work here for you.
With Vim on Windows, use Ctrl + Q in place of Ctrl + V.
This is the best answer for the way I think, but it would have been nicer in a table:
Why is \r a newline for Vim?
So, rewording:
You need to use \r to use a line feed (ASCII 0x0A, the Unix newline) in a regex replacement, but that is peculiar to the replacement - you should normally continue to expect to use \n for line feed and \r for carriage return.
This is because Vim used \n in a replacement to mean the NIL character (ASCII 0x00). You might have expected NIL to have been \0 instead, freeing \n for its usual use for line feed, but \0 already has a meaning in regex replacements, so it was shifted to \n. Hence then going further to also shift the newline from \n to \r (which in a regex pattern is the carriage return character, ASCII 0x0D).
Character | ASCII code | C representation | Regex match | Regex replacement
-------------------------+------------+------------------+-------------+------------------------
nil | 0x00 | \0 | \0 | \n
line feed (Unix newline) | 0x0a | \n | \n | \r
carriage return | 0x0d | \r | \r | <unknown>
NB: ^M (Ctrl + V Ctrl + M on Linux) inserts a newline when used in a regex replacement rather than a carriage return as others have advised (I just tried it).
Also note that Vim will translate the line feed character when it saves to file based on its file format settings and that might confuse matters.
From Eclipse, the ^M characters can be embedded in a line, and you want to convert them to newlines.
:s/\r/\r/g
But if one has to substitute, then the following thing works:
:%s/\n/\r\|\-\r/g
In the above, every next line is substituted with next line, and then |- and again a new line. This is used in wiki tables.
If the text is as follows:
line1
line2
line3
It is changed to
line1
|-
line2
|-
line3
Here's the answer that worked for me. From this guy:
----quoting Use the vi editor to insert a newline char in replace
Something else I have to do and cannot remember and then have to look up.
In vi, to insert a newline character in a search and replace, do the following:
:%s/look_for/replace_with^M/g
The command above would replace all instances of “look_for” with “replace_with\n” (with \n meaning newline).
To get the “^M”, enter the key combination Ctrl + V, and then after that (release all keys) press the Enter key.
If you need to do it for a whole file, it was also suggested to me that you could try from the command line:
sed 's/\\n/\n/g' file > newfile
in vim editor the following command successfully replaced \n with new line
:%s/\\n/\r/g
i am running my application in linux by providing inputs as command line. My input field contain an argument which contains ";"(semicolon) internally.(For example:123;434;5464).
This will be parsed using UTF8String encode and send.
But when i am using like this, in initial itself i am getting,
bash: 434: command not found
bash: 5464: command not found
And when i capture traffic the output contains only 123 instead 123;434;5464
But if i give without semicolon (Ex:123:434:5464),not getting any problem output coming properly as 123:434:5464
Point me how to give command line input by using semicolon as to come output. Is there any particular syntax to use while doing with semicolon.
I am running like below
./runASR.sh -ip 10.78.242.4 -port 3868 -sce 10.78.241.206 -id 85;167838865;1385433280
where -id field contain that value with issue.
; is treated an end of command character. So 123;456;5464 to bash is in fact 3 commands. To pass such meta-characters escape it with escape character \.
./command 123\;456\;5464
Or Just quote it with single quote (double quote evaluates the inner string) (Thanks Triplee, I forgot to mention this)
./command '123;456;5464'
I wrote a little Bash script and I'm having a problem while reading from the command line. I think its because I wrote the script on Windows. Here is the code:
read NEW_MODX_PROJECT
and the output of the debug mode
+ read $'NEW_MODX_PROJECT\r'
Finally here the error I get
': Ist kein gültiger Bezeichner.DX_PROJECT
I think in English it should mean "': is not a valid identifier.DX_PROJECT"
While writing it on Windows, it worked fine. I used console2 to test it which is using the sh.exe.
Your assertion is correct -- Windows uses CRLF line separators but Linux just uses a LF.
The reason for your strange error message is that while printing the name of your variable, it includes the carriage return as part of its name -- the terminal then jumps back to the first column to print the rest of the error message (which overwrites the beginning of the message with the end of it).
There are a set of utilities known as dos2unix and unix2dos which you can use to easily convert between formats, e.g.:
dos2unix myscript.sh
If you don't happen to have them, you can achieve the same using tr:
tr -d '\r' < myscript.sh > myscript-new.sh
Either will strip all the carriage returns and should un-confuse things.
I'm trying to replace each , in the current file by a new line:
:%s/,/\n/g
But it inserts what looks like a ^# instead of an actual newline. The file is not in DOS mode or anything.
What should I do?
If you are curious, like me, check the question Why is \r a newline for Vim? as well.
Use \r instead of \n.
Substituting by \n inserts a null character into the text. To get a newline, use \r. When searching for a newline, you’d still use \n, however. This asymmetry is due to the fact that \n and \r do slightly different things:
\n matches an end of line (newline), whereas \r matches a carriage return. On the other hand, in substitutions \n inserts a null character whereas \r inserts a newline (more precisely, it’s treated as the input CR). Here’s a small, non-interactive example to illustrate this, using the Vim command line feature (in other words, you can copy and paste the following into a terminal to run it). xxd shows a hexdump of the resulting file.
echo bar > test
(echo 'Before:'; xxd test) > output.txt
vim test '+s/b/\n/' '+s/a/\r/' +wq
(echo 'After:'; xxd test) >> output.txt
more output.txt
Before:
0000000: 6261 720a bar.
After:
0000000: 000a 720a ..r.
In other words, \n has inserted the byte 0x00 into the text; \r has inserted the byte 0x0a.
Here's the trick:
First, set your Vi(m) session to allow pattern matching with special characters (i.e.: newline). It's probably worth putting this line in your .vimrc or .exrc file:
:set magic
Next, do:
:s/,/,^M/g
To get the ^M character, type Ctrl + V and hit Enter. Under Windows, do Ctrl + Q, Enter. The only way I can remember these is by remembering how little sense they make:
A: What would be the worst control-character to use to represent a newline?
B: Either q (because it usually means "Quit") or v because it would be so easy to type Ctrl + C by mistake and kill the editor.
A: Make it so.
In the syntax s/foo/bar, \r and \n have different meanings, depending on context.
Short:
For foo:
\r == "carriage return" (CR / ^M)
\n == matches "line feed" (LF) on Linux/Mac, and CRLF on Windows
For bar:
\r == produces LF on Linux/Mac, CRLF on Windows
\n == "null byte" (NUL / ^#)
When editing files in linux (i.e. on a webserver) that were initially created in a windows environment and uploaded (i.e. FTP/SFTP) - all the ^M's you see in vim, are the CR's which linux does not translate as it uses only LF's to depict a line break.
Longer (with ASCII numbers):
NUL == 0x00 == 0 == Ctrl + # == ^# shown in vim
LF == 0x0A == 10 == Ctrl + J
CR == 0x0D == 13 == Ctrl + M == ^M shown in vim
Here is a list of the ASCII control characters. Insert them in Vim via Ctrl + V,Ctrl + ---key---.
In Bash or the other Unix/Linux shells, just type Ctrl + ---key---.
Try Ctrl + M in Bash. It's the same as hitting Enter, as the shell realizes what is meant, even though Linux systems use line feeds for line delimiting.
To insert literal's in bash, prepending them with Ctrl + V will also work.
Try in Bash:
echo ^[[33;1mcolored.^[[0mnot colored.
This uses ANSI escape sequences. Insert the two ^['s via Ctrl + V, Esc.
You might also try Ctrl + V,Ctrl + M, Enter, which will give you this:
bash: $'\r': command not found
Remember the \r from above? :>
This ASCII control characters list is different from a complete ASCII symbol table, in that the control characters, which are inserted into a console/pseudoterminal/Vim via the Ctrl key (haha), can be found there.
Whereas in C and most other languages, you usually use the octal codes to represent these 'characters'.
If you really want to know where all this comes from: The TTY demystified. This is the best link you will come across about this topic, but beware: There be dragons.
TL;DR
Usually foo = \n, and bar = \r.
You need to use:
:%s/,/^M/g
To get the ^M character, press Ctrl + v followed by Enter.
\r can do the work here for you.
With Vim on Windows, use Ctrl + Q in place of Ctrl + V.
This is the best answer for the way I think, but it would have been nicer in a table:
Why is \r a newline for Vim?
So, rewording:
You need to use \r to use a line feed (ASCII 0x0A, the Unix newline) in a regex replacement, but that is peculiar to the replacement - you should normally continue to expect to use \n for line feed and \r for carriage return.
This is because Vim used \n in a replacement to mean the NIL character (ASCII 0x00). You might have expected NIL to have been \0 instead, freeing \n for its usual use for line feed, but \0 already has a meaning in regex replacements, so it was shifted to \n. Hence then going further to also shift the newline from \n to \r (which in a regex pattern is the carriage return character, ASCII 0x0D).
Character | ASCII code | C representation | Regex match | Regex replacement
-------------------------+------------+------------------+-------------+------------------------
nil | 0x00 | \0 | \0 | \n
line feed (Unix newline) | 0x0a | \n | \n | \r
carriage return | 0x0d | \r | \r | <unknown>
NB: ^M (Ctrl + V Ctrl + M on Linux) inserts a newline when used in a regex replacement rather than a carriage return as others have advised (I just tried it).
Also note that Vim will translate the line feed character when it saves to file based on its file format settings and that might confuse matters.
From Eclipse, the ^M characters can be embedded in a line, and you want to convert them to newlines.
:s/\r/\r/g
But if one has to substitute, then the following thing works:
:%s/\n/\r\|\-\r/g
In the above, every next line is substituted with next line, and then |- and again a new line. This is used in wiki tables.
If the text is as follows:
line1
line2
line3
It is changed to
line1
|-
line2
|-
line3
Here's the answer that worked for me. From this guy:
----quoting Use the vi editor to insert a newline char in replace
Something else I have to do and cannot remember and then have to look up.
In vi, to insert a newline character in a search and replace, do the following:
:%s/look_for/replace_with^M/g
The command above would replace all instances of “look_for” with “replace_with\n” (with \n meaning newline).
To get the “^M”, enter the key combination Ctrl + V, and then after that (release all keys) press the Enter key.
If you need to do it for a whole file, it was also suggested to me that you could try from the command line:
sed 's/\\n/\n/g' file > newfile
in vim editor the following command successfully replaced \n with new line
:%s/\\n/\r/g