I have big portions of code where functions are formatted with the opening curly brace on the next line, like this :
function foo()
{
}
I'm trying to figure out how to search / replace in order to change it to this :
function foo(){
}
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI have big portions of code where functions are formatted with the opening curly brace on the next line, like this :
function foo()
{
}
I'm trying to figure out how to search / replace in order to change it to this :
function foo(){
}
You can use the new line character is your search (\n
for linux):
:%s/)\s*\n{/){/c
Replaces )\s*\n{
into ){
The c
flag at the end will ask you to confirm before each replacement.
EDIT:
Another solution would be to use the join line feature (J
) with the g
command:
:g/function [^{]\+$/join
Which will execute the command join
on every line matching the pattern function [^{]\+$
("function" followed by anything without a {
).
It will however put a space by default between )
and {
, it can be changed by running the following before the g command:
:set nojoinspaces
formatprg
or use a plugin such as github.com/prettier/vim-prettier or github.com/sbdchd/neoformat. – Hotschke Apr 3 '19 at 15:13