Setting the ghci prompt with colors - haskell

I try to use terminal colors within my ghci prompt.
So when I open ghci and try to:
Prelude> :set prompt '\[\033[1haskell > \033[0m\]'
'\[\033[1\]haskell> \[\033[0m\] '
I know that these codes are interpreted by bash with echo and the -e flag. But how can I do this within ghci?

According to https://wiki.haskell.org/GHCi_in_colour, you can use
:set prompt "\ESC[33m\STXhaskell > \ESC[m\STX"
A few notes of explanation:
Only a double-quoted string is treated specially; single quotes are treated as part of the prompt.
The double-quoted string follows Haskell practice.
\STX corresponds to the \] of your bash prompt; it's not clear why GHCi does not require the equivalent of \[ as well. (Perhaps it does; I haven't played with this much.). See http://trac.haskell.org/haskeline/wiki/ControlSequencesInPrompt for an explanation.

Haskell character escape codes are in decimal:
Prelude> :set prompt "\027[31mhaskell>\027[0m "
Putting the same directive in your .ghci file should also work.

Related

Backslashes are removed unless quoted in command line flags

I'm using the flag package to interpret flags entered at the command line.
I created a variable using
ptrString := flag.String("string", "", "A test string")
flat.Parse()
Then when I want to print it,
fmt.Println("You entered " + *ptrString)
If I enter something like -string=hello! as a command line argument, it prints "hello!"
If I enter something like -string=hello\Bob as a command line argument, it prints "helloBob"
Is there a recommended way to convert or interpret the flag argument to a string that doesn't remove the backslash? (This is being tested on Linux and OS X, if the shell is interfering...)
Characters that have special meaning in the shell need to be quoted or escaped. You can find complete list in the shell's man pages (under "Quoting" in man 1 bash).
In this case, you can either quote or escape the baskslash
-string=hello\\Bob
// or
-string='hello\Bob'

Why my gdb prompt shows wrong after I change its color

