You are not logged in.
That's why we get into pointless back and forths on a forum about the best way to write a silly script which most people wouldn't care about as long as it gets the job done. Practice.
Quite right you are, I should, practice;)
About the script, it works as intended, a numbered list of programs in dmenu to choose from.
@kristoferus, try the last the last lines from #395 should work!
Offline
Ok, so again, what's the output of wmctrl -lx, the awk script will need to be adjusted for the different output.
I'd guess this might do:
list=$(wmctrl -lx)
num=$(echo "$list" | awk '{ $1=$2=""; gsub(/^ */,""); printf "%3d - %s\n", NR, $0; }' | dmenu -l 20 | cut -d '-' -f 1)
[ -z "$num" ] && exit
echo "$list" | awk -v N="$num" 'NR == N { print $1; }' | xargs wmctrl -i -a
But this is a wild guess. I don't use wmctrl. I can polish the text processing, but if you don't provide the actual input I'm just guessing what it might look like.
Last edited by Trilby (2020-05-14 16:53:40)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
wmctrl -l
0x0160000a 0 HOME @HOME:~
0x00a00001 1 HOME Dmenu Hacking Thread (Page 17) / Programming & Scripting / Arch Linux Forums - Chromium
0x01c00003 3 HOME
wmctrl -lx
0x0160000a 0 urxvt.URxvt HOME@HOME:~
0x00a00001 1 chromium.Chromium HOME Dmenu Hacking Thread (Page 17) / Programming & Scripting / Arch Linux Forums - Chromium
0x01c00003 3 pcmanfm.Pcmanfm HOME
Last edited by kristoferus (2020-05-14 16:56:04)
Offline
See my edit, I believe my guess was right.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
no it shows now:
2 - HOME @HOME:~
Last edited by kristoferus (2020-05-14 16:55:39)
Offline
My script with the wmctrl -lx output you provided puts this in dmenu:
1 - urxvt.URxvt HOME@HOME:~
2 - chromium.Chromium HOME Dmenu Hacking Thread (Page 17) / Programming & Scripting / Arch Linux Forums - Chromium
3 - pcmanfm.Pcmanfm HOME
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Sorry Yes you are right -> mybe a copy and paste mistake !
THANKS cool :-)
Offline
@Trilby
Is it also possible to adapt the script that see it in dmenu like this ?
1 - URxvt HOME@HOME:~
2 - Chromium HOME Dmenu Hacking Thread (Page 17) / Programming & Scripting / Arch Linux Forums - Chromium
3 - Pcmanfm HOME
Offline
Just replace the gsub in the first awk with this:
gsub(/^[^\.]*\./,"");
This replaces everything up to the first "." rather than just the spaces at the start of the line.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
@Trilby
Thanks for the help -> perfect !
Last edited by kristoferus (2020-05-16 13:50:58)
Offline
@Trilby
Sorry again
The hostname is also not nessacery in the usage (HOME)
2 - chromium.Chromium HOME Dmenu Hacking Thread (Page 17) / Programming & Scripting / Arch Linux Forums - Chromium
Is this also possible ?
Thanks !
Offline
I've been assuming there's a typo in the example output of wmctrl I've been working with as there are different numbers of columns. If the Urxvt line actually has "HOME @HOME:~" or perhaps "HOME HOME@HOME:~" then this is very easy, just blank one more column in the start of the awk thread. Please take some time to read up on awk to know what it is doing with that script and you'll be able to adjust this all you'd like.
While I've not been through his awk tutorial, Bruce Barnett's sed tutorial is great, so I suspect the linked awk tutorial should be as well. It is, however, written for those with a bit of *nix background. If you don't have that yet, I'd recommend working through Ryan's tutorials as they are an excellent foundation for those with little to no backround in unix/linux.
Last edited by Trilby (2020-05-16 16:48:57)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
@Trilby
Thanks i have solve it :-)
awk '{ $1=$2=$4=""....
Last edited by kristoferus (2020-05-16 18:47:28)
Offline
Nicely done.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Hi !
I use this script to search sezific files and open with xdg-open -> it works -> but files with an ' in the filename doesnt work:
fd -a -e "pdf" -e "jpg" -e "txt" -e "mp3" -e "mkv" -e "mp4" . ~/ | dmenu -l 20 -i | (nohup xargs -I{} xdg-open "{}" >/dev/null 2>&1 &)
Thanks !
Offline
Why the xargs in a subshell at the end. Dmenu can only return one result, right? So just get rid of that bit at the end and just open the result with xdg-open.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
like this it doesnt work :
fd -a -e "pdf" -e "jpg" -e "txt" -e "mp3" -e "mkv" -e "mp4" . ~/ | dmenu -l 20 -i | xargs xdg-open
Offline
If you want to use xargs:
... | xargs -I{} xdg-open "{}"
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Thanks !
with this command it works:
fd -a -e "pdf" -e "jpg" -e "txt" -e "mp3" -e "mkv" -e "mp4" . ~/ | dmenu -l 20 -i | xargs -0 -I{} xdg-open "{}"
Offline
Hi folks,
I am trying to write a simple script to search and edit recents files in vim. Right now, I have:
#!/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
Offline
What is that command given to alacritty? Why the braces, quotes, and commas?? Shouldn't that just be:
alacritty -e vim -o "$path2file"
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Hi Trilby,
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.
Offline
You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine
I created this from an existing plugin I created a while ago to search a file using dmenu & open it in a tab.
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
Last edited by qinohe (2020-11-02 22:13:21)
Offline
Ogashi, the problem is a trailing newline(s) at the end of path2file. You'll need to strip that or rethink your script. As for the braces, shell and C are completely different languages, don't try to overlap them.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
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...
Last edited by qinohe (2020-11-03 01:32:56)
Offline