1

I am using:

  • Neovim 0.9.0 (latest version) and
  • Arch linux 6.2.10 (latest version)

I am using the Vim Plugged plugin manager

Recently :ha (print hardcopy) stopped working.

The error message I get is as follows:

:ha

E492: Not an editor command: ha
and:

:hardcopy  

E492: Not an editor command: hardcopy

I did not recently remove or add any plugin's that I am aware of.

Can anyone suggest why these might have stopped working? (Possibly a bug in a nvim update?)

5
  • 3
    Recently, Neovim removed hardcopy Commented Apr 18, 2023 at 11:30
  • thanks. Did they replace it with something else? Is there another way I can print the nvim scratch content without saving it first?
    – Kes
    Commented Apr 19, 2023 at 11:11
  • I really enjoyed reading the link @ChristianBrabandt posted. While every SW project on this planet is working hard to add features, neovim is removing them. This comment should be an answer. If you click through the discussion on github, you will eventually find this plugin which is supposed to offer a :Hardcopy command that takes a detour over HTML and your browser.
    – Friedrich
    Commented Apr 19, 2023 at 11:27
  • For a proper answer, one should also mention the 2html plugin, which is distributed with vim and neovim and can be used to create a syntax enabled html page of the current buffer. Commented Apr 19, 2023 at 12:50
  • @ChristianBrabandt you know a lot yet you are reluctant to post an answer. Why is that? What strikes me as odd is that everyone assumes you want a colored printout with line numbers etc. Printing a txt file, I'd consider using :!lpr % and be done with it.
    – Friedrich
    Commented Apr 19, 2023 at 13:14

3 Answers 3

2

So :ha(rdcopy) has been deleted from Neo Vim.

I can understand why, since it makes code refactoring for developers easiear without having to consider an infinite number of things.

Here is the workaround.

Use this function in your .vimrc file.

There is no need to save the working file to print.

Remember to change the printer name to your own printer name

The first letter of a command has to be upper case so we cannot have :ha or :hardcopy and have instead to have :Ha and :Hardcopy.

Place all of this inside your .vimrc

function! PrintBuffer()
  silent execute '!lpoptions -d HP_LaserJet_2200_LOCAL_hpcups'
  silent execute 'w !lp -n 1 -o media=a4 -o sides=two-sided-long-edge -'
endfunction


command! Ha call PrintBuffer()
command! Hardcopy call PrintBuffer()

Or use the below function to print to the default printer, again no need to save the file to print.

This time I have mapped this to :Hc

" below for instant printing to default printer
function! PrintBufferWithCR()
  silent execute 'w !lp'
endfunction

command! Hc call PrintBufferWithCR()

To find out which is your default printer use lpstat -d on the command line. `

3
  • This allows you to print without saving the buffer and uses the default printer. Fast and easy. Thanks @Friedrich for the hint. Modify for your own preferred shortcut. nnoremap <leader>p :w !lp<cr><cr>
    – Kes
    Commented Apr 19, 2023 at 13:26
  • Agreed, why does the function use silent/execute? Doesn’t need to, IMO.
    – D. Ben Knoble
    Commented Apr 19, 2023 at 18:32
  • thanks @d-ben-knoble, that was the best I could come up with. Change it if there is a better way. I found silent execute was the only way I could come up with without having to use a <cr> after issuing the command
    – Kes
    Commented Apr 21, 2023 at 6:33
2

Hardcopy has been removed from neovim, but it still works fine in vim.

So just keep using neovim for editing files and just use vim to generate the postscript file from terminal:

vim -c ":hardcopy > output.ps" -c ":quit" input.txt

1
  • Welcome to Vi and Vim! This information has already been covered in detail by existing answers. Consider editing your answer to include additional detail to distinguish it from other answers.
    – D. Ben Knoble
    Commented Jun 26, 2023 at 13:20
1

The problem for me with the above answers is that they're not well-formatted - large type, not mono-spaced. And any syntax coloring is gone.

I'm on MacOS, so I have MacVim that still has the :hardcopy command, but I wanted to automate as much of the process as I could and not have to manually type and twiddle just to get my printout.

So I set it up so that can run a command from the command-line and have it switch to MacVim, print, and quit. On the Mac, the print goes to Preview, so I can just print from there.

I did this with a bit of bash and MacVim features.

  1. I added the official built-in MacVim mvim command like so, along with the MacVim man pages:
export PATH="/Applications/MacVim.app/Contents/bin:$PATH"
export MANPATH="/Applications/MacVim.app/Contents/man:$MANPATH"
  1. In my ~.bashrc file I defined the following function:
unset -f mvprt
function mvprt() {
  mvim -c ':hardcopy' -c ':sleep 500m' -c ':quit' $1
}
export -f mvprt
  1. So now, I just type:
mvprt <filename>

to send my formatted output to Preview.

Next step will be to get this or something similar that I can run from inside NeoVim itself

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.