Monthly Archives: December 2024

Sway and Waybar

Up to now, I have shown how to get started with Sway with the initial configurations and some other configurations.

Now, I’ll show how to install another status bar in Sway: Waybar (which is more powerful than the default Sway bar). Again, I’m going to do that for Arch Linux. As in the previous posts, I will NOT focus on the look and feel configuration. (This post is similar to the post about Hyprland and Waybar, but it’s specific to Sway).

Before continuing, the Waybar module for keyboard status (Caps lock and Num lock) requires the user to be part of the “input” group. Ensure your user is part of that group by running “groups”. If not, then add it with

Then, you must log out and log in.

Let’s install the package waybar:

Let’s open a terminal and start Waybar (of course, it will appear on top of the current Sway bar at the moment).

The result is not that good-looking:

Waybar heavily relies on Nerd fonts for icons, and, currently, we don’t have any installed (unless you have already installed a few yourself).

The terminal will also be filled with warnings about a few missing things (related to Sway) and errors about failures to connect to MPD.

Let’s quit Waybar (close the terminal from where you launched it), and let’s fix the font problem by installing a few font packages:

Let’s start Waybar again, and this time it looks better:

Try to click on the modules and see what happens. For some, the information shown will change (e.g., the time will turn into the date).

If you have “pavucontrol” installed (which should be the case for an EndeavourOS installation), clicking on the volume section will open the “pavucontrol” dialog for adjusting audio settings:

The “Caps” and “Num” locks should react according to the status of the corresponding keyboard keys. The section “it” shows that my current keyboard layout is “Italian.” if you configure several layouts and switch among them, that part in the bar will update automatically. And, of course, the workspace indicator works like in the default Sway bar.

Let’s quit Waybar again and change the Sway configuration to start Waybar instead of the default Sway bar. This section:

must be changed as follows:

Now, start Sway and enjoy Waybar.

Note that Waybar, by default, shows the Sway “mode”. For example, by default, SUPER+R enters the resize mode, where you can resize the current tiled window with arrow keys. The mode is indicated in the Waybar near the workspaces:

To customize the Waybar, create the configuration files for the Waybar (by default, they are searched for in “~/.config/waybar”). We can do that by using the default ones:

The above command will copy “config.jsonc” (with the configuration of Waybar modules, i.e., the “boxes” shown in the bar; The configuration uses the JSON file format) and “style.css” (for the style).

At the time of writing, this is the initial part of the configuration file:

The initial parts specify the position and other main configurations. You can edit as you see fit by removing a module, moving it to another position, or enabling new ones. You might want to have a look at the Waybar Wiki for an explanation of the modules.

NOTE: I’m testing this configuration in a virtual machine, so modules like “backlight” and “battery” are not shown because there’s nothing corresponding to them in the virtual machine.

Remember that for each module you mention here, you can have a configuration in the rest of the file, e.g.:

Otherwise, you get the defaults for that module.

Let’s add a configuration for showing a Taskbar to show all the running applications from all workspaces. This can be useful for quickly looking at all the running applications and quickly switching\ to any of them, especially in many workspaces. I’ll add it to the left section and remove the “custom/media” block, which I don’t use:

The module must also be configured to get the functionality that clicking on an element activates that element, possibly switching to the containing workspace (in this example, middle-click will close the window):

Save the file, and reload Sway to see the block in action (I opened four different applications in four different workspaces):

The right-most section in the bar is a Tray, showing icons of running applications like Dropbox, Skype, or the Network Applet. Let’s install the latter one:

Let’s configure Sway to autostart the “nm-applet”:

Let’s restart Sway, and now the Tray section is populated by the Network Manager Applet, which you can click and interact with, e.g., for editing connections or connecting to a WiFi network:

Let’s create a custom module for the Power menu, taking inspiration from the EndeavourOS Sway GitHub repository (I’ll change things a bit and simplify them; in particular, I’m going to use “rofi”, i.e., the package “rofi-wayland”, as shown in my previous posts, instead of “wofi”).

First, add the “custom/power” after the “tray” module:

Then, we define the module:

Note that I’ve used a character using the installed Nerd font. Of course, you can choose anything you like.

Then, we add the script in the corresponding directory:

Remember, the script must be made executable.

Reload Sway. Here’s the result after clicking on the new top-right power button:

That’s my “right” part’s configuration:

That’s all! Stay tuned for other posts about Sway.

LazyVim 14, some new and breaking features

LazyVim has just released version 14, with some new, interesting, and breaking features.