I change my gdb prompt's color by writing set prompt \033[1;33m(gdb) \033[0m into .gdbinitfile. And I change my gdb prompt's color sucessfully.
But I find that my long command with my parameters will overwrite my prompt after I input a long command without going to newline. Why?
Edit: if your gdb has python scripting enabled, look at #matt's answer to see how to do this using the set extended-prompt command - it's a better solution.
Gdb manages command input by using the readline package. The way to tell readline that a character sequence in a prompt string doesn't actually move the cursor when output to the screen is to surround it with the markers RL_PROMPT_START_IGNORE (currently '\001' in readline's C header file) and RL_PROMPT_END_IGNORE (currently '\002').
Bash has a portable way of expressing this: when it sees "\[" and "\]" in the prompt variable, it will convert them to RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE. Bash does this while it's processing various other escape sequences such as \w to include the current working directory.
Gdb's set prompt command doesn't support "\[" and "\]", but you can put the octal escapes \001 and \002 in your set prompt string (subject to change if readline's authors ever choose to use a different set of markers).
set prompt \001\033[1;33m\002(gdb) \001\033[0m\002
gdb has a command: set extended-prompt that accepts escape sequences described in the gdb.prompt python module. which includes the \[ and \] bashism's for wrapping characters that do not contribute to the prompts length. This of course requires a python enabled gdb to work though.

what does the number 1 in the shellescape function mean in vim?

A vim command confuses me. I have read and re-read :help shellescape() several times. I still don't understand the meaning of the number 1 in shellescape(expand('%:p'), 1).
Here's the full command I'm trying to understand:
:nnoremap <F4> :exe ':silent !"c:\Program Files\Mozilla Firefox\firefox.exe"'shellescape(expand('%:p'), 1)<CR>
Let's break down this long command piece by piece.
the command is to map an exe command into F4 in the whole.
:exe is to execute some command.
:silent ! is to execute a shell command silently
"c:\Program Files\Mozilla Firefox\firefox.exe" can call my firefox program.
shellescape(), there are blanks to be escaped.
expand('%:p') can get current file name in full expression that is path + filename.
What does this excerpt from the help page mean?
With a |non-zero-arg| {special} and 'shell' containing "csh" in the tail it's
escaped a second time.
Is there some meaning such the same as 1 or 2 in s/(ha.*)(world)/\2\1/g?
please take a simple example in detail.
And i have two questions related to the topic.
1.In which way i can get how many type of shells in my gvim?
2.In which situation can i change 1 into 2 in shellescape()?
:nnoremap <F4> :exe ':silent !"c:\Program Files\Mozilla Firefox\firefox.exe"'shellescape(expand('%:p'), 2)<CR>
There are basically two different uses for shellescape(); one is the Vimscript system() function, the other the :! Ex command. While most escaping needs are identical in both uses (e.g. to deal with whitespace, arguments must be enclosed in quotes), the :! command requires some additional escaping, because on the command-line, Vim assigns special meaning to symbols like % (replacing it with the current file name). This does not happen for system().
Therefore, to tell shellescape() which escaping mode to use, it has the additional {special} argument; if you pass 1 (or any other value that evaluates to "true"), the additional characters are escaped, too.
TL;DR: Pass 1 for :! commands, 0 or omit the argument for use with system().
From the help for shellescape():
shellescape({string} [, {special}]) shellescape()
Escape {string} for use as a shell command argument.
On MS-Windows and MS-DOS, when 'shellslash' is not set, it
will enclose {string} in double quotes and double all double
quotes within {string}.
For other systems, it will enclose {string} in single quotes
and replace all "'" with "'\''".
When the {special} argument is present and it's a non-zero
Number or a non-empty String (non-zero-arg), then special
items such as "!", "%", "#" and "<cword>" will be preceded by
a backslash. This backslash will be removed again by the :!
command.
The 1 in your example simply tells shellescape() to escape special characters with a backslash. If you were to have (say) a ! in the path returned by expand(), it’d be replaced with \!.
shell is an option:
'shell' 'sh' E91
'shell' 'sh' string (default $SHELL or "sh",
MS-DOS and Win32: "command.com" or
"cmd.exe", OS/2: "cmd")
global
Name of the shell to use for ! and :! commands.
— :help 'shell'
“With a non-zero-arg {special} and 'shell' containing "csh" in the tail it's escaped a second time” means that if, as in your example, shellescape() is given a non-zero argument for special (your example has a 1), it will check shell for that "csh", and if it finds it (if your shell is set to something containing csh) it will double-escape it.
EDIT to specifically answer two questions added to (edited) original post:
You can get your shell setting (referred to in the shellescape() help quote) using :echo &shell.
2 is a Number and is non-zero. You should therefore be able to substitute 2 for the 1 in your example and get the same result.

Is there different between single quote and double quote in vim command mode?

In my vim, I can use :%!sed "s/^/ /", got the wrong output when I use :%!sed 's/^/ /' .
sed: -e expression #1, char 0: no previous regular expression
Is there differences between single quote and double quote in vim command mode?
In my sed, single quote is the same as double quote.
$ echo "wha012" | sed 's/w/haha/'
hahaha012
$ echo "wha012" | sed "s/w/haha/"
hahaha012
my system is xp+vim 7.3 for windows.
In my system:
[1] "c://cygwin/bin/ash.exe"
[2] "c://cygwin/bin/bash.exe"
[3] "c://cygwin/bin/dash.exe"
[4] "c://cygwin/bin/sh.exe"
if i set set shell=\"c:\cygwin\bin\sh.exe"\ -f in _vimrc,i get the new wrong messages:
sed command can not found.
Funny, when I try :%!sed "/^/ /" I get the same error message as when I use single quotes:
sed: 1: "/^/ /": invalid command code /
(This line replaces the content of my file.) I expect to get an error message there because, as #Birei pointed out, you left out the sed s command. This works as expected, with either single or double quotes:
:%!sed "s/^/ /"
#Birei is also right that you can use vim to do things like this, but I assume you have simplified the example from what you were really trying to do.
To answer the original question, Vim uses single quotes for literal strings. The only special character in a literal string is ' itself. Strings delimited with double quotes use \ to denote special character, such as `"\<Esc>".
:echo 'a''b' == "a'b"
:help expr-string
:help literal-string
my system is xp+vim 7.3 for windows
By default Vim uses cmd.exe to run :! commands on Windows, which behaves differently with regard to quoting from the POSIX shell that your s/w/haha/ examples suggest you've been testing with. Try something like
:set shell=\"C:\path\to\sh.exe\"\ -f
to tell it to use your POSIX shell instead. Or if you're using cygwin then try the cygwin version of vim instead of the Windows native one.
The difference is in the sed command, that lets interpolate variables when you execute it directly from the shell, like:
sed "s/$pattern/$replacement/"
but your problem is that you have to use a substitution command that begins with letter s, like:
:%!sed "s/^/ /"
Also you can have same behaviour inside vim without an external command, like:
:%s/^/ /

vim expanded makeprg variable

I use the :set makeprg functionality to set different make behaviour depending on files. One feature of this is that I can use % and %< in order to refer to the file in the active buffer, as well as $ for environment variables.
I'd like to echo the expanded makeprg variable, but I can't seem to achieve this.
For example, suppose I have :set makeprg=build\ %, and I'm working on file Foo.txt.
I would expect the output of echoing an expanded makeprg to be:
build Foo.txt
However, we have the following result when echoing the &makeprg variable:
:echo(&makeprg)
build\ %
The solution probably involves using expand(), except that this would involve parsing the &makeprg for escaped symbols and dealing with $ appropriately for environment variables. Is there a solution to this that I'm missing?
Why would you need this? For troubleshooting, I would probably just append echo to 'makeprg' (that's a trailing space after \):
:setl makeprg^=echo\
But the following should do what you had in mind. The trick is to split the 'makeprg' string on spaces, and process each word individually:
:echo join(map(split(&makeprg), 'expand(v:val)'))
A bit closer to the truth:
join(map(split(mystring, '\ze[<%#]'), 'expand(v:val)'), '').

Resources