This shows you the differences between two versions of the page.
ii:labs:01:tasks:03 [2021/10/08 21:41] radu.mantu created |
ii:labs:01:tasks:03 [2024/10/18 12:35] (current) radu.mantu [03. [20p] Quality-of-life improvements] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 03. [20p] quality-of-life aliases ==== | + | ==== 03. [20p] Quality-of-life improvements ==== |
+ | |||
+ | [[https://ocw.cs.pub.ro/courses/_media/ii/labs/01/tasks/man-demo.gif|{{ :ii:labs:01:tasks:man-demo.gif?700 |}}]] | ||
+ | <html><center><i> Click GIF to maximize. </i></center></html> | ||
=== [10p] Task A - Color man pages === | === [10p] Task A - Color man pages === | ||
Line 23: | Line 26: | ||
</code> | </code> | ||
+ | <note important> | ||
+ | If the command above doesn't work, it's possible that **echo** doesn't interpret escape sequences. | ||
+ | |||
+ | Either add the **-e** argument to //enable interpretation of backslash escapes//, or replace **echo** with **printf** and add a ''\n''. | ||
+ | </note> | ||
We can use this... Add this at the end of your //.zshrc// file, source it, and open the same man page again: | We can use this... Add this at the end of your //.zshrc// file, source it, and open the same man page again: | ||
Line 43: | Line 51: | ||
* ''man()'': we're basically replacing the **man** command with a //bash function//. | * ''man()'': we're basically replacing the **man** command with a //bash function//. | ||
* ''LESS_TERMCAP_*'': these are variables used (indirectly) by **man** to format its output. The authors of these pages decide how and when to use them. We decide what they are. By defining these variables before running a bash command, we set them only in the environment of the started process and will disappear when said process terminates. | * ''LESS_TERMCAP_*'': these are variables used (indirectly) by **man** to format its output. The authors of these pages decide how and when to use them. We decide what they are. By defining these variables before running a bash command, we set them only in the environment of the started process and will disappear when said process terminates. | ||
- | * ''command man "$@"'': here, **command** tells **bash** / **zsh** to use the //original// man command, not the function we just defined. If you're wandering why this is necessary, think back to the whole ''tmux'' and ''zsh'' fiasco described earlier. ''"%@"'' here is substituted with all the arguments that the //**man** bash function// received and passes them on to the //**man** command//. | + | * ''command man "$@"'': here, **command** tells **bash** / **zsh** to use the //original// man command, not the function we just defined. If you're wandering why this is necessary, think back to the whole ''tmux'' and ''zsh'' fiasco described earlier. The ''"$@"'' here is substituted with all the arguments that the //**man** bash function// received and passes them on to the //**man** command//. |
+ | |||
+ | **//Alternatively//** you can change the [[https://unix.stackexchange.com/questions/144016/what-is-a-pager|pager]] used by **man** from **less** to **[n]vim**. This means that every time you bring up the manual page for something, it will be opened in your favorite text editor. | ||
+ | |||
+ | First of all, let's install **nvim**. This is a //fork// of **vim** created due to Bram Moolenaar (the author of **vim**) rejecting a large number of improvements proposed by the community. | ||
+ | |||
+ | <code bash> | ||
+ | $ sudo apt install neovim | ||
+ | </code> | ||
+ | |||
+ | To change **man**'s pager from **less** to **nvim**, add the following line to your ''.zshrc'' and source it. | ||
+ | |||
+ | <code> | ||
+ | export MANPAGER='nvim +Man!' | ||
+ | </code> | ||
+ | |||
+ | Now, try opening a manual page: | ||
+ | |||
+ | <code bash> | ||
+ | $ man 2 open | ||
+ | </code> | ||
=== [10p] Task B - Color iproute2 output === | === [10p] Task B - Color iproute2 output === | ||
- | If you scroll back to the first //gif// in this lab, you might notice the execution of the ''ip addr show'' command, to list the network interfaces on my system, an their IP addresses. This is similar to the **ipconfig** command in Windows cmd. In fact, prior to **ip**, Linux had an even more similar command, called **ifconfig** (yeah...) But **ifconfig** is now deprecated and is not even installed by default in most modern distributions. People still (incorrectly) use it because they don't like the monochrome output of **ip** and it's output is more nicely formatted. But **ip** can use ANSI escape codes if you feed it the ''-c'' flag. To this end, let's specify an [[https://tldp.org/LDP/abs/html/aliases.html|alias]] (basically a shortcut) in //.zshrc//. | + | If you scroll back to the first //gif// in this lab, you might notice the execution of the ''ip addr show'' command, to list the network interfaces on my system, and their IP addresses. This is similar to the **ipconfig** command in Windows **cmd**. In fact, prior to **ip**, Linux had an even more similar command, called **ifconfig** (yeah...) But **ifconfig** is now deprecated and is not even installed by default in most modern distributions. People still (incorrectly) use it because they don't like the monochrome output of **ip** and it's output is more nicely formatted. But **ip** can use ANSI escape codes if you feed it the ''-c'' flag. To this end, let's specify an [[https://tldp.org/LDP/abs/html/aliases.html|alias]] (basically a shortcut) in //.zshrc//. |
<code bash> | <code bash> | ||
# alias for iproute2 color output | # alias for iproute2 color output | ||
- | ip='ip -c' | + | alias ip='ip -c' |
</code> | </code> | ||
Source //.zshrc// and try ''ip addr show'' again. Now, every time you run ''ip'', it automatically expands to ''ip -c''. | Source //.zshrc// and try ''ip addr show'' again. Now, every time you run ''ip'', it automatically expands to ''ip -c''. |