The first breaking feature is that Telescope is no longer the default fuzzy finder picker; the default is fzf-lua. I haven’t been using Neovim for a long time, but I do like Telescope, and I’m not sure I like the new picker.

Here’s a screenshot using Telescope:

Here’s the one using fzf-lua:

Maybe it’s just a matter of configuring the size and dimensions of the window, but I prefer Telescope: I can read the items better, while in fzf-lua, the list width is too short.

This can be seen better when using a folder with a deep subfolder, for example, based on my previous Neovim, Java, and LazyVim post. For example, with Telescope:

While with fzf-lua I can hardly see the names of the files:

The same holds when searching for keymaps (“<leader> s k”). Here’s Telescope:

And here’s fzf-lua (I don’t even understand the strange sequence of characters reported as “details” for some keymaps):

The news page tells you that you have to enable the “editor.telescope” extra to bring back Telescope as the default picker:

However, I went for the alternative shown on the “Telescope” page: it’s just a matter of setting an option (vim.g.lazyvim_picker = “telescope”) to use Telescope as the default picker:

Another change, which is not as breaking as the previous one but concerns the UI, is that the “which-key” interface now defaults to “helix”, so the useful which-key contents are not shown at the bottom of the screen as they used to be:

the contents are shown on the right:

I like this change: the which-key pop-up does not risk covering your buffer, and it can display more content more readably. With the old interface, when the cursor was near the end of the buffer, the interface could not show all the commands, and you had to scroll it. Now, it’s rarer for this to happen. I’ll keep it with the new “helix” preset.

If you look at the corresponding page, it is straightforward to change the corresponding “preset”:

The other breaking change is “blink.cmp as a replacement for nvim-cmp“; even in this case, you can revert to the old behavior by enabling the corresponding Extra. This doesn’t seem to break anything for me for the moment, so I’ll keep that.

You might also want to try the new keymaps; in particular, I like these ones:

Enjoy this new version! 🙂

Neovim and Java with LazyVim, part 2: IDE mechanisms

This is the second part of a few tutorials on Java development with Neovim using the LazyVim setup.

This post assumes you already read and applied all the steps of the first part.

You need Java installed (possibly 21), while Maven is optional.

I will use this Maven example during the tutorial: https://github.com/LorenzoBettini/maven-bank-example, part of my TDD book.

The final result of this series of tutorials can be found here: https://github.com/LorenzoBettini/lazyvim-java. The “main” branch always points to the latest blog post.

The end of the first part is still the branch “first-blog-post” (this blog post does not touch the configuration files).

In the first part, we saw how to enable Java LSP in the LazyVim distribution. We also saw a few interesting features for programming in Java in Neovim with such a configuration. In this post, we’ll see a few of the IDE mechanisms we get from the Java LSP in LazyVim. Let’s continue from where we left off (remember, I’ll use the above-mentioned Maven project).

Code completion and snippets

Let’s instead create a new Java file inside a source folder of this Maven project (e.g., starting from “src/main/java”), either with “:e <path>” or with the “Neotree” file explorer that comes installed and configured with LazyVim: toggle the explorer with “<leader> e”. I’ll create an Example.java inside the “app” subfolder: get to that folder, press “a” (for add), write the name “Hello.java”, and press ENTER to confirm:

Pressing ENTER again will open the empty file in a new buffer.

We’ll use snippets configured by default instead of writing all the contents, e.g., the correct package and the class implementation. Let’s enter INSERT mode and press Ctrl+SPACE. Start typing “cl”, we should get to the snippet we need (see the preview on the right): the one with all the correct contents:

Press ENTER to accept the snippet, and we get a valid Java class, with the cursor positioned inside the class’ body:

Let’s keep on using snippets to create a “main” method:

And a “System.out.println” statement:

Till you can write the “Hello World” string as an argument!

Snippets come from various sources (including the “friendly-snippets” plugin that LazyVim configures by default; (see https://github.com/rafamadriz/friendly-snippets/blob/main/snippets/java/java.json for the available templates). However, the best ones are those from Jdtls (like the “class” snippet we used above).

You might want to explore other useful snippets, like creating a public method:

Once the snippet has been selected, you have placeholders to edit the return type, the name, etc. Start typing to insert the return type, press TAB to get to the name, change the name, and press TAB to get inside the parenthesis to insert the parameters possibly. The final TAB will get you inside the method body. Shift TAB goes in the other direction.

Code actions

Let’s look at a few other features provided by LazyVim Java Extra. Note that these LSP-related features are not strictly related to the Java LSP but to the general configurations for LSPs. The same keybindings and mechanisms are meant to work with other LSPs, though not all LSPs implement all such capabilities.

You have code actions, using “<leader> c a”, which depend on the context you’re in the source file:

This menu is also handy for having the IDE fix errors for you (handy when you apply TDD); in Eclipse, we’d call them “quick fixes”, but in this context, “quick fix” is meant for quickly seeing all the diagnostics. For example, I intentionally refer to a non-existing symbol, and I use “Code actions” to have the IDE create that field for me (remember to exit insert mode to get the diagnostics; by the way, such dialogs can be filtered starting typing something to access the desired item quickly):

As another example, let’s change the package name in the “Hello.java” file we created above:

As expected, this makes the file invalid, as reported in the editor (and also in the explorer). Let’s use the code actions:

We use the first one because we want the file to be moved to the correct source directory:

Let’s do the opposite, press “c” in the Explorer to cut it:

move to the “app” folder and “p” to paste it:

The file is invalid again; this time, we use the second code action to change the package name in the file to respect the current source directory (remember that you must be in the buffer for code actions, not in the Neotree; you can use Ctrl + L to move to the right window):

Navigating through symbols

You might explore other features like showing symbols; there are many ways of achieving that in LazyVim and its default plug-ins. You can use “<leader> s k” to search for keybindings and start typing “Symbol”. Here are some examples.

“<leader> s s” gives you symbols with Telescope:

“<leader> s S” gives you symbols with Telescope in the entire workspace; the workspace is an LSP concept, but for the Java LSP, it can be seen as the Eclipse workspace that is being used under the hood: you get access to all the Java source files and all the types in the dependencies of the project. Start typing (be patient if you have a workspace/project with tons of dependencies), and you should see the classes of your sources and dependencies. In this example, we can access all the types starting with “Assert” from the JDK and the dependencies (in this project, JUnit, AssertJ, Log4J).

Selecting one will open it in the editor. If it comes from a dependency, the LSP will download the sources of the corresponding Maven artifact and show them in a read-only buffer.

“<leader> c s” gives you the “Trouble” symbol window, which is similar to the Eclipse Outline (note that it is synchronized with the editor; I changed the color theme to make selections more visible):

“<leader> c S” (capital “S”) gives you the “Trouble” window with references and calls to the currently focused element (again, the window is kept synchronized):

Then you have all the navigation capabilities, typically starting with “g,” like goto definition or goto references (opening a telescope picker for all the references in the workspace). You can also explore jump operations like “[c”, “]c”, “[f”, “]f” to navigate around a source code (class definitions, method definitions, etc.)

You also have some refactorings, like “Rename” (of course, updating references throughout the project’s files) and “Extract” (variable, constant, methods). You can experiment with them by searching for them with “<leader> s k”  or using “<leader> c” and then waiting for the available commands (provided by the Neovim plugin “which-ley”, automatically configured by LazyVim).

That’s all for this post; stay tuned for future blog posts, where we’ll cover:

  • dealing with Maven dependencies (with the current configuration, editing POM files is not yet ideal);
  • how to run/debug Java programs (with the current configuration, this is not yet possible);
  • how to run tests
  • maybe other features

Sway in Arch Linux: other configurations

Let’s continue the Sway window manager (in Arch Linux) series I started in my previous post and continued in the other post.

Screenshots

We can use “grimshot”, part of sway-contrib, to take screenshots, which requires the main program for performing the actual screenshot, “grim”.

By default, such utilities are installed in “/usr/share/sway-contrib”, which is not part of the PATH. So, put that directory in the PATH environment variable or prefix the call with the full path.

Here’s the help of grimshot:

You can try running that from the command line and do some experiments.

Then, you might want to set a few keybindings in the Sway config file, e.g.:

Let’s also install an image viewer, like “Eye of Gnome”:

This way, we can open the generated screenshot images.

Brightness and volume

How do you set the screen’s brightness and volume using the corresponding keys?

First, we need a program to control the brightness, “brightnessctl”. For the volume, we use “wpctl”, part of “wireplumber”:

Here are the keybindings for the media keys for brightness and volume (if your keyboard has them):

Note the use of “-l 1.0” meaning that we don’t want to allow the wireplumber to increase the volume above 100%.

Running from a Display Manager

The default installation already created a file in the appropriate folder to let SDDM start the Sway session.

Let’s install the SDDM package:

Then, we enable the service at boot:

If we want to start it without rebooting, the first time we run:

And now you can enter Sway from here.

Stay tuned for other posts on the Sway WM!