Vi and Vim Asked by alec on December 30, 2020
I have this code intended to:
#ad04e482
ad04e482
<leader>tn
I’m struggling to define and use these variables inside the execute
function calls…
function TaskSearch()
let line = getline('.')
let task_id = split(line, "#")[1]
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’ve looked through various posts on the stack network but I haven’t seen any examples using a vimscript variable (like task_id
in my case) inside a shell command’s arguments.
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
Call with :call TaskSearch()
.
Some of the key changes...
*
) in the find
call.system()
which is a vim function that returns result of executing external processes.exe
and e[dit]
.find
should return a full path so there shouldn't be a need to repeat ~/.task/notes
in the exe
line.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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP