7.5 C
Vancouver
Thursday, November 21, 2024

VIM & Tmux Basics

Must read

Why?

Vi (vim) is the only editor you can be guaranteed to have on a Linux machine. If you ever have to log into a server, or container, to see what’s going on or make changes this will be all you’ll have. It will save your life.

Tmux allows you to use the terminal without having to grab the mouse. It can run processes in the background without making them background.

Prep

Install vim and tmux on OSX

brew install vim
brew install tmux

Or use a Ubuntu instance to create the following Vagrantfile and run vagrant up

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-20.04"
end

Vim

Cheetsheet: https://vim.rtorr.com/

For users with an existing vimrc file use the following env variable to start afresh


export VIMINIT=":set runtimepath^=$HOME/.vim|:source $HOME/.vim/vimrc"

Edit a javascript file without using any local vimrc config

cd tec/web/react-dashboard
vim -u NONE webpack.config.js

Navigation

^f - forward
^b - back

:100 - go to line 100
:0 - start of file

G - go to end of file
^g - file length and current position

$ - end of line
0 - start of line

/ - reg exp search forward
? - reg exp search back

:x - write and quit
:q - quit
:q! - quit, don't save

Commandline

:filetype
:filetype plugin indent on
:filetype detect
:syntax on

:set backspace=start,eol,indent

Editting

i  - insert
a - append
ESC - exit editting mode
dd - deletes line and put it into your stack
yy - yanks (copies) a line and puts it into your stack
p - pastes the last line in your stack

Create a vimrc file

:! mkdir ~/.vim - run a shell command
:e ~/.vim - edit the directory (this will open netrw)

Netrw – https://gist.github.com/t-mart/610795fcf7998559ea80

% - will create a new file, enter vimrc

vimrc

filetype plugin indent on
syntax on
set backspace=start,eol,indent
set hidden
set noswapfile

let $RC="$HOME/.vim/vimrc"
:so % - source current file

Buffers

:bn - next buffer
:bp - previous buffer
:bd - delete buffer
:ls - list all open buffers
:b# - go to buffer number

Other commands

:set number - show line numbers
:e $RC - open .vimrc
:set paste - take what ever comes next

mV - create global filemark call V
:marks - list filemarks
`V - return to the V filemark

:set hls - highlight search
:nohls - turn off highlight search 

:1,$s/blah/foo/g - search from line 1 to the end of the file and replace all instances of blah with foo

^a - if cursor is on a number will increment it by 1

:^f - view commandline history

Tmux

https://tmuxcheatsheet.com/

create a ~/.tmux.conf file using vim.


# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# split panes using | and -
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %

# reload config file (change file location to your the tmux.conf you want to use)
bind r source-file ~/.tmux.conf

# use vi keys for navigation 'C-a [' to enter
setw -g mode-keys vi

Start tmux

tmux

Create a new window

C-a c

Rename session

C-a $

Move between windows

C-a n - next window
C-a p - previous window
C-a 1 - window 1

Create panes

C-a | - create a vertical window
C-a - - create a horizontal window

Scrolling in a window (using vim keybindings)

C-a [ - enter vim mode
^b - scroll back
^f - scroll forwards
/ - reg exp search forward
? - reg exp search back

Exit tmux, without killing the session

C-a C-d

Start a second tmux session

tmux new -s session2

List session

tmux ls

Connect to a running session by name

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article