You are not logged in.

#401 2020-05-14 16:42:23

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Dmenu Hacking Thread

Trilby wrote:

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.  wink

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

#402 2020-05-14 16:44:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

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

Offline

#403 2020-05-14 16:48:20

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

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

#404 2020-05-14 16:50:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

See my edit, I believe my guess was right.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#405 2020-05-14 16:53:48

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

no it shows now:

2 - HOME  @HOME:~

Last edited by kristoferus (2020-05-14 16:55:39)

Offline

#406 2020-05-14 16:57:35

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

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

Offline

#407 2020-05-14 17:00:23

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

Sorry Yes you are right -> mybe a copy and paste mistake !

THANKS cool :-)

Offline

#408 2020-05-16 07:47:12

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

@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

#409 2020-05-16 12:15:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

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

Offline

#410 2020-05-16 13:50:43

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

@Trilby

Thanks for the help -> perfect !

Last edited by kristoferus (2020-05-16 13:50:58)

Offline

#411 2020-05-16 16:08:36

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

@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

#412 2020-05-16 16:45:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

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

Offline

#413 2020-05-16 18:40:47

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

@Trilby

Thanks i have solve it :-)

awk '{ $1=$2=$4=""....

Last edited by kristoferus (2020-05-16 18:47:28)

Offline

#414 2020-05-16 19:09:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

Nicely done.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#415 2020-06-22 18:52:20

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

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

#416 2020-06-22 19:02:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

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

Offline

#417 2020-06-22 20:27:56

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

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

#418 2020-06-22 20:54:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

If you want to use xargs:

... | xargs -I{} xdg-open "{}"

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#419 2020-06-23 07:43:41

kristoferus
Member
Registered: 2015-09-08
Posts: 27

Re: Dmenu Hacking Thread

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

#420 2020-11-02 18:23:23

Ogashi
Member
Registered: 2017-03-05
Posts: 5

Re: Dmenu Hacking Thread

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

#421 2020-11-02 19:41:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

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

Offline

#422 2020-11-02 20:09:51

Ogashi
Member
Registered: 2017-03-05
Posts: 5

Re: Dmenu Hacking Thread

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.

Trilby wrote:
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

#423 2020-11-02 21:34:13

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Dmenu Hacking Thread

You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine tongue
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

#424 2020-11-02 22:17:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: Dmenu Hacking Thread

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

Offline

#425 2020-11-03 00:41:45

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Dmenu Hacking Thread

Ogashi wrote:

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

Board footer

Powered by FluxBB