#!/bin/bash
readarray -t link < links
printf "%s\\n" "${link[@]}" | dmenu
sed -n 's/'"$(sed 's/ @.*//' links.txt | dmenu)"' @ //p' links.txt
Although if you can change the format of links you can open up many other options. For example, if you are using bash, layout the list of links as follows:
[blah1 blah1 blah1]=http://ww.somesite1.com
[blah2 blah2 blah2]=http://ww.somesite2.com
[blah3 blah3 blah3]=http://ww.somesite3.com
Then you can use this:
#!/bin/bash
declare -A links
eval links=($(cat ./links.txt))
echo ${links[$(printf '%s\n' "${!links[@]}" | dmenu)]}
Or if you were to use python, you might keep the links in a json format.
]]>i have a file mylinks.txt like this :
blah1 blah1 blah1 @ http://ww.somesite1.com
blah2 blah2 blah2 @ http://ww.somesite2.com
blah3 blah3 blah3 @ http://ww.somesite3.com
How to display in dmenu the text blah.... and when selected output or go to url ?
cat mylinks.txt | cut -d'@' -f1 | dmenu
I'am stuck i really don't how to link the text blah to his url ?
Sorry for noobiness !
]]>Two keybinds in your vimrc or exrc
map <c-t> :call DmenuVimFind("tabe")<cr>
map <c-f> :call DmenuVimEdit("tabe")<cr>
One plugin with the two functions(DmenuVimEdit & DmenuVimFind) , put it in ~/.vim/plugin/dedit.vim
" Vim Dmenu edit plugin
" Original Author: qinohe
" Maintainer: qinohe
" Last Change: 2020 Nov 02
" License: Public
" Dmenu edit
" Remove newline from string
function! Chomp(str)
return substitute(a:str, '\n$', '', '')
endfunction
" Edit file from vim history(viminfo) & rm temp file
function! DmenuVimEdit(cmd)
let dmena = system("./.config/dmenu/d-script/dmenu_vim_edit")
if exists("dmena")
let dmenb = Chomp(system("cat /tmp/vde"))
endif
if filereadable("/tmp/vde")
execute a:cmd . " " . dmenb
endif
let dmenc = system('rm -rf /tmp/vde')
endfunction
" Find file and open it in vim tab & rm temp file
function! DmenuVimFind(cmd)
let dmena = system("./.config/dmenu/d-script/dmenu_vim_find")
if exists("dmena")
let dmenb = Chomp(system("cat /tmp/vde"))
endif
if filereadable("/tmp/vde")
execute a:cmd . " " . dmenb
endif
let dmenc = system('rm -rf /tmp/vde')
endfunction
" End Dmenu Edit
And last two dmenu scripts, put them in ~/.config/dmenu/d-script or change the path in the plugin
first; dmenu_vim_edit
#!/bin/sh
# shellcheck disable=1090
[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
. "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmnews='dmenu -i'
# Find recently edited file in viminfo
vde=$( vde 2> /dev/null ) || vde=/tmp/vde
input="$( awk '/\47[0-9]/ {print $4}' ~/.viminfo | $dmnews -p "Edit recent file:" )"
[ -n "$input" ] && echo "$input" > "$vde"
exit 0
second; dmenu_vim_find
#!/bin/sh
# shellcheck disable=1090
[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
. "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmnews='dmenu -i'
vde=$( vde 2> /dev/null ) || vde=/tmp/vde
input="$( xsel -o | $dmnews -p "file search:" )"
[ "$input" != '' ] && result="$( locate -e -r "$input" | $dmnews -p "search result:" )"
[ -n "$result" ] && echo "$result" > "$vde"
exit 0
Btw. I would also use a dmenurc which holds colors for dmenu, uses vertical instead of horiz. lists etc.
I put this in ~/.config/dmenu/dmenurc, an example;
# standardize dmenu
# RAL 9004 0n RAL 9003 & choice RAL 9007 or reversed.
dmnews='dmenu -i -l 40 -fn DejaVuSansMono-11 -nb #2e3032 -nf #f4f8f4 -sb #2e3032 -sf #8f8f8c'
qinohe wrote:You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine
Exactly, a little bit overkilling and now I am frustrated with the script I made and wanted to make it work.
I know, but like I said, you use it in an already opened vim instance with a keybind, you could use it for more things, like search, find and open in a vim tab.
It may be a biy overkill, but ,I like using it this way;)
eval alacritty -e vim -o "$path2file"
This actually works. I read the manual you cite, but it does not mention the dangerous stuff. I have looked it up a bit and I see why is not a very good idea to use it, although in this case the operands concatenated should be fine.
I fail to see why it does not work without eval, maybe it means that the variable holding the path is not passed to vim correctly?
Yes, the variable doesn't get passed correctly. About 'eval' mostly referred to as 'evil' can do nasty things when the variable would be a command, it would simply execute it without you being able to prevent that. In this case however you're variable is a path which don't pose a threat. It would be good to avoid 'eval' as much as possible, better read this wooledge wiki https://mywiki.wooledge.org/BashFAQ/048 it explains it much better than me.
edit: Btw. you shouldn't use the command as is, because if you don't open a file and simply escape dmenu, it would still try to open a 'no name' file, check if there is a file otherwise exit
[ -n "$path2file" ] && alacritty -e vim -o "$path2file" || exit
You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine
Exactly, a little bit overkilling and now I am frustrated with the script I made and wanted to make it work.
Ogashi, the problem is a trailing newline(s) at the end of path2file. You'll need to strip that or rethink your script.
I also thought of that, tried using printf to get rid of the new line, now I also tried
path2file=$(awk ' /\47[0-9]/ {print $4} ' ~/.viminfo | dmenu -p 'Edit recent file:' | tr -d '\n')
But I keep having the same behaviour, it is driving me nuts.
eval alacritty -e vim -o "$path2file"
This actually works. I read the manual you cite, but it does not mention the dangerous stuff. I have looked it up a bit and I see why is not a very good idea to use it, although in this case the operands concatenated should be fine.
I fail to see why it does not work without eval, maybe it means that the variable holding the path is not passed to vim correctly?
]]>This behaves exactly the same as what I tried. The script runs, dmenu captures my option well, if I write in my script
echo "alacritty -e vim -o "$path2file""
I can see in the terminal the correct output, e.g. selecting dwm config.h gives
alacritty -e vim -o ~/.config/dwm/config.h
If I paste that in my terminal and run it, it opens a terminal window with vim and that file open. But for some reason, when I try to use it from the script, it opens a blank file and says NEW DIRECTORY at the bottom. I also tried with different terminal emulator, same result.
What if you use 'eval'?
(see)man --pager='less -p ^SHELL\ \BUILTIN' bash
Scroll down to to section 'eval'
eval alacritty -e vim -o "$path2file"
edit: before I forget to mention it, be careful with eval, in this case it poses little to no risk but you'd better avoid using it...
It's just an easy solution, read about eval if you consider...
From an already opened vim look in the history by using a keybind - I used Ctrl-f
Choose one of the files using dmenu and open the file in a new tab.
Simplify it to your needs.
Put this keybind( or whichever you like) in vimrc
map <c-f> :call DmenuVimEdit("tabe")<cr>
Put this plugin in '.vim/plugin' - I called it dedit.vim
" Vim Dmenu edit plugin
" Original Author: qinohe
" Maintainer: qinohe
" Last Change: 2020 Nov 02
" License: Public
" Dmenu edit
" Remove newline from string
function! Chomp(str)
return substitute(a:str, '\n$', '', '')
endfunction
" Edit file from vim history(viminfo) & rm temp file
function! DmenuVimEdit(cmd)
let dmena = system("./.config/dmenu/d-script/dmenu_vim_edit")
if exists("dmena")
let dmenb = Chomp(system("cat /tmp/vde"))
endif
if filereadable("/tmp/vde")
execute a:cmd . " " . dmenb
endif
let dmenc = system('rm -rf /tmp/vde')
endfunction
" End Dmenu Edit
Put this dmenu script in '.config/dmenu/d-script/dmenu_vim_edit', or edit this path in the plugin too!!
#!/bin/sh
# shellcheck disable=1090
[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
. "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmnews='dmenu -i'
# Find recently edited file in viminfo
vde=$( vde 2> /dev/null ) || vde=/tmp/vde
input="$( awk ' /\47[0-9]/ {print $4} ' ~/.viminfo | $dmnews -p "Edit recent file:" )"
[ -n "$input" ] && echo "$input" > "$vde"
exit 0
edit: removed a variable that en not needed in this situation
]]>thanks for your reply. The braces and commas was an attempt to give the command and arguments as an array of strings, in a dwm config.h fashion.
alacritty -e vim -o "$path2file"
This behaves exactly the same as what I tried. The script runs, dmenu captures my option well, if I write in my script
echo "alacritty -e vim -o "$path2file""
I can see in the terminal the correct output, e.g. selecting dwm config.h gives
alacritty -e vim -o ~/.config/dwm/config.h
If I paste that in my terminal and run it, it opens a terminal window with vim and that file open. But for some reason, when I try to use it from the script, it opens a blank file and says NEW DIRECTORY at the bottom. I also tried with different terminal emulator, same result.
]]>alacritty -e vim -o "$path2file"
#!/bin/sh
# Get the file path to edit and output to dmenu for the user to select
path2file=$(awk ' /\47[0-9]/ {print $4} ' ~/.viminfo | dmenu -p 'Edit recent file:')
# Launch a terminal
alacritty -e {"vim","-o","$path2file"}
But it opens a new directory with that name. What am I missing?
Thanks
]]>