How do I make elinks dump the web-page at a URL under cursor in a new vim buffer/tab? - scripting

I am trying to make elinks dump the web-page at the URL which starts at the current buffer position in vim (and ends at EOL), by mapping this keyboard shortcut and putting it in my .vimrc:
nmap owp :norm yE \| new \| .!elinks -dump #"<CR>
This yanks the rest of the line into the " register. Then it's supposed to open a new buffer and invoke elinks which should dump the rendered web-page into this new buffer. When I run the command the URL gets yanked, and that's it. New buffer does not open and elinks does not get invoked.
What am I doing wrong here?
Is there a smarter way to yank URLs
under the cursor? This method won't
work for URLs which occur in the middle of the line.

Never mind. Dug around in the vim manual and found some workarounds. One problem was with this:
.!elinks -dump #"<CR>
this won't work as expected. Everything after ! operator is passed to the shell verbatim by vim - so the register value will not be passed to elinks. Another problem is that the command separator somehow is not working after :norm yE. The final solution to get around these problems was this:
function! Browser ()
normal yE
new
execute ".!elinks -dump " . #"
set nomodified
endfunction
nmap owp :call Browser ()<CR>
Notice the use of execute to get around the limitation of "!" operator. I still need to figure out a solution for question 2 though.

Related

Paramiko, channel.recv(9999) causing confusion [duplicate]

