0

I am running the latest version of nvim on the latest version of arch.

I have this bash one liner which works as intended

echo; lpstat -p | sed 's/printer //g' | sed 's/is idle.  enabled since.*//g'; echo"

I wish to assign the above command to a keyboard shortcut within nvim and run it within nvim so that it drops the result within the currently open nvim text file

Here is what I have in .vimrc

nnoremap <leader><leader>p :!echo; lpstat -p | sed 's/printer //g' | sed 's/is idle.  enabled since.*//g'; echo

but it does not work

How do I wrap the bash command up so it is treated as a one liner then get it to drop the result into the open text file at the cursor?

7
  • 1
    Well, first look at :h :r!.
    – B Layer
    Dec 5, 2021 at 12:06
  • 2
    "Does not work" doesn't help me much. Why don't you start with just part of your shell command and build it up a bit at a time until you find what's causing problems. Because the basic form of :nnoremap XX :r!shell-cmd is valid.
    – B Layer
    Dec 5, 2021 at 12:15
  • 1
    thanks. Brackets gone. Error is E492: Not an editor command: sed 's/printer //g' | sed 's/is idle. enabled since.*//g'; echo. Think it is looking at | and wants <bar> so may be is seeing it as a vim vcommand and not bash.
    – Kes
    Dec 5, 2021 at 12:18
  • 2
    Try escaping the pipes... \|.
    – B Layer
    Dec 5, 2021 at 12:19
  • 1
    Thanks escaping the | with \| did it.
    – Kes
    Dec 5, 2021 at 12:21

1 Answer 1

3

Here is the solution. What I needed to do was

  1. insert an r to read the result of the bash one liner into the current file, and
  2. escape the two occurances of | with \|
" below gets current list of printers  
nnoremap <leader><leader>p :r !echo; lpstat -p \| sed 's/printer //g' \| sed 's/is idle.  enabled since.*//g'; echo<cr>
2
  • 1
    Another approach could be to use your shell to define an alias for that whole lpstat command, and then your nnoremap command wouldn't need to escape as many characters.
    – alxndr
    Dec 7, 2021 at 23:39
  • Thanks. I thought it might be some escaping or white space problem so initially set-up an alias printer_list="command here" within .bash_aliases but for some reason it did not work. Probarly something to do with my config. I beleive it should have worked. But I got it working within .vimrc anyways. Thanks.
    – Kes
    Dec 9, 2021 at 0:28

Your Answer

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

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