pythonvimscriptingneovim

Why does neovim not see my python script in Command-Line mode?


For my goals I wrote two simple scripts - 1 bash script and 1 python script. Below I attached them.

Bash:

#!/bin/bash

cat $1 | xclip -selection clipboard

Python:

#!/usr/bin/python3

import sys
import os

PATH = '/home/gipakrot/sql/sql_ex/solutions'

files = os.listdir(PATH)

current_file = sys.argv[1]

if current_file in files:
    version = 1
    task = current_file[2: current_file.find('.sql')]
    for file in files:
        if file.startswith(f'ex{task}_'):
            version += 1
    final_name = f'ex{task}_{version}.sql'

else:
    final_name = current_file

with open(current_file, 'r') as f:
    lines = f.readlines()

with open(f'{PATH}/{final_name}', 'a') as f:
    for line in lines:
        f.write(line)

Also i made aliases for both of them:

alias movesql='mvsql.py'
alias copysql='copysql.sh'

I want to use them in neovim Command-Line mode.

When I am trying to use these scripts in neovim Command-Line mode:

:!copysql or :!movesql

I get these error:

:!copysql [Command Line]                                                                                                      
/bin/bash: line 1: copysql: command not found

shell returned 127

Can you help me to fix this problem? Don't see what I do wrong. In terminal both scripts (via aliases) works as expected.


Solution

  • This is because the shell provided via the :! interface in neovim is not an interactive shell. We can verify this via :!echo $-. The resulting string of characters does not include i. As a result, aliases (and functions) will not be loaded in. A good alternative for you would to be to symlink these two scripts into ~/.local/bin, which is a common spot for user scripts. Depending on what distribution you're on, this directory may or may not be in your PATH environment variable, so check to make sure, and add it in your .bashrc or other init script if not. Note that the exact name of the symlink is what you will call to execute the script, so consider ln -s mvsql.py ~/.local/bin/mvsql (or something even shorter if you're going to be using it a lot) so you don't have to include the .py when calling it.