#!/usr/bin/env python
# Author: James Cherti
# License: MIT
# URL: https://www.jamescherti.com/python-calculate-the-size-of-a-directory-and-its-sub-directories/
"""Calculate the size of a directory and its sub-directories."""
import os
from pathlib import Path
from typing import Union
def get_size(path: Union[Path, str], include_dirs_size=True) -> int:
"""Return the size of a file or a directory in bytes."""
path = Path(path)
size = 0
if path.is_dir():
list_paths = path.glob("**/*")
elif path.is_file():
list_paths = [path] # type: ignore
else:
list_paths = [] # type: ignore
for cur_path in list_paths:
if not include_dirs_size and cur_path.is_dir():
continue
if not cur_path.is_symlink():
size += cur_path.stat().st_size
return size
Code language: Python (python)
Author Archives: James Cherti
A Vim function that returns all monospaced fonts (UNIX / Linux only)
" Language: Vim script
" Author: James Cherti
" License: MIT
" Description: A function that returns all available monospaced fonts
" (Linux and UNIX only).
" URL: https://www.jamescherti.com/vim-a-function-that-returns-all-available-fonts-unix-linux-only
function! FontList() abort
let l:result = []
if has('win32') || !has('gui_running') || !executable('fc-list')
return l:result
endif
" Search for monospaced fonts (spacing=100)
let l:fclist_output = systemlist('fc-list :spacing=100')
let l:style_var = 'style='
for l:fclist_line in l:fclist_output
let l:fclist_line_items = split(l:fclist_line, ':')
let l:font_file = l:fclist_line_items[0]
let l:list_font_names = split(l:fclist_line_items[1], ',')
let l:font_name = trim(l:list_font_names[0])
if len(l:fclist_line_items) <= 2
if index(l:result, l:font_name) ==# -1
call add(l:result, l:font_name)
endif
continue
endif
let l:font_style = l:fclist_line_items[2]
if l:font_style[0:len(l:style_var)-1] ==# l:style_var
for l:font_style in split(l:font_style[len(l:style_var):], ',')
let l:font_name = l:font_name . ' ' . trim(l:font_style)
if index(l:result, l:font_name) ==# -1
call add(l:result, l:font_name)
endif
endfor
endif
endfor
return l:result
endfunction
Code language: Vim Script (vim)
A Vim plugin for persisting and restoring Vim editing sessions easily and effortlessly
The Vim plugin vim-easysession, written by James Cherti, offers a convenient and effortless way to persist and restore Vim editing sessions. It can significantly increase productivity and save a lot of time for users who frequently switch between different projects and those who frequently open and close the Vim editor.
In addition to its automatic session management capabilities, the Vim plugin Easysession also offers a variety of useful Vim commands that allow users to save, switch, list and delete sessions manually.
Easysession features
- Save and restore the session “main”, which includes various settings such as the font, background, and color scheme (The
&guifont
,&background
, and the color scheme are also part of the session file that is generated by Easysession) - Auto-complete the Vim commands
:EasySessionLoad
and:EasySessionRemove
- Specify the directory where the session files are located:
let g:easysession_dir = expand('~/.my_vim_sessions')
- Save the session:
:EasySessionSave
- Switch to a different session:
:EasySessionLoad SESSION_NAME
- List the available sessions:
:EasySessionList
- Delete a session with:
:EasySessionRemove SESSION_NAME
Please star vim-easysession on GitHub to support the project!
Installation
The Vim plugin Easysession can be installed with Vim’s built-in package manager (Vim 8 and higher):
mkdir -p ~/.vim/pack/jamescherti/start
cd ~/.vim/pack/jamescherti/start
git clone --depth 1 https://github.com/jamescherti/vim-easysession
vim -u NONE -c "helptags vim-easysession/doc" -c q
Code language: JavaScript (javascript)
The plugin can also be installed with any third-party plugin manager such as Pathogen or Vundle.
Conclusion
The Vim plugin Easysession provides an easy and effortless way to persist and restore Vim editing sessions. Its automatic session management capabilities, manual commands, auto-complete feature, and its ability to specify the directory for saves session are all valuable features that make it easy to manage Vim editing sessions.
If you are looking for a way to manage and organize your Vim session more efficiently, the Easysession Vim plugin is an excellent option to consider.
Master the HJKL navigation with the Vim plugin Hjklmode, which can help breaking the habit of moving the right hand away from the home row
Vim’s popularity is largely due to its ability to increase productivity through efficient navigation and editing techniques. One such technique is HJKL navigation, which allows using the H, J, K, and L keys to move the cursor left, down, up, and right. This method of navigation may seem strange at first, but it is actually a very efficient way to navigate through text and can greatly increase productivity when using Vim.
In addition to that, the HJKL navigation can help reduce wrist strain, as it allows moving the cursor without having to reach for the arrow keys, which are located in a position that can cause wrist to bend and flex in an unnatural position.
However, HJKL navigation can be hard to learn at first, because it requires using unfamiliar key combinations to move the cursor. One way of making it easier to learn HJKL navigation is by using the Vim plugin Hjklmode, written by James Cherti. You can use the Vim plugin to force yourself to rely on HJKL navigation to move around the text.
If you are interested in trying the Hjklmode Vim plugin, you can download it from its Git repository: https://github.com/jamescherti/vim-hjklmode
Hjklmode Vim plugin features
One of the key features of the Vim plugin Hjklmode is its ability to disable certain keys that require moving the hand away from the Touch Typing position, which can disrupt typing flow. These keys include: Backspace, Insert, Delete, Home, End, Page Up and Page Down, Arrows.
In addition to disabling certain keys, the Vim plugin Hjklmode also adds key mappings for Alt+h, Alt+j, Alt+k, Alt+l to Insert Mode, Command Mode, and Terminal Mode, which allow moving the cursor even in those modes.
Overall, the Hjklmode Vim plugin is a great tool for those looking to learn the HJKL navigation to increase their efficiency and productivity. Whether you are a beginner or an experienced Vim user, the Hjklmode Vim plugin can help you break the habit of moving your hand away from the home row/Touch Typing position.
Do you like the Vim plugin Hjklmode? Please add a star to vim-hjklmode on GitHub.
Install the Vim plugin Hjklmode with Vim’s Built-in package manager (Vim 8 and above)
mkdir -p ~/.vim/pack/jamescherti/start
cd ~/.vim/pack/jamescherti/start
git clone --depth 1 https://github.com/jamescherti/vim-hjklmode
vim -u NONE -c "helptags vim-hjklmode/doc" -c q
Code language: JavaScript (javascript)
(The plugin can also be installed with any third-party plugin manager such as Pathogen or Vundle)
What are the alternatives to pressing the Escape, Backspace, and Arrow keys?
Built-in Vim Key Mapping | Equivalent to |
Ctrl-[ | Escape |
Ctrl-h | Backspace |
Ctrl-f | Page down |
Ctrl-b | Page up |
h | Left |
j | Down |
k | Up |
l | Right |
0 (zero) | Home |
$ | End |
For more information:
- :help motion.txt
- :help search-commands
Configure XFCE 4 programmatically with the help of watch-xfce-xfconf
Configuring XFCE 4 programmatically is useful if you wish to have the same XFCE 4 settings on several computers.
The command-line tool jamescherti/watch-xfce-xfconf (previously named: monitor-xfconf-changes) will help you to create shell scripts that can configure XFCE 4 programmatically. It will display the xfconf-query commands of all the Xfconf settings that are being modified by XFCE 4 programs (like xfce4-settings-manager, thunar, ristretto, etc.).
Please star watch-xfce-xfconf on GitHub to support the project!
How to use the command-line tool monitor-xfconf-changes?
The watch-xfce-xfconf command-line tool can be installed locally, in ~/.local/bin/watch-xfce-xfconf
, using pip:
pip install --user watch-xfce-xfconf
Run xfce4-settings-manager in the background:
xfce4-settings-manager &
Execute watch-xfce-xfconf:
~/.local/bin/watch-xfce-xfconf
Code language: plaintext (plaintext)
The xfconf-query commands will be displayed by watch-xfce-xfconf on the terminal every time an XFCE 4 setting is changed (with xfce4-settings-manager, thunar, ristretto, etc.).
You can then use the xfconf-query commands to configure XFCE 4 programmatically.