Vim Tips: Running Commands

Let’s explore a few different ways to run commands in our terminal from the comfort of neovim. Trust me, it’s cooler than it sounds! 😎

Here’s a list of commands that I use all the time (and you should too!).

# run a command
:!sh <cmd>

# execute the line and replace with the output
:.!sh

# execute without replacing
:.w !sh

# execute without replacing selection and it will persist until next selection!
:'<,'>w !sh

# execute a the line 8
:8w !sh

# execute and replace a the line 8
:8 !sh

# execute the selection in python
:'<,'>w !python -c "$(cat)"

# execute and replace the selection in python
:'<,'>!python -c "$(cat)"

Hands-on

Hey! This is my absolute favorite command ever! I use it all the time when I’m writing docs or README files because, let’s face it - who wants to manually copy-paste code outputs? Not me! 😅 Instead of doing things the boring way, I use this neat little trick to get things done quickly. Plus, it’s super handy when you want to format text using other tools too like sql queries!

Docker Compose Env Var Adventures

Let’s dive into the world of Docker Compose and environment variables, where hilarity ensues and variables come alive (or do they?).

We’ve all been there. Playing hide and seek with environment variables in Docker Compose. Let’s see how our dear friend MYVAR enjoys this grand game of Peekaboo!

Makefile

Makefile

Some times you need a good starting point, here is mine:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# variables
PYVER  := 3.10
venv   := .venv
python := $(venv)/bin/python
pip    := $(venv)/bin/pip


##@ Utility
.PHONY: help
help:  ## Display this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make <target>\033[36m\033[0m\n"} /^[a-zA-Z\._-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)


##@ Setup
$(venv):
    @python$(PYVER) -m venv $(venv)


.PHONY: install
install: $(venv)  ## install
	$(pip) install . -r requirements.txt


##@ Development
.PHONY: dev
dev: $(venv) ## install dev mode
	$(pip) install -e .[dev]

.PHONY: test
test: $(venv) ## run tests
	$(python) -m pytest tests

.PHONY: lint
lint: $(venv)  ## run linting check
	$(python) -m ruff ./src

.PHONY: format
format: $(venv)  ## fomat code using ruff
	$(python) -m ruff format src test


.PHONY: requirements.txt
requirements.txt:  ## update requirements.txt, e.g. make requirements.txt
	@test -d /tmp/venv && rm -r /tmp/venv || true
	@$(python) -m venv /tmp/venv
	@/tmp/venv/bin/python -m pip -q install pip -U
	@/tmp/venv/bin/python -m pip -q install . --progress-bar off
	@/tmp/venv/bin/python -m pip freeze > requirements.txt

Markdown preview PDF friendly

How to print a PDF

Edit the following files:

  • ~/.local/share/nvim/lazy/markdown-preview.nvim/app/_static/markdown.css
  • ~/.local/share/nvim/lazy/markdown-preview.nvim/app/_static/page.css

add the following lines at the bottom:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@media print {
  #page-header {
    display: none;
  }
  .markdown-body {
    background-color: #fff;
    border: none;
    color: none;
  }
  main {
    background-color: #fff;
  }
}

Mermaid example

Cambiando el formato de un arhivo.

Me encontraba trabajando tranquilamente cuando tuve la necesidad de copiar un archivo de un servidor a mi maquina local para continuar trabajando, asi que me dispuse a ejecutar scp <server> ~/file.sh y al ejecutarlo me encuentro con este error:

1
2
3
4
5
6
$ bash ./file.sh
./file.sh: line 2: $'\r': command not found
 ")60)) * 5rror: invalid arithmetic operator (error token is "
./file.sh: line 6: $'\r': command not found
./file.sh: line 7: syntax error near unexpected token `$'{\r''
'/file.sh: line 7: `show_progress() {

Solución

Me llevo unos minutos darme cuenta de que el problema venia por el formato del archivo file.sh y no por la sintaxis.

Reflexiones sobre el Eterno retorno

Conociendo al Eterno Retorno

He estado pensando mucho en el ‘Eterno Retorno’ de Nietzsche. Este concepto es algo así como un faro que ilumina nuestro camino moral y nos ayuda a tomar decisiones conscientes. Además, también puede darnos una idea muy clara de quiénes somos y de si estamos a gusto con lo que vemos cuando nos miramos al espejo.

Diagramas en NeoVim made easy

Contexto

Siempre he buscado una manera simple de incorporar diagramas directamente desde Neovim, pero nunca había encontrado una solución sencilla hasta hoy.

Hasta ahora, no tenía un método sencillo para incluir diagramas, así que delegaba ese trabajo a documentación externa al código, usando Mermaid. Aunque estaba satisfecho con los resultados, me preguntaba cómo podría hacerlo más simple. La respuesta vino justamente de mi experiencia con Mermaid.