I have this code intended to:
I’m struggling to define and use these variables inside the
execute function calls…
function TaskSearch() let line = getline('.') let task_id = split(line, "#") let task_note = execute('find ~/.task/notes/ -name "task_id*"') execute('edit ~/.task/notes/$task_note') endfunction command! TaskSearch :call TaskSearch() nnoremap <leader>tn :TaskSearch <Enter>
I’ve tried many different variatlions of the
let task_note... and
execute(...) statements without any success.
I need to take the results of that command, which should be just a single file, and then open that file in a new buffer to edit it. How can I achieve this?
Here's one way that works...
function! TaskSearch() ..snip... let task_note = system('find ~/.task/notes -name "' . task_id . '"*') exe 'e ' . task_note endfunction
Some of the key changes...
*) in the
system()which is a vim function that returns result of executing external processes.
findshould return a full path so there shouldn't be a need to repeat
Might need a little fine tuning to get it to work for your exact problem (e.g. this isn't controlling for a scenario where
find returns more than one file...make sure your query is specific/explicit enough to prevent that or use additional shell commands to narrow it down, if needed). Let me know if you have any problems or questions about how it works.
An important point is that when you want to include the value of a variable in a command you usually need to use something like
exe, i.e. something that will treat its arguments as an expression to be evaluated.
Correct answer by B Layer on December 30, 2020
4 Asked on November 10, 2021
2 Asked on August 31, 2021 by oalders
1 Asked on August 31, 2021 by user96649
1 Asked on August 31, 2021 by rohan
1 Asked on August 31, 2021 by lus-de-sousa
2 Asked on August 31, 2021
2 Asked on August 31, 2021 by elliptical-view
1 Asked on August 31, 2021 by tallboy
2 Asked on August 31, 2021 by bapi
3 Asked on August 31, 2021
1 Asked on August 31, 2021
Get help from others!