Suppose that my pastetoggle is set to <F10>, if I run echo &pastetoggle it prints out <80>k; (Question number 1) how can I reach its value as string "<F10>" instead of this <80>k; code. I mean is it possible to set a variable to "<F10>" based on the value of pastetoggle?
Now if I run let #a=&pastetoggle and then run echo #a it prints out the same <80>k; string, but if I run let &pastetoggle=#a afterwards and run echo &pastetogggle it prints out <80><fe>Xk; (Question number 2) why? (Question number 3) how can I set pastetoggle based on value in #a?
The <80>k; is the internal keycode representation of <F10>; unfortunately, as you've found out, it cannot be saved and then reassigned to &pastetoggle.
You can get the "actual" value via
:set pastetoggle?
To capture that, you'd have to use :redir and string extraction:
redir => setOutput
silent! set pastetoggle?
redir END
let pasteToggleKey = matchstr(setOutput, 'pastetoggle=\zs.*')
echo pasteToggleKey
Related
I want to execute an external program in lua. Usually this can be done with
os.execute("run '"..arg0.."' 'arg1' arg2")
The problem with this approach is if I want to pass user input as string to an external program, user input could be '; evil 'h4ck teh system' ' and the script from above would execute like this:
/bin/bash -c "run ''; evil 'h4ck teh system' '' 'arg1' arg2"
Another problem occurs when I have '$var' as argument and the shell replaces this with its environment variable. In my particular case I have something like [[program 'set title "$My Title$"']] – so nested strings – and program parses "$My Title$" (with escape sequences) differently than '$My Title$' (as it is). Because I want to set the title as it, the best way is to have arguments like this: 'My Title'. But now the command have to be:
os.execute([[run "set title '$My Title$'"]])
But now – as I said – $My will be replaced with an empty string, because the environment does not know any variable named $My and because, I never wanted it to be replaced.
So I am looking for the usual approach with
execv("run", {"set title '"..arg0.."'", arg1, arg2})
local safe_unquoted = "^[-~_/.%w%%+,:#^]*$"
local function q(text, expand) -- quoting under *nix shells
-- "expand"
-- false/nil: $var and `cmd` must NOT be expanded (use single quotes)
-- true: $var and `cmd` must be expanded (use double quotes)
if text == "" then
text = '""'
elseif not text:match(safe_unquoted) then
if expand then
text = '"'..text:gsub('["\\]', '\\%0')..'"'
else
local new_text = {}
for s in (text.."'"):gmatch"(.-)'" do
new_text[#new_text + 1] = s:match(safe_unquoted) or "'"..s.."'"
end
text = table.concat(new_text, "\\'")
end
end
return text
end
function execute_commands(...)
local all_commands = {}
for k, command in ipairs{...} do
for j = 1, #command do
if not command[j]:match"^[-~_%w/%.]+$" then
command[j] = q(command[j], command.expand)
end
end
all_commands[k] = table.concat(command, " ") -- space is arguments delimiter
end
all_commands = table.concat(all_commands, ";") -- semicolon is commands delimiter
return os.execute("/bin/bash -c "..q(all_commands))
end
Usage examples:
-- Usage example #1:
execute_commands(
{"your/program", "arg 1", "$arg2", "arg-3", "~/arg4.txt"},
{expand=true, "echo", "Your program finished with exit code $?"},
{"ls", "-l"}
)
-- The following command will be executed:
-- /bin/bash -c 'your/program '\''arg 1'\'' '\''$arg2'\'' arg-3 ~/arg4.txt;echo "Your program finished with exit code $?";ls -l'
$arg2 will NOT be expanded into value because of single quotes around it, as you required.
Unfortunately, "Your program finished with exit code $?" will NOT be expanded too (unless you explicitly set expand=true).
-- Usage example #2:
execute_commands{"run", "set title '$My Title$'", "arg1", "arg2"}
-- the generated command is not trivial, but it does exactly what you need :-)
-- /bin/bash -c 'run '\''set title '\''\'\'\''$My Title$'\''\'\'' arg1 arg2'
In substitution or deletion of sub string from original string, usually uses this form in windows CMD.
set result=%original:<strings_to_be_removed>=<strngs_to_be_newly_substituted>%
So it works for many situation as follows ..
set "original=Questions that may already have your answer"
set "you=%original:that=you%"
set you
you=Questions you may already have your answer
set challenge=%original:Questions=Challenges%
set challenge
challenge=Challenges that may already have your answer
set answer=%original:*your=%
set answer
answer= answer
But I don't know how to substitute or remove sub-string which starts from specific character(or word) to the end of the original string.
For example, suppose I would like to remove sub-string which starts from "that" to the end of the original string. Then I use command as follows and expect result string to be "Questions "
set result=%original:that*=%
But, result string has no difference from original string. No effect occures. Substitution intention fails..
set result
result=Questions that may already have your answer
I used escape character '^', '\' for this case, but no effect..
How to fix this to substitute or remove substring like this type?
How can you substitute or remove substring which starts from specific character(or word) to the end of the original string? Thank you:-)
you can trick the command line parser to do that:
set "original=Questions that may already have your answer"
set result=%original: may =&REM %
set result
sadly, set "result=%original:may=&REM %" doesn't work, so the string should be free from poison characters.
How it works:
replace the word with &REM, which makes your string:
Questions that & REM already have your answer
and the command:
set result=Questions that & REM already have your answer
& is used as a delimiter for commands (try echo hello&echo world, which executes both echo commands). So what's really executed, is two commands:
set result=Questions that
and
REM already have your answer
It also doesn't work with delayed expansion. You can use a subfunction for it instead:
setlocal enabledelayedexpansion
if 1==1 (
set "original=Questions that may already have your answer"
call :substring "!original!"
set result
)
goto :eof
:substring
set org=%~1
set result=%org: may =&REM %
goto :eof
I have the following string in a batch file script:
ABCE#$1 TroubleMaker[FFFFF A=MyCountry US=1 CA=1 JP=1 EU=1
and it's stored in _var,when I do
set _var=%_var:* A=% - it cuts all the characters before " A" (including the 'A') and i'm left with =MyCountry US=1 CA=1 JP=1 EU=1
how can I change the set command to cut also the = mark from the string?
tried set _var=%_var:*==% - didn't work.
Thanks.
#ECHO OFF
SETLOCAL
SET "string=ABCE#$1 TroubleMaker[FFFFF A=MyCountry US=1 CA=1 JP=1 EU=1"
FOR /f "tokens=1*delims==" %%s IN ("%string%") DO SET "string=%%t"
ECHO "%string%"
GOTO :EOF
This assumes that you want to delete up to and including the initial =
The = disturbs the substring replacement syntax, because it contains a = on its own.
You could go for the following work-around:
set _var=%_var:* A=%
set _var=%_var:~1%
The second line constitutes the substring expansion syntax (type set /? for details), which splits off the first character, that is supposed to be a =.
This of course works only if the = immediately follows the A substring.
You can check whether the first character is = before removing it, like:
set _var=%_var:* A=%
if "%_var:~,1%"=="=" set _var=%_var:~1%
If you just want to search for the (first) = character and to ignore the A substring, you could establish a loop structure like this:
:LOOP
if "%_var%"=="" goto :END
if "%_var:~,1%"=="=" (
set _var=%_var:~1%
goto :END
) else (
set _var=%_var:~1%
goto :LOOP
)
:END
This cuts off the first character and checks whether it is a =. If it is, the remaining string is stored in _var and the loop is left; if not, the loop continues checking the next character. The first line is inserted to not hang in case the string does not contain a = character.
Say I have this vimscript as "/tmp/example.vim":
let g:input = "START; % END"
exec("! clear && echo " . shellescape(g:input))
If I open that file and run it with :so %, the output will be
START; /tmp/example.vim END
because the "%" is expanded to the buffer name. I want the output to be
START; % END
I can use the generic escape() method to escape percent signs in particular. This works:
let g:input = "START; % END"
exec("! clear && echo " . escape(shellescape(g:input), "%"))
But is that really the best way? I'm sure there're more characters I should escape. Is there a specific escape function for this purpose? Or a better way to shell out?
For use with the :! command, you need to pass the optional {special} argument to shellescape():
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.
:exec("! clear && echo " . shellescape(g:input, 1))
You need to properly escape the '%'. So it should be:
let g:input = "START; \\% END"
This seems to do it:
let g:input = "START; % END"
echo system("echo " . shellescape(g:input))
It should be noted I don't really care about the output; I'll use this with silent in a larger script.
Is there any way to retrieve last echoed message into a variable?
For example: if i call function, that does:
echo 'foo'
Can I somehow retrieve this 'foo' into a variable?
Thanks!
You can't retrieve last echoed message. But there are other options:
If you can place a :redir command before this function call and another one after, you can catch everything it echoes. But be aware that redirections do not nest, so if function uses :redir itself, you may get nothing:
redir => s:messages
echo "foo"
redir END
let s:lastmsg=get(split(s:messages, "\n"), -1, "")
If function uses :echomsg instead of :echo, then you can use :messages command and :redir:
echom "foo"
redir => s:messages
messages
redir END
let s:lastmsg=get(split(s:messages, "\n"), -1, "")