1

So I am a beginner competitive coder who just started using NeoVim after using gVim. So when I copy sample test cases from lets say : Codeforces or Hackerearth. For instance if the sample text is :

5

RRGRR

Then when I use Ctrl + C to copy from the website and then I paste it in the inbuilt terminal the code is now formatted in a different way as: 5RRGRR

So now the spaces and new lines are removed hence the answers I get are wrong. Please help me correct this. This is a screenshot to how the pasted text looks

This is my first question I have asked so please forgive me for any mistakes I have committed I have previously tried to use the

:set paste

in NeoVim but even that does not seem to solve my problem.
(I'am a windows 10 user)

pradeexsu
  • 1,029
  • 1
  • 10
  • 27

2 Answers2

0

I had faced a similar problem post shifting from Gvim to Neovim-qt and upon searching I found this post, :set paste does not seem to work for this particular issue.

Specifically the problem occurs when an .exe file is executed in Neovim terminal(not sure if it happens for other possible things too) it seems to concatenate multiple lines into a single line as you pointed out thereby merging last char of previous line and 1st character of next line into a single word causing erroneous input.

Upon doing some more research I found nothing but an alternative could be to write copied text into a input file and then pass it to .exe file.

Note :- The below codes provided are in lua.

First create a code block in lua which creates a file according to user input name or by default assumes the current file name being user by user in neovim to be the input file name with .in extension.

function CreateInputFile(filename)
  filename = filename or vim.fn.expand("%:r")  -- use current file name if no filename provided
  local dir = vim.fn.expand("%:p:h")
  local input_file = dir .. "/" .. filename .. ".in"

  -- Prompt the user to enter input
  local input = vim.fn.input("Enter input:\n")

  -- Write input to the input file
  local file = io.open(input_file, "w")
  file:write(input)
  file:close()

  -- Show a message indicating the input file has been created
  vim.api.nvim_out_write("Created input file: " .. input_file .. "\n")
end

assume a file you are working on as cpcode.cpp and the above code is called via neovim command line as

:lua CreateInputFile()

Now you may enter anything and then press enter twice, you will then notice that a file is created in same directory with the name cpcode.in with data you just typed.

Now assume i want to compile a C++ code by remapping F5 to first enter copied text from clipboard as input and then display corresponding output in Neovim terminal.

Note : The below code was made keeping command prompt into consideration , things will change a little for powershell if set to default terminal for neovim and other terminals too.

-- C++ code runner using std=c++20 with input file containing latest copied data in register !!!
vim.api.nvim_create_autocmd("Filetype", {
    pattern = {"cpp","CPP","cxx","CXX","hpp","hxx","Hxx","HXX"},
    command = "nnoremap <F5> :lua CreateInputFile() <CR> <C-r>+ <CR> <CR> :w <bar> :split<CR> :term g++ % -std=c++20 -pipe -Wall -Wextra -Wshadow -Og -o %:r && %:r < %:r.in <CR> i"
})

Note :- Please remember to copy the thing you want to pass to the input in .exe file just before pressing F5 so that its the latest one in the system clipboard since Ctrl-r + in the above code loads data from system clipboard, read more here.

The output will be displayed in split screen in neovim terminal as I have remapped it to do so which will be more convenient to compare code with output in Competitive programming, but it's up to personal preference and it can modified or removed as one chooses to.

Simply the above code does something similar to after having performed creating input file cpcode.in as I mentioned earlier.

g++ cpcode.cpp -std=c++20 -pipe -Wall -Wextra -Wshadow -Og -o cpcode && cpcode < cpcode.in

Hope this code helps you with the problem you faced. I have made a complete Neovim config in LUA, do check it out if it helps you with your problem any better.

-1

Try using ctrl-v instead of p... that works for me