1

I have a lot of plaintext documentation of many different programming languages I like to browse with vim. My documents look like

w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/events.html
w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/http.html
w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/net.html

So to just yank paste them in the terminal to get the information. But I end up pipping it to a text document that I end up opening with vim

So to simplify this I learnt either to

:%!w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/path.html

For what I have a shortcut with <f6> in my ~/.vimrc

nmap <F6> :exec '!'.getline('.')

It just takes <f6> and <Enter> to spawn a temporary shell with that website

But I wanted it to be opened in a new tab, for that I learnt that the command to input in vim is

:tab term w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/path.html

But unfortunately using this <f6> and <Enter> on this line will still be spawning a new shell.

Do you know how to change that <f6> keybinding to adapt it to this :tab term functionality?

1
  • This is irrelevant to your question, but consider using readability-cli for a much better output (readable 'https://nodejs.org/dist/latest-v16.x/docs/api/path.html' | w3m -dump -T text/html)
    – r_31415
    Commented Jun 2, 2022 at 23:27

2 Answers 2

2

So let's brake down what you want to do:

  1. open a new tab
  2. get the output from a command
  3. put that output into the newly opened tab

So in Vim commands, this would be:

  1. :tabnew
  2. :call system('command')
  3. :call append(previous output)

So now, let put this all together into a "single" command, that can be mapped. To concatenate several commands at once, we can use the | command:

:tabnew |:call systemlist('w3m -dump http...')->append(0)

We can use the method syntax to chain several Vim Script functions to work on the resulting output. It's a bit like piping in the shell from one command to the next.

Now we need to translate this into a mappable sequence. That means to explicitly add <cr> in front of the | to make it clear Vim has to hit Enter. Also we need to translate the | into <Bar> or otherwise this would terminate the mapping and execute the next command right when defining the mapping instead of when the mapping is executed.

So this would turn the sequence into:

nnoremap <f6> :nnoremap <f6> :tabnew <CR><bar>:call systemlist('w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/path.html')->append(0)<cr>

There are perhaps a few minor improvements we can add:

  1. mark the buffer as a nofile buffer, so that vim allows to easily throw away this without complaining about changes
  2. give the buffer a meaning fule name
  3. move the cursor to the top of the buffer

The first can be achieved by setting the 'buftype' option, e.g. :set buftype=nofile. The second by using tabnew <filename> And the third, by executing the normal mode command gg at the end. This would make our mapping:

:nnoremap <f6> :tabnew +set\ buftype=nofile scratch.txt<CR><bar>:call systemlist('w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/path.html')->append(0)<cr><bar>gg
4
  • It is not working, I am starting step by step. First of all if I execute this manually on vim's console :tabnew |:call systemlist('w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/path.html')->append(0) this will error to E488: Trailing characters Commented May 26, 2022 at 15:46
  • Also the final goal is to be able to in a text document with lines made up of links such as https://nodejs.org/dist/latest-v16.x/docs/api/net.html be able to place the cursor on top of them an press a keybinding which will execute that certain command (of opening a new tab with the STDOUT of w3m -dump <VIM-LINE> ) This command does execute the command that is on the line nmap <F6> :exec '!'.getline('.') source Commented May 26, 2022 at 15:53
  • 1
    I am not sure where the E488 comes from. Does the calling the systemlist command throw that error? What Vim version and what is your shell? Commented May 27, 2022 at 16:37
  • 1
    For older versions of vim which do not support the method syntax (->), this will cause that E488 error. In that case the mapping can be rewritten like this: :nnoremap <f6> :tabnew +set\ buftype=nofile scratch.txt<CR><bar>:call append(0, systemlist('w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/path.html'))<cr><bar>gg
    – nickspoon
    Commented May 31, 2022 at 23:10
1

Not sure if I get the problem right, but with the following mapping I get result in the new tabpage by pressing <F6>:

nnoremap <F6> :tab term w3m -dump https://nodejs.org/dist/latest-v16.x/docs/api/path.html<CR>

without spawning a new shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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