# Treinamento Linux
# Como ter mais agilidade no terminal
Dica: USAR TAB
## Navegação
### Emacs keybinds
* https://readline.kablamo.org/emacs.html
### Vi Keybinds
* http://www.viemu.com/vi-vim-cheat-sheet.gif
## Pedindo por ajuda
### Manual de comandos
```
man COMANDO
```
Descricao de um comando:
```
whatis COMANDO
```
Alternativa:
```
tldr COMANDO
```
## Coringas
Principais coringas:
```
*
?
[]
{}
```
Informacoes adicionais:
https://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm
## Gerenciamento de arquivos e diretorios (pasta eh no Windows)
### Criar diretório
```
mkdir -p
```
_DICA:_ Criar um diretorio e entrar nele:
```
mkdir foo && cd "$_"
```
### Remover diretório
```
rm ARQUIVO
rm -r ARQUIVO/DIRETORIO
rm -rf ARQUIVO/DIRETORIO
```
Esta com duvida? Lembre-se que shell nao tem lixeira!
```
rm -i ARQUIVO
```
### Listar diretorio
```
ls CAMINHO
ls -a CAMINHO
ls -ls CAMINHO
ls -la CAMINHO
ls CAMINHO | less
```
### Mudar de diretorio
```
cd CAMINHO
cd ~
cd ../../../../
cd -
```
#### Salvar um caminho na pilha
```
pushd caminho
popd
```
### Onde estou?
```
pwd
```
### Copiar arquivos e diretorios
```
cp
cp -r
```
Pode ser utilizado `\` como separador para copiar arquivos com espaco
### Mover arquivos e diretorios
```
mv
```
### Criar modificar arquivos
```
touch ARQUIVO
> ARQUIVO
```
## Busca de arquivos, diretórios e historico de comandos
### find
```
find -name teste /teste
```
* https://www.tecmint.com/35-practical-examples-of-linux-find-command/
* https://danielmiessler.com/study/find/
### find mais amigável
* https://github.com/junegunn/fzf
### grep e git grep
```
grep EXPR ARQ
grep -r teste teste.txt
grep -i Orelha cabeca.txt
```
* https://ryanstutorials.net/linuxtutorial/cheatsheetgrep.php
### Encontrar caminho do comando
```
which COMANDO
```
### bg fg
### Pesquisar no historico
Ctrl-r
Usando fzf
## Visualizar arquivos e diretorios
#### Ler o conteudo de um arquivo
```
cat ARQUIVO
less ARQUIVO
```
#### Ler o inicio de um arquivo
```
head ARQUIVO
head -n 2 ARQUIVO
```
#### Ler o final de um arquivo
```
tail ARQUIVO
tail -n 2 ARQUIVO
tail -f ARQUIVO
```
#### Visualizar como uma arvore
```
tree CAMINHO
tree -L 2 CAMINHO
```
#### xdg
https://wiki.archlinux.org/index.php/XDG_user_directories
```
xdg-open arquivo.pdf
```
### Executar mais de um comando por vez
```
&&
;
```
### Redirecionador de comandos
### Ler as mensagens do kernel
```
dmesg
dmesg -w
```
## Compactadores
Opcao tar:
-c cria
-t lista
-x extrai
### tar: criar pacote
```
tar -cvf aquivo.tar diretorio
```
### tar: listar arquivos em um pacote
```
tar -tvf aquivo.tar
```
### tar: extrair pacote
```
tar -xvf arquivo.tar.gz
tar -xvf arquivo.tar.gz -C caminho
```
### Compactar gz
```
tar -cvzf arquivo.tar.gz diretorio
```
### Compactar bz2
```
tar -cvjf aquivo.tar.bz2 diretorio
```
### gzip gunzip zcat
```
gunzip arquivo.gz
gzip arquivo.txt
zcat arquivo.txt.gz
```
### zip unzip
```
zip -r arquivo.zip caminho
unzip arquivo.zip
```
### Comandos de rede
wget para baixar arquivos
curl eh muito utilizado para desenvolvimento
axel para arquivos grandes, eh bem rapido o donwload
### SSH acessando um host remoto e copiando arquivos
```
ssh user@ipaddr
scp arquivo user@ipaddr
scp -r diretorio user@ipaddr
```
### Gerenciador de arquivos pelo terminal
ranger
### Leitor de pdf
zathura
## Ferramentas e dicas para otimizar o fluxo de trabalho
### Clipboard Manager
* O que é o clipboard?
* https://wiki.archlinux.org/index.php/Clipboard
* Diferença entre primary e clipboard
* Qual ferramenta usar?
* O Parcellite
### Configurar sudo
* https://wiki.archlinux.org/index.php/Sudo#Tips_and_tricks
* Senha valida para todos os terminais
* Aumentar o timeout
### SSH sem usar knowhosts
```
alias issh='ssh -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
alias iscp='scp -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
```
## Fluxo de trabalho para ser mais produtivo
### Dicas para organizar o ambiente
### Emacs keybinds no gtk
https://wiki.archlinux.org/index.php/GTK#Emacs_key_bindings
### Trocar Caps para Ctrl
### Dicas pesquisa na web?
Melhores fontes de informação:
Dica: Acostume-se a pesquisar em inglês
https://wiki.archlinux.org/
https://wiki.gentoo.org/wiki/Main_Page
## Tmux
### Informacoes gerais
https://github.com/tmux/tmux/wiki
https://wiki.archlinux.org/index.php/Tmux
### Configuracao
Adicione o conteudo abaixo no arquivo `~/.tmux.conf`
```
# Setting the prefix from C-b to C-a
set -g prefix C-a
unbind C-b
bind-key a last-window
bind C-a send-prefix
# Setting the delay between prefix and command
set -s escape-time 0
set-option -g repeat-time 1000
# Scrollback buffer number of lines
set -g history-limit 10000
# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"
# Refresh 'status-left' and 'status-right' more often
set -g status-interval 5
# tmux messages are displayed for 3 seconds
set -g display-time 3000
# Super useful when using "grouped sessions" and multi-monitor setup
setw -g aggressive-resize on
# Focus events enabled for terminals that support them
set -g focus-events on
# Renumber windows sequentially after closing any of them.
set -g renumber-windows on
# Set the base index for windows to 1 instead of 0
set -g base-index 1
# Set the base index for panes to 1 instead of 0
setw -g pane-base-index 1
# Enable emacs mode
set -g status-keys emacs
# Display mouse off
set -g mouse off
# Easier and faster switching between next/prev window
bind C-p previous-window
bind C-n next-window
# Enable clipboard
set -g set-clipboard on
# Enable xterm-keys in your tmux configuration
setw -g xterm-keys on
# Key bindings for horizontal and vertical panes
unbind %
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# Swap Window
bind-key -r "<" swap-window -t -1
bind-key -r ">" swap-window -t +1
# Moving between panes
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Resize pane
bind -r H resize-pane -L 3
bind -r J resize-pane -D 3
bind -r K resize-pane -U 3
bind -r L resize-pane -R 3
# Open new window and new panel at current directory
bind C new-window -c "#{pane_current_path}"
# Preview sessions in zoom mode
set-hook -g pane-mode-changed 'if -F "#{||:#{&&:#{pane_in_mode},#{&&:#{pane_in_mode},#{!=:#{pane_mode},copy-mode}}},#{&&:#{?pane_in_mode,0,1},#{window_zoomed_flag}}}" "resizep -Z"'
# Reload the file with Prefix r
bind r source-file ~/.tmux.conf \; display "Reloaded!"
### Status Bar confs
#
# Disable activity alerts
setw -g monitor-activity off
set -g visual-activity off
# Set pane colors - hilight the active pane
set -g pane-border-style fg=colour235
set -g pane-active-border-style fg=colour240
# colorize messages in the command line
set -g message-style fg=brightred,bg=black
# Status Bar
set -g status on
set -g status-justify centre
# Set color for status bar
set -g status-bg colour0
set -g status-fg yellow
# Set window list colors - red for active and cyan for inactive
# set-window-option -g window-status-style fg=brightblue
# set-window-option -g window-status-style bg=colour0
set-window-option -g window-status-style dim
# set-window-option -g window-status-current-style fg=brightred
# set-window-option -g window-status-current-style bg=colour0
set-window-option -g window-status-current-style bright
# Status bar left
set -g status-left-length 40
set -g status-left "#[fg=green] #S :: #I - #P "
# Status bar rigth
set -g status-right-length 40
set -g status-right "#{prefix_highlight} #[fg=green] %R :: #H "
```
### Cheat Sheet
Start new tmux:
tmux new -s NAME
Attach a session:
tmux attach -t NAME
List sessions:
tmux ls
Kill session:
tmux kill-session -t NAME
#### Sessions
:new<CR> new session
s list sessions
$ name session
#### Windows (tabs)
c create window
w list windows
n next window
p previous window
f find window
, name window
& kill window
#### Panes (splits)
| vertical split
- horizontal split
o swap panes
q show pane numbers
x kill pane
+ break pane into window (e.g. to select text by mouse to copy)
- restore pane from window
⍽ space - toggle between layouts
<prefix> q Show pane numbers
<prefix> z toggle pane zoom
PREFIX : resize-pane -J (Resizes the current pane down)
PREFIX : resize-pane -K (Resizes the current pane upward)
PREFIX : resize-pane -H (Resizes the current pane left)
PREFIX : resize-pane -L (Resizes the current pane right)
## Copy mode:
Enter Copy Mode [
Clear selection C-g
Copy selection M-w
In copy mode use Emacs keys to navigate
#### Misc
d detach
t big clock
? list shortcuts
: prompt