As a result of my previous question I decided to

alias groe= ?vim -c 'normal "+p1Gdddd"' -c ':%s/\n/' -c 'normal"gVGgJy"' -c'q!'?

I cant find out how to substitute '?s' in my example to make it work. Already tried question marks and apostrophes. Any Ideas would be appreciated. Thx

  • Try using hard (single) quotes for the question marks and then escaping the inner hard quotes with backslashes (\'). Maybe you also need to escape the backslash to work or escape both to make an escaped quote (i.e. \\' or \\\'). This depends on how nested the whole thing is. Better pretest your alias by just echoing it. Douple quotes within single quotes should be fine. – Fiximan Oct 25 '16 at 9:39

"Inside" single quotes you could have a single quote by closing the outer single quote, then add an escaped single quote and re-open the outer single quotes, like

echo 'It look'\''s ugly.'

In other words all ' will become '\'' plus outer single quotes.

Your alias would look like that:

alias groe='vim -c '\''normal "+p1Gdddd"'\'' -c '\'':%s/\n/'\'' -c '\''normal"gVGgJy"'\'' -c'\''q!'\'''

Or you could use C-style escapes $'...' where you have to escape single quotes only once but also escape character in \n:

echo $'It\'s a newline: \\n'
alias groe=$'vim -c \'normal "+p1Gdddd"\' -c \':%s/\\n/\' -c \'normal"gVGgJy"\' -c\'q!\''
  • Fantastic. It worked, but I it needed one more escape before '\n' to escape newline in the middle of the command ( or is it rather an expression?) – xi100f Oct 25 '16 at 11:08
  • Hm for me it seems work, see echo 'It look'\''s \n ugly.' I'll add another version using C-sytle escapes. – rudimeier Oct 25 '16 at 11:26
  • Yes you are right, it works in Bash. I tend to use Z shell and in it fails ( as well as in tcsh).. – xi100f Oct 25 '16 at 15:00

Make it a shell function, so you can lose the outer quotes.

groe() {
  vim -c 'normal "+p1Gdddd"' -c ':%s/\n/' -c 'normal"gVGgJy"' -c'q!' "$@"

That should call vim with the arguments -c, normal "+p1Gdddd", -c, :%s/\n/, -c, normal"gVGgJy", -cq!. The "$@" at the expands to the parameters you gave to the function, so groe foo.txt should work.

  • Yes, that worked for me too, but isn't it general rule that if you can contain command in one line you should use alias, otherwise define function? – xi100f Oct 27 '16 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.