I am using Python's Paramiko library to SSH a remote machine and fetch some output from command-line. I see a lot of junk printing along with the actual output. How to get rid of this?
chan1.send("ls\n")
output = chan1.recv(1024).decode("utf-8")
print(output)
[u'Last login: Wed Oct 21 18:08:53 2015 from 172.16.200.77\r', u'\x1b[2J\x1b[1;1H[local]cli#BENU>enable', u'[local]cli#BENU#Configure',
I want to eliminate, [2J\x1b[1;1H and u from the output. They are junk.
It's not a junk. These are ANSI escape codes that are normally interpreted by a terminal client to pretty print the output.
If the server is correctly configured, you get these only, when you use an interactive terminal, in other words, if you requested a pseudo terminal for the session (what you should not, if you are automating the session).
The Paramiko automatically requests the pseudo terminal, if you used the SSHClient.invoke_shell, as that is supposed to be used for implementing an interactive terminal. See also How do I start a shell without terminal emulation in Python Paramiko?
If you automate an execution of remote commands, you better use the SSHClient.exec_command, which does not allocate the pseudo terminal by default (unless you override by the get_pty=True argument).
stdin, stdout, stderr = client.exec_command('ls')
See also What is the difference between exec_command and send with invoke_shell() on Paramiko?
Or as a workaround, see How can I remove the ANSI escape sequences from a string in python.
Though that's rather a hack and might not be sufficient. You might have other problems with the interactive terminal, not only the escape sequences.
You particularly are probably not interested in the "Last login" message and command-prompt (cli#BENU>) either. You do not get these with the exec_command.
If you need to use the "shell" channel due to some specific requirements or limitations of the server, note that it is technically possible to use the "shell" channel without the pseudo terminal. But Paramiko SSHClient.invoke_shell does not allow that. Instead, you can create the "shell" channel manually. See Can I call Channel.invoke_shell() without calling Channel.get_pty() beforehand, when NOT using Channel.exec_command().
And finally the u is not a part of the actual string value (note that it's outside the quotes). It's an indication that the string value is in the Unicode encoding. You want that!
This is actually not junk. The u before the string indicates that this is a unicode string. The \x1b[2J\x1b[1;1H is an escape sequence. I don't know exactly what it is supposed to do, but it appears to clear the screen when I print it out.
To see what I mean, try this code:
for string in output:
print string

How can I run a bat file with spaces in the filepath?

I want to run a bat file used to compile sass to css from within a Kotlin program, on a Windows machine. I had everything working using Runtime.exec until I switched to a Windows account that had a space in the username. From what I read, I read that using ProcessBuilder would make this easier. It seems that even with ProcessBuilder I still can't get it to work, no matter what I try.
Here is my code so far
val commands = mutableListOf(
"cmd",
"/c",
"C:\\Users\\John Doe\\VCS\\test\\tools\\sass\\windows\\dart-sass\\sass.bat",
"--no-source-map",
"C:\\Users\\John Doe\\VCS\\test\\src\\main\\sass\\global.scss",
"global.css"
)
val processBuilder = ProcessBuilder(commands)
val process = processBuilder.start()
...
The error I get is 'C:\Users\John' is not recognized as an internal or external command, operable program or batch file. It doesn't help if I surround the strings that have spaces with \".
If I remember correctly, all windows files and folders that have a space in the name have a matching short name in the old 8.3 format replacing additional space and other characters with a tilde (~) and a number.
So whatever is returning you the path for the .bat and .sscs files could return the full filename in that format?
Doesn't solve the problem but avoids it instead, I admit.
Also means you won't get busted when someone puts a space in the filename (OK, unlikely, but still something better to deal with from the start).
Consider something along the lines of the top 2 answers on this superuser thread
This is actually a Windows cmd issue. The question here shows that in cmd, in addition to quoting the file paths, you also have to quote the entire part of the command line text after the /c switch.
I don't know if this is the best way to do it in ProcessBuilder, but I was able to get it to work with the following code.
"cmd.exe",
"/c",
"\"\"C:/Users/John Doe/VCS/test/tools/sass/windows/dart-sass/sass.bat\" "
+ "--no-source-map "
+ "\"C:/Users/John Doe/VCS/test/src/main/sass/global.scss\" "
+ "\"global.css\"\""

Zsh trouble when using echo with color/formatting characters

I'm just switch to zsh and now adapting the alias in which was printing some text (in color) along with a command.
I have been trying to use the $fg array var, but there is a side effect, all the command is printed before being executed.
The same occur if i'm just testing a echo with a color code in the terminal:
echo $fg_bold[blue] "test"
]2;echo "test" test #the test is in the right color
Why the command print itself before to do what it's supposed to do ? (I precise this doesn't happen when just printing whithout any wariable command)
Have I to set a specific option to zsh, use echo with a special parameter to get ride of that?
Execute the command first (keep its output somewhere), and then issue echo. The easiest way I can think of doing that would be:
echo $fg[red] `ls`
Edit: Ok, so your trouble is some trash before the actual output of echo. You have some funny configuration that is causing you trouble.
What to do (other than inspecting your configuration):
start a shell with zsh -f (it will skip any configuration), and then re-try the echo command: autoload colors; colors; echo $fg_bold[red] foo (this should show you that the problem is in your configuration).
Most likely your configuration defines a precmd function that gets executed before every command (which is failing in some way). Try which precmd. If that is not defined, try echo $precmd_functions (precmd_functions is an array of functions that get executed before every command). Knowing which is the code being executed would help you search for it in your configuration (which I assume you just took from someone else).
If I had to guess, I'd say you are using oh-my-zsh without knowing exactly what you turned on (which is an endless source of troubles like this).
I don't replicate your issue, which I think indicates that it's either an option (that I've set), or it's a zsh version issue:
$ echo $fg_bold[red] test
test
Because I can't replicate it, I'm sure there's an option to stop it happening for you. I do not know what that option is (I'm using heavily modified oh-my-zsh, and still haven't finished learning what all the zsh options do or are).
My suggestions:
You could try using print:
$ print $fg_bold[red] test
test
The print builtin has many more options than echo (see man zshbuiltins).
You should also:
Check what version zsh you're using.
Check what options (setopt) are enabled.
Check your ~/.zshrc (and other loaded files) to see what, if any, options and functions are being run.
This question may suggest checking what TERM you're using, but reading your question it sounds like you're only seeing this behaviour (echoing of the command after entry) when you're using aliases...?

Powershell scripting - in middle of writing, how to undo?

I'm new to powershell and as such finding my fingers are hitting wrong keys which produces the >> symbol. How to I 'undo' that to continue with the initial script?
E.g:
$i=0; get-childitem $dirname | foreach-object {...rest of script and I miss closing a bracket or something then get... >> and can't remove that and continue writing script.
I'm sure it's an easy key combination that I haven't found yet, but better than closing app and re-opening.
That symbol is a line continuation. Just keep typing as though you didn't hit Enter and it will treat everything as the full line you intended.
If you just want to "undo" and restart writing your script you can press:
Ctrl + C

How do I navigate the MiniBufExplorer without going into the MiniBufExplorer window?

How do I navigate the MiniBufExplorer without going into the MiniBufExplorer window?
In lines 170-174 of minibufexpl.vim:
" To enable the optional mapping of <C-TAB> and <C-S-TAB> to a
" function that will bring up the next or previous buffer in the
" current window, you can put the following into your .vimrc:
"
" let g:miniBufExplMapCTabSwitchBufs = 1
I added that to my .vimrc, restarted a blank Vim, then did the following commands to bring up multiple buffers (and consequently, the MiniBufExplorer):
:e somefile
:e someotherfile
Now, with my cursor inside the file window (not the MiniBufExplorer window), I try CtrlTab and CtrlShiftTab to no effect. Something I missed?
I think this is most likely a conflict with snipMate, which I believe you have since you mention the tab mapping works in select mode. I tried to do it manually map <C-Tab> :bn<cr>,
and it did not work either. I now use map <C-P> :bp<cr> and map <C-N> :bn<cr> to navigate through buffers, and I suppose other mappings would work just as well as long as there is no conflict.
Minibuf explorer shortcuts (may require additional configuration):
[C-TAB] and [C-S-TAB] - move to next and prev buffers
Vim regular buffer shortcuts:
:bn and :bp - move to next and prev buffers
:b# - move to buffer of number #
:h :buffers - for additional help
Did you leave the " at the beginning of the let line? If so, it's commented out, and will not work. Otherwise, you may have a mapping conflict between plugins. The :map command will list current mappings.
It's easier to deal with mappings if you install headlights though.