Reputation: 24555
I see many codes like following on the net:
public static void read()
{
using (StreamReader m_StreamReader = new StreamReader("C:\\myCsv.Csv"))
{
while (m_StreamReader.Peek >= 0)
{
string m_Str = m_StreamReader.ReadLine;
string[] m_Splitted = m_Str.Split(new char[] { "," });
Console.WriteLine(string.Format("{0}={1}", m_Splitted[0], m_Splitted[1]));
}
}
}
However, I want to convert above to following:
public static void read() {
using (StreamReader m_StreamReader = new StreamReader("C:\\myCsv.Csv")) {
while (m_StreamReader.Peek >= 0) {
string m_Str = m_StreamReader.ReadLine;
string[] m_Splitted = m_Str.Split(new char[] { "," });
Console.WriteLine(string.Format("{0}={1}", m_Splitted[0], m_Splitted[1]));
}
}
}
Hence starting curly brace is taken to the end of previous line. How can this be done programmatically in Vim? It tried but though I can pick up starting curly brace but could not manage to take it to end of previous line.
Note: above code is from here.
Upvotes: 1
Views: 140
Reputation: 196606
Joining the next line to the current line is done with :help J
in normal mode or :help :join
in command-line mode.
Joining the current line to the previous line is done in normal mode by moving the cursor to the previous line with -
and then joining with J
. In command-line mode, you would use -
, short for .-1
("current line number minus one"), as :h address
for :join
: :-j
, which mirrors the normal mode method quite well.
To do this on the whole buffer, you need a way to execute a given command on every isolated opening brace. This is done with :help :g
:
:g/^\s*{\s*$/-j
Breakdown:
:g/<pattern>/<command>
executes <command>
on every line matching <pattern>
,^\s*{\s*$
matches lines with a single opening braces and optional leading and trailing whitespace,-j
joins the current line with the line above.But the result is not correctly indented anymore so you will need something like the following command to fix the mess:
gg=G
Breakdown:
gg
moves the cursor to line 1,=G
re-indents every line from the cursor to the last line, see :help =
.That said, switching from one coding style to another seems like something that should be done with a dedicated tool rather than with general text editing.
Upvotes: 5
Reputation: 8982
The desired output looks strikingly similar to the ratliff style. If that's correct, astyle
is one formatter that supports this particular style and this solution will be more robust than making changes manually.
You can wrap it in your own command as follows:
command! -buffer Fmt let winsaved = winsaveview() | execute '%! astyle --style=ratliff' | if v:shell_error > 0 | silent undo | endif | call winrestview(winsaved)
Upvotes: 0