From 0833c79b34c36cf4121055f0bf929a5526d0d5ea Mon Sep 17 00:00:00 2001 From: kaernyk Date: Fri, 5 Sep 2014 07:48:24 -0400 Subject: Massive re-write I wanted to show a lot more than I had previously, though this may have gotten a little out of hand for a casual glance in comparison to the previous version. I feel that a majority of the available tools/features that tmux has to offer should be noticeable during a quick skim, and the bulk of the information is still digestible over a short period of time. --- LearnTmux.txt | 241 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 185 insertions(+), 56 deletions(-) diff --git a/LearnTmux.txt b/LearnTmux.txt index eaf3fd25..65ead04f 100644 --- a/LearnTmux.txt +++ b/LearnTmux.txt @@ -6,66 +6,195 @@ contributors: filename: LearnTmux.txt --- +```bash +# tmux is a terminal multiplexer: it enables a number of terminals to be +# created, accessed, and controlled from a single screen. tmux may be detached +# from a screen and continue running in the background, then later reattached. - tmux is a terminal multiplexer: it enables a number of terminals to be -created, accessed, and controlled from a single screen. tmux may be detached -from a screen and continue running in the background, then later reattached. +# Once you feel comfortable manipulating tmux to suit your needs, I strongly +# suggest you read the man pages, as there are TONS of features I’m not going +# to go over. - Once you feel comfortable manipulating tmux to suit your needs, I strongly -suggest you read the man pages. +# We’ll start of with managing tmux from a terminal, then introduce you to the +# keybinds: - -``` -# Session Management - - tmux new Create new session - -s "Session" Create named session - -n "Window" Create named Window - -c "/dir" Start in target directory - - C^b $ Rename current session - C^b d Detach current session - C^b D Select session to detach + tmux | tmux new # Create an unamed session + -s "Session" # Create named session + -n "Window" # Create named Window + -c "/dir" # Start in target directory - tmux attach Attach last/available session - -t "#" Attach target session - -d Detach the session from other instances + tmux attach # Attach last/available session + -t "#" # Attach target session + -d # Detach the session from other instances - tmux ls List open sessions - C^b s Select new session for attached client interactively - - kill-session Kill current session - -t "#" Kill target session - -a Kill all sessions - -a -t "#" Kill all sessions but the target - - -# Window Management - - C^b c Create another window - C^b " Split Horizontally - C^b % Split Vertically - C^b M-(1-5) 1) Tile vertically - 2) Tile horizontally - 3) Tile Vertically /w large horizontal - 4) Tile horizontally /w large vertical - 5) Tile all windows evenly - - C^b q Briefly display pane indexes - C^# Choose current window by # - C^b w Choose current window interactively - C^b n Change to next window - C^b p Change to previous window - C^b Up, Right Change to pane in selected direction - Down, left - C^b { Swap current/previous window - C^b } Swap current/next window - - C^b C-Up, Right Resize in steps of one cell - Down, left - C^b M-Up, Right resize in steps of five cells - Down, left - - exit or C^b x Kill the current window + tmux ls # List open sessions + + kill-session # Kill current session + -t "#" # Kill target session + -a # Kill all sessions + -a -t "#" # Kill all sessions but the target + + + +## Key Bindings + +# The method of controlling an attached tmux session is fairly simple, but +# there are two keys we will see repeated throughout this write-up (as well as +# the man pages). + + (C-b) == Ctrl + b THEN ? to list all keybinds + +# The default keybinds are as follows: + + C-b # Send the prefix key (C-b) through to the application. + C-o # Rotate the panes in the current window forwards. + C-z # Suspend the tmux client. + ? # List all key bindings. + : # Enter the tmux command prompt. + r # Force redraw of the attached client. + + c # Create a new window. + $ # Rename the current session. + , # Rename the current window. + + ! # Break the current pane out of the window. + % # Split the current pane into two, left and right. + " # Split the current pane into two, top and bottom. + + n # Change to the next window. + p # Change to the previous window. + { # Swap the current pane with the previous pane. + } # Swap the current pane with the next pane. + + s # Select a new session for the attached client interactively. + w # Choose the current window interactively. + 0 to 9 # Select windows 0 to 9. + + d # Detach the current client. + D # Choose a client to detach. + + & # Kill the current window. + x # Kill the current pane. + + Up, Down # Change to the pane above, below, left, or right. + Left, Right + + M-1 to M-5 # Arrange panes: + # 1) even-horizontal + # 2) even-vertical + # 3) main-horizontal + # 4) main-vertical + # 5) tiled. + + C-Up, C-Down # Resize the current pane in steps of one cell. + C-Left, C-Right + + M-Up, M-Down # Resize the current pane in steps of five cells. + M-Left, M-Right +``` + + +### Configuring ~/.tmux.conf + + tmux.conf can be used to set options automatically on start up, much +like how .vimrc or init.el are used. + +```bash +# Example tmux.conf +# http://dev.gentoo.org/~wired/conf/tmux.conf +# http://wiki.gentoo.org/wiki/Tmux +# https://wiki.archlinux.org/index.php/Tmux + +### Keybinds +########################################################################### + +## Prefix Adjustment +unbind-key C-b +set-option -g prefix ` +#set option -g prefix C-a + +bind-key C-a last-window +bind-key ` last-window +bind-key a send-prefix +bind-key F11 set-option -g prefix C-a +bind-key F12 set-option -g prefix ` + +## Index Start +set -g base-index 1 + +## Window Cycle/Swap +bind e previous-window +bind f next-window +bind E swap-window -t -1 +bind F swap-window -t +1 + + +### Theme +########################################################################### + +## Statusbar Color Palatte +#set-option -g status-justify centre +set-option -g status-justify left +set-option -g status-bg black +set-option -g status-fg white +set-option -g status-left-length 40 +set-option -g status-right-length 80 + + + +## Pane Border Color Palette +set-option -g pane-active-border-fg green +set-option -g pane-active-border-bg black +set-option -g pane-border-fg white +set-option -g pane-border-bg black + + +## Message Color Palette +set-option -g message-fg black +set-option -g message-bg green + + +#setw -g mode-bg black + + +## Window Status Color Palette +setw -g window-status-bg black +setw -g window-status-current-fg green +setw -g window-status-bell-attr default +setw -g window-status-bell-fg red +setw -g window-status-content-attr default +setw -g window-status-content-fg yellow +setw -g window-status-activity-attr default +setw -g window-status-activity-fg yellow + + +### UI +########################################################################### + +## Window Interface Adjustments +set-option -g status-utf8 on +setw -g mode-keys vi +setw -g mode-mouse on +setw -g monitor-activity on + +set-option -g mouse-select-pane on +set-option -g status-keys vi +set-option -g bell-action any +set-option -g set-titles on +set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) +set-option -g visual-bell off + + +## Statusbar Adjustments +set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' +set -g status-interval 5 +#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' +set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' + + +### Misc +########################################################################### +set -g history-limit 4096 +bind r source-file ~/.tmux.conf ``` -- cgit v1.2.3 From cd7ed4f9f9590f82acf953bba8d6032e1f62fa0c Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 5 Sep 2014 20:47:18 -0500 Subject: Couple changes to C to close https://github.com/adambard/learnxinyminutes-docs/pull/594 --- c.html.markdown | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 79b7aec7..cbb6d289 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -4,6 +4,7 @@ filename: learnc.c contributors: - ["Adam Bard", "http://adambard.com/"] - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] + - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] --- @@ -175,6 +176,9 @@ int main() { i2 * i1; // => 2 i1 / i2; // => 0 (0.5, but truncated towards 0) + // You need to cast at least one integer to float to get a floating-point result + (float)i1 / i2 // => 0.5f + i1 / (double)i2 // => 0.5 // Same with double f1 / f2; // => 0.5, plus or minus epsilon // Floating-point numbers and calculations are not exact @@ -194,9 +198,11 @@ int main() { 2 >= 2; // => 1 // C is not Python - comparisons don't chain. - // WRONG: - //int between_0_and_2 = 0 < a < 2; - // Correct: + // Warning: The line below will compile, but it means `(0 < a) < 2`. + // This expression is always true, because (0 < a) could be either 1 or 0. + // In this case it's 1, because (0 < 1). + int between_0_and_2 = 0 < a < 2; + // Instead use: int between_0_and_2 = 0 < a && a < 2; // Logic works on ints -- cgit v1.2.3 From 64c1ee391f4e03f363fd829ce658fc881bcffb6f Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 5 Sep 2014 20:48:42 -0500 Subject: Add lua-cn filename to close https://github.com/adambard/learnxinyminutes-docs/pull/610/files --- zh-cn/lua-cn.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/zh-cn/lua-cn.html.markdown b/zh-cn/lua-cn.html.markdown index 95a94c76..3ba098ec 100644 --- a/zh-cn/lua-cn.html.markdown +++ b/zh-cn/lua-cn.html.markdown @@ -9,6 +9,7 @@ contributors: - ["Amr Tamimi", "https://amrtamimi.com"] translators: - ["Jakukyo Friel", "http://weakish.github.io"] +filename: lua-cn.lua --- ```lua -- cgit v1.2.3 From eefa3add633ef1e80fb7403eec7854da1077188b Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 5 Sep 2014 20:51:48 -0500 Subject: Add traditional for loop bash example to close https://github.com/adambard/learnxinyminutes-docs/pull/654 --- bash.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bash.html.markdown b/bash.html.markdown index 57fb5c55..dc7d32b6 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -8,6 +8,7 @@ contributors: - ["Denis Arh", "https://github.com/darh"] - ["akirahirose", "https://twitter.com/akirahirose"] - ["Anton Strömkvist", "http://lutic.org/"] + - ["Rahil Momin", "https://github.com/iamrahil"] filename: LearnBash.sh --- @@ -140,6 +141,12 @@ do echo "$VARIABLE" done +# Or write it the "traditional for loop" way: +for ((a=1; a <= 3; a++)) +do + echo $a +done + # They can also be used to act on files.. # This will run the command 'cat' on file1 and file2 for VARIABLE in file1 file2 -- cgit v1.2.3 From b35ccf01af17b52447c98d7101ee932e7f089c52 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 5 Sep 2014 20:59:37 -0500 Subject: Add FZSS's GitHub profile link to close https://github.com/adambard/learnxinyminutes-docs/pull/666 --- zh-cn/markdown-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zh-cn/markdown-cn.html.markdown b/zh-cn/markdown-cn.html.markdown index 975ebcb5..f1166a04 100644 --- a/zh-cn/markdown-cn.html.markdown +++ b/zh-cn/markdown-cn.html.markdown @@ -3,7 +3,7 @@ language: Markdown contributors: - ["Dan Turkel", "http://danturkel.com/"] translators: - - ["Fangzhou Chen"] + - ["Fangzhou Chen","https://github.com/FZSS"] filename: learnmarkdown-cn.md lang: zh-cn --- -- cgit v1.2.3 From e97b3d6a0ff0715b18ac1f0195a064759002975d Mon Sep 17 00:00:00 2001 From: kaernyk Date: Fri, 5 Sep 2014 22:06:10 -0400 Subject: Update and rename learntmux.html.markdown to tmux.html.markdown Fixed some spelling, grammar, and punctuation issues Line 48: Added additional items to the list of keybinds for power users Line 104: Created section on configuring ~/.tmux.conf & added additional comments Line 203: Added section including external resources --- learntmux.html.markdown | 71 ---------------- tmux.html.markdown | 209 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+), 71 deletions(-) delete mode 100644 learntmux.html.markdown create mode 100644 tmux.html.markdown diff --git a/learntmux.html.markdown b/learntmux.html.markdown deleted file mode 100644 index eaf3fd25..00000000 --- a/learntmux.html.markdown +++ /dev/null @@ -1,71 +0,0 @@ ---- -category: tool -tool: tmux -contributors: - - ["kaernyk", "http://github.com/kaernyk"] -filename: LearnTmux.txt ---- - - - tmux is a terminal multiplexer: it enables a number of terminals to be -created, accessed, and controlled from a single screen. tmux may be detached -from a screen and continue running in the background, then later reattached. - - Once you feel comfortable manipulating tmux to suit your needs, I strongly -suggest you read the man pages. - - - -``` -# Session Management - - tmux new Create new session - -s "Session" Create named session - -n "Window" Create named Window - -c "/dir" Start in target directory - - C^b $ Rename current session - C^b d Detach current session - C^b D Select session to detach - - tmux attach Attach last/available session - -t "#" Attach target session - -d Detach the session from other instances - - tmux ls List open sessions - C^b s Select new session for attached client interactively - - kill-session Kill current session - -t "#" Kill target session - -a Kill all sessions - -a -t "#" Kill all sessions but the target - - -# Window Management - - C^b c Create another window - C^b " Split Horizontally - C^b % Split Vertically - C^b M-(1-5) 1) Tile vertically - 2) Tile horizontally - 3) Tile Vertically /w large horizontal - 4) Tile horizontally /w large vertical - 5) Tile all windows evenly - - C^b q Briefly display pane indexes - C^# Choose current window by # - C^b w Choose current window interactively - C^b n Change to next window - C^b p Change to previous window - C^b Up, Right Change to pane in selected direction - Down, left - C^b { Swap current/previous window - C^b } Swap current/next window - - C^b C-Up, Right Resize in steps of one cell - Down, left - C^b M-Up, Right resize in steps of five cells - Down, left - - exit or C^b x Kill the current window -``` diff --git a/tmux.html.markdown b/tmux.html.markdown new file mode 100644 index 00000000..53606bbe --- /dev/null +++ b/tmux.html.markdown @@ -0,0 +1,209 @@ +--- +category: tool +tool: tmux +contributors: + - ["kaernyk", "http://github.com/kaernyk"] +filename: LearnTmux.txt +--- + + + +tmux is a terminal multiplexer: it enables a number of terminals to be +created, accessed, and controlled from a single screen. tmux may be detached +from a screen and continue running in the background, then later reattached. + +```bash +# We’ll start off with managing tmux from a terminal: + + tmux | tmux new # Create an unamed session + -s "Session" # Create named session + -n "Window" # Create named Window + -c "/dir" # Start in target directory + + tmux attach # Attach last/available session + -t "#" # Attach target session + -d # Detach the session from other instances + + tmux ls # List open sessions + + kill-session # Kill current session + -t "#" # Kill target session + -a # Kill all sessions + -a -t "#" # Kill all sessions but the target + + + +## Key Bindings + +# The method of controlling an attached tmux session is fairly simple, but +# there are two keys we will see repeated throughout this write-up (as well as +# the man pages). + + (C-b) == Ctrl + b + +# This combination is the default prefix key that is to be used in conjunction +# with a keybind to produce the stated effect. + + +# The default keybinds are as follows: + + C-b # Send the prefix key (C-b) through to the application. + C-o # Rotate the panes in the current window forwards. + C-z # Suspend the tmux client. + ? # List all key bindings. + : # Enter the tmux command prompt. + r # Force redraw of the attached client. + + c # Create a new window. + $ # Rename the current session. + , # Rename the current window. + + ! # Break the current pane out of the window. + % # Split the current pane into two, left and right. + " # Split the current pane into two, top and bottom. + + n # Change to the next window. + p # Change to the previous window. + { # Swap the current pane with the previous pane. + } # Swap the current pane with the next pane. + + s # Select a new session for the attached client interactively. + w # Choose the current window interactively. + 0 to 9 # Select windows 0 to 9. + + d # Detach the current client. + D # Choose a client to detach. + + & # Kill the current window. + x # Kill the current pane. + + Up, Down # Change to the pane above, below, left, or right. + Left, Right + + M-1 to M-5 # Arrange panes: + # 1) even-horizontal + # 2) even-vertical + # 3) main-horizontal + # 4) main-vertical + # 5) tiled. + + C-Up, C-Down # Resize the current pane in steps of one cell. + C-Left, C-Right + + M-Up, M-Down # Resize the current pane in steps of five cells. + M-Left, M-Right +``` + + +### Configuring ~/.tmux.conf + + tmux.conf can be used to set options automatically on start up, much +like how .vimrc or init.el are used. + +```bash +# Example tmux.conf +# 2014.9 + +### Keybinds +########################################################################### + +## Prefix Adjustment + +# Unbind C-b as the default prefix +unbind-key C-b + +# Set ` as the default prefix +set-option -g prefix ` +#set option -g prefix C-a + +# Return to previous window when prefix is pressed twice +bind-key C-a last-window +bind-key ` last-window +bind-key a send-prefix + +# Allow swapping C-a and ` using F11/F12 +bind-key F11 set-option -g prefix C-a +bind-key F12 set-option -g prefix ` + +## Index Start +set -g base-index 1 + +## Window Cycle/Swap +bind e previous-window +bind f next-window +bind E swap-window -t -1 +bind F swap-window -t +1 + + +### Theme +########################################################################### + +## Statusbar Color Palatte +#set-option -g status-justify centre +set-option -g status-justify left +set-option -g status-bg black +set-option -g status-fg white +set-option -g status-left-length 40 +set-option -g status-right-length 80 + +## Pane Border Color Palette +set-option -g pane-active-border-fg green +set-option -g pane-active-border-bg black +set-option -g pane-border-fg white +set-option -g pane-border-bg black + +## Message Color Palette +set-option -g message-fg black +set-option -g message-bg green + +#setw -g mode-bg black + +## Window Status Color Palette +setw -g window-status-bg black +setw -g window-status-current-fg green +setw -g window-status-bell-attr default +setw -g window-status-bell-fg red +setw -g window-status-content-attr default +setw -g window-status-content-fg yellow +setw -g window-status-activity-attr default +setw -g window-status-activity-fg yellow + + +### UI +########################################################################### + +## Window Interface Adjustments +set-option -g status-utf8 on +setw -g mode-keys vi +setw -g mode-mouse on +setw -g monitor-activity on + +set-option -g mouse-select-pane on +set-option -g status-keys vi +set-option -g bell-action any +set-option -g set-titles on +set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) +set-option -g visual-bell off + +## Statusbar Adjustments +set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' +set -g status-interval 5 +#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' + +## Show performance counters in statusbar +#set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' + + +### Misc +########################################################################### +set -g history-limit 4096 +bind r source-file ~/.tmux.conf +``` + +### External Resources + +Tmux | Home
+Manual page
+Display CPU/MEM % in statusbar
+Archlinux Wiki
+Gentoo Wiki
-- cgit v1.2.3 From 29581e5c1307e43f752e08b6fdd66cde54448424 Mon Sep 17 00:00:00 2001 From: kaernyk Date: Fri, 5 Sep 2014 23:11:58 -0400 Subject: Update tmux.html.markdown Corrected page structure/style to conform to other tutorials ``` Line 18: Removed "tmux" as it has other purposes as well as quickly starting a blank session that will not be covered Line 45: Added Meta/Alt explanation Line 126: Moved 'bind-key C-a send-prefix" to the group that requires it Line 184: Added comment Line 186: Added comment pointing to resource required (if enabled) - Removed 'set-option -g status-justify centre' ``` --- tmux.html.markdown | 200 ++++++++++++++++++++++++++--------------------------- 1 file changed, 97 insertions(+), 103 deletions(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index 53606bbe..7127d3b9 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -12,10 +12,11 @@ tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached. + ```bash # We’ll start off with managing tmux from a terminal: - tmux | tmux new # Create an unamed session + tmux new # Create a new session -s "Session" # Create named session -n "Window" # Create named Window -c "/dir" # Start in target directory @@ -40,9 +41,10 @@ from a screen and continue running in the background, then later reattached. # the man pages). (C-b) == Ctrl + b - -# This combination is the default prefix key that is to be used in conjunction -# with a keybind to produce the stated effect. + + (M-1) == Alt + b + -or- + Meta + b # The default keybinds are as follows: @@ -92,7 +94,6 @@ from a screen and continue running in the background, then later reattached. M-Up, M-Down # Resize the current pane in steps of five cells. M-Left, M-Right -``` ### Configuring ~/.tmux.conf @@ -100,104 +101,97 @@ from a screen and continue running in the background, then later reattached. tmux.conf can be used to set options automatically on start up, much like how .vimrc or init.el are used. -```bash -# Example tmux.conf -# 2014.9 - -### Keybinds -########################################################################### - -## Prefix Adjustment - -# Unbind C-b as the default prefix -unbind-key C-b - -# Set ` as the default prefix -set-option -g prefix ` -#set option -g prefix C-a - -# Return to previous window when prefix is pressed twice -bind-key C-a last-window -bind-key ` last-window -bind-key a send-prefix - -# Allow swapping C-a and ` using F11/F12 -bind-key F11 set-option -g prefix C-a -bind-key F12 set-option -g prefix ` - -## Index Start -set -g base-index 1 - -## Window Cycle/Swap -bind e previous-window -bind f next-window -bind E swap-window -t -1 -bind F swap-window -t +1 - - -### Theme -########################################################################### - -## Statusbar Color Palatte -#set-option -g status-justify centre -set-option -g status-justify left -set-option -g status-bg black -set-option -g status-fg white -set-option -g status-left-length 40 -set-option -g status-right-length 80 - -## Pane Border Color Palette -set-option -g pane-active-border-fg green -set-option -g pane-active-border-bg black -set-option -g pane-border-fg white -set-option -g pane-border-bg black - -## Message Color Palette -set-option -g message-fg black -set-option -g message-bg green - -#setw -g mode-bg black - -## Window Status Color Palette -setw -g window-status-bg black -setw -g window-status-current-fg green -setw -g window-status-bell-attr default -setw -g window-status-bell-fg red -setw -g window-status-content-attr default -setw -g window-status-content-fg yellow -setw -g window-status-activity-attr default -setw -g window-status-activity-fg yellow - - -### UI -########################################################################### - -## Window Interface Adjustments -set-option -g status-utf8 on -setw -g mode-keys vi -setw -g mode-mouse on -setw -g monitor-activity on - -set-option -g mouse-select-pane on -set-option -g status-keys vi -set-option -g bell-action any -set-option -g set-titles on -set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) -set-option -g visual-bell off - -## Statusbar Adjustments -set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' -set -g status-interval 5 -#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' - -## Show performance counters in statusbar -#set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' - - -### Misc -########################################################################### -set -g history-limit 4096 -bind r source-file ~/.tmux.conf +``` + # Example tmux.conf + # 2014.9 + + ## Prefix Adjustment + + # Unbind C-b as the default prefix + unbind-key C-b + + # Set ` as the default prefix + set-option -g prefix ` + + # Set C-a as the default prefix + #set option -g prefix C-a + + # Return to previous window when prefix is pressed twice + bind-key C-a last-window + bind-key ` last-window + + # Allow swapping C-a and ` using F11/F12 + bind-key F11 set-option -g prefix C-a + bind-key F12 set-option -g prefix ` + bind-key C-a send-prefix + + ## Index Start + set -g base-index 1 + + ## Window Cycle/Swap + bind e previous-window + bind f next-window + bind E swap-window -t -1 + bind F swap-window -t +1 + + ## Statusbar Color Palatte + set-option -g status-justify left + set-option -g status-bg black + set-option -g status-fg white + set-option -g status-left-length 40 + set-option -g status-right-length 80 + + ## Pane Border Color Palette + set-option -g pane-active-border-fg green + set-option -g pane-active-border-bg black + set-option -g pane-border-fg white + set-option -g pane-border-bg black + + ## Message Color Palette + set-option -g message-fg black + set-option -g message-bg green + + #setw -g mode-bg black + + ## Window Status Color Palette + setw -g window-status-bg black + setw -g window-status-current-fg green + setw -g window-status-bell-attr default + setw -g window-status-bell-fg red + setw -g window-status-content-attr default + setw -g window-status-content-fg yellow + setw -g window-status-activity-attr default + setw -g window-status-activity-fg yellow + + ## Window Interface Adjustments + set-option -g status-utf8 on + setw -g mode-keys vi + setw -g mode-mouse on + setw -g monitor-activity on + + set-option -g mouse-select-pane on + set-option -g status-keys vi + set-option -g bell-action any + set-option -g set-titles on + set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) + set-option -g visual-bell off + + ## Statusbar Adjustments + set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' + set -g status-interval 5 + + # Statusbar with right-aligned Date / Time + set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' + + ## Show performance counters in statusbar + # Requires https://github.com/thewtex/tmux-mem-cpu-load/ + #set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' + + ## Scrollback/History limit + set -g history-limit 4096 + + bind r source-file ~/.tmux.conf +``` ``` ### External Resources -- cgit v1.2.3 From 408a4689f240e15d2b7e7517949d7c5682da6e8a Mon Sep 17 00:00:00 2001 From: kaernyk Date: Fri, 5 Sep 2014 23:20:51 -0400 Subject: fix code block example tmux.conf was not nested as I previously thought. I combined the blocks and fixed the issue --- tmux.html.markdown | 183 +++++++++++++++++++++++++++-------------------------- 1 file changed, 92 insertions(+), 91 deletions(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index 7127d3b9..b9257edc 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -96,102 +96,103 @@ from a screen and continue running in the background, then later reattached. M-Left, M-Right + + ### Configuring ~/.tmux.conf tmux.conf can be used to set options automatically on start up, much like how .vimrc or init.el are used. -``` - # Example tmux.conf - # 2014.9 - - ## Prefix Adjustment - - # Unbind C-b as the default prefix - unbind-key C-b - - # Set ` as the default prefix - set-option -g prefix ` - - # Set C-a as the default prefix - #set option -g prefix C-a - - # Return to previous window when prefix is pressed twice - bind-key C-a last-window - bind-key ` last-window - - # Allow swapping C-a and ` using F11/F12 - bind-key F11 set-option -g prefix C-a - bind-key F12 set-option -g prefix ` - bind-key C-a send-prefix - - ## Index Start - set -g base-index 1 - - ## Window Cycle/Swap - bind e previous-window - bind f next-window - bind E swap-window -t -1 - bind F swap-window -t +1 - - ## Statusbar Color Palatte - set-option -g status-justify left - set-option -g status-bg black - set-option -g status-fg white - set-option -g status-left-length 40 - set-option -g status-right-length 80 - - ## Pane Border Color Palette - set-option -g pane-active-border-fg green - set-option -g pane-active-border-bg black - set-option -g pane-border-fg white - set-option -g pane-border-bg black - - ## Message Color Palette - set-option -g message-fg black - set-option -g message-bg green - - #setw -g mode-bg black - - ## Window Status Color Palette - setw -g window-status-bg black - setw -g window-status-current-fg green - setw -g window-status-bell-attr default - setw -g window-status-bell-fg red - setw -g window-status-content-attr default - setw -g window-status-content-fg yellow - setw -g window-status-activity-attr default - setw -g window-status-activity-fg yellow - - ## Window Interface Adjustments - set-option -g status-utf8 on - setw -g mode-keys vi - setw -g mode-mouse on - setw -g monitor-activity on - - set-option -g mouse-select-pane on - set-option -g status-keys vi - set-option -g bell-action any - set-option -g set-titles on - set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) - set-option -g visual-bell off - - ## Statusbar Adjustments - set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' - set -g status-interval 5 - - # Statusbar with right-aligned Date / Time - set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' - - ## Show performance counters in statusbar - # Requires https://github.com/thewtex/tmux-mem-cpu-load/ - #set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' - - ## Scrollback/History limit - set -g history-limit 4096 - - bind r source-file ~/.tmux.conf -``` + +# Example tmux.conf +# 2014.9 + +## Prefix Adjustment + +# Unbind C-b as the default prefix +unbind-key C-b + +# Set ` as the default prefix +set-option -g prefix ` + +# Set C-a as the default prefix +#set option -g prefix C-a + +# Return to previous window when prefix is pressed twice +bind-key C-a last-window +bind-key ` last-window + +# Allow swapping C-a and ` using F11/F12 +bind-key F11 set-option -g prefix C-a +bind-key F12 set-option -g prefix ` +bind-key C-a send-prefix + +## Index Start +set -g base-index 1 + +## Window Cycle/Swap +bind e previous-window +bind f next-window +bind E swap-window -t -1 +bind F swap-window -t +1 + +## Statusbar Color Palatte +set-option -g status-justify left +set-option -g status-bg black +set-option -g status-fg white +set-option -g status-left-length 40 +set-option -g status-right-length 80 + +## Pane Border Color Palette +set-option -g pane-active-border-fg green +set-option -g pane-active-border-bg black +set-option -g pane-border-fg white +set-option -g pane-border-bg black + +## Message Color Palette +set-option -g message-fg black +set-option -g message-bg green + +#setw -g mode-bg black + +## Window Status Color Palette +setw -g window-status-bg black +setw -g window-status-current-fg green +setw -g window-status-bell-attr default +setw -g window-status-bell-fg red +setw -g window-status-content-attr default +setw -g window-status-content-fg yellow +setw -g window-status-activity-attr default +setw -g window-status-activity-fg yellow + +## Window Interface Adjustments +set-option -g status-utf8 on +setw -g mode-keys vi +setw -g mode-mouse on +setw -g monitor-activity on + +set-option -g mouse-select-pane on +set-option -g status-keys vi +set-option -g bell-action any +set-option -g set-titles on +set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) +set-option -g visual-bell off + +## Statusbar Adjustments +set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' +set -g status-interval 5 + +# Statusbar with right-aligned Date / Time +set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' + +## Show performance counters in statusbar +# Requires https://github.com/thewtex/tmux-mem-cpu-load/ +#set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' + +## Scrollback/History limit +set -g history-limit 4096 + +bind r source-file ~/.tmux.conf ``` ### External Resources -- cgit v1.2.3 From 9a909d6a4a1083a97b54c50de2fb390e76f879c0 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 7 Sep 2014 02:39:31 -0400 Subject: Added Boolean operators and lowered line lengths Added Bool operators and and or, and lowered some line lengths. Also shifted some notes above the code lines to keep with same style and help readability. Also added a note on list comprehension. Also added adding to dictionary. --- python3.html.markdown | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/python3.html.markdown b/python3.html.markdown index f6babaff..c3e2ede5 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -61,6 +61,17 @@ False not True # => False not False # => True +# Boolean Operators +# Note "and" and "or" are case-sensitive +True and False #=> False +False or True #=> True + +# Note with ints +0 and 2 #=> 0 +-5 or 0 #=> -5 +0 == False #=> True +2 == True #=> False #only 1 == True #=> True + # Equality is == 1 == 1 # => True 2 == 1 # => False @@ -127,7 +138,8 @@ bool({}) #=> False # Python has a print function print("I'm Python. Nice to meet you!") -# No need to declare variables before assigning to them. Convention is to use lower_case_with_underscores +# No need to declare variables before assigning to them. +# Convention is to use lower_case_with_underscores some_var = 5 some_var # => 5 @@ -176,7 +188,8 @@ li[::-1] # => [3, 4, 2, 1] del li[2] # li is now [1, 2, 3] # You can add lists -li + other_li # => [1, 2, 3, 4, 5, 6] - Note: values for li and for other_li are not modified. +# Note: values for li and for other_li are not modified. +li + other_li # => [1, 2, 3, 4, 5, 6] # Concatenate lists with "extend()" li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] @@ -215,14 +228,17 @@ filled_dict = {"one": 1, "two": 2, "three": 3} # Look up values with [] filled_dict["one"] # => 1 -# Get all keys as a list with "keys()". We need to wrap the call in list() because we are getting back an iterable. We'll talk about those later. -list(filled_dict.keys()) # => ["three", "two", "one"] +# Get all keys as a list with "keys()". +# We need to wrap the call in list() because we are getting back an iterable. We'll talk about those later. # Note - Dictionary key ordering is not guaranteed. # Your results might not match this exactly. +list(filled_dict.keys()) # => ["three", "two", "one"] + # Get all values as a list with "values()". Once again we need to wrap it in list() to get it out of the iterable. -list(filled_dict.values()) # => [3, 2, 1] # Note - Same as above regarding key ordering. +list(filled_dict.values()) # => [3, 2, 1] + # Check for existence of keys in a dictionary with "in" "one" in filled_dict # => True @@ -242,6 +258,10 @@ filled_dict.get("four", 4) # => 4 filled_dict.setdefault("five", 5) # filled_dict["five"] is set to 5 filled_dict.setdefault("five", 6) # filled_dict["five"] is still 5 +# Adding to a dictionary +filled_dict.update({"four":4}) #=> {"one": 1, "two": 2, "three": 3, "four": 4} +#filled_dict["four"] = 4 #another way to add to dict + # Remove keys from a dictionary with del del filled_dict["one"] # Removes the key "one" from filled dict @@ -458,6 +478,7 @@ map(add_10, [1, 2, 3]) # => [11, 12, 13] filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] # We can use list comprehensions for nice maps and filters +# List comprehension stores the output as a list which can itself be a nested list [add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7] -- cgit v1.2.3 From 62f4c0de22452177d96ba24ce0cf8cce119af47b Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 7 Sep 2014 19:22:01 -0400 Subject: [python/en] Added Bool operators Bool operators --- python.html.markdown | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/python.html.markdown b/python.html.markdown index 9057dde2..6b2a40de 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -57,9 +57,17 @@ to Python 2.x. Look for another tour of Python 3 soon! # Enforce precedence with parentheses (1 + 3) * 2 # => 8 -# Boolean values are primitives -True -False +# Boolean Operators ++# Note "and" and "or" are case-sensitive ++True and False #=> False ++False or True #=> True ++ ++# Note with ints ++0 and 2 #=> 0 ++-5 or 0 #=> -5 ++0 == False #=> True ++2 == True #=> False +1 == True #=> True # negate with not not True # => False -- cgit v1.2.3 From 820eebcd3362841721a981f91695b64af8f50b94 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 7 Sep 2014 19:23:07 -0400 Subject: [python3/en] Bool operators Bool operators --- python3.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python3.html.markdown b/python3.html.markdown index c3e2ede5..a94f4eae 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -66,11 +66,12 @@ not False # => True True and False #=> False False or True #=> True -# Note with ints +# Note using Bool operators with ints 0 and 2 #=> 0 -5 or 0 #=> -5 0 == False #=> True -2 == True #=> False #only 1 == True #=> True +2 == True #=> False +1 == True #=> True # Equality is == 1 == 1 # => True -- cgit v1.2.3 From 81b4f49e999a6ec41bd1f79d77208d2db0a29871 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 7 Sep 2014 19:32:46 -0400 Subject: [python/en] Added Bool operators Modified Bool operators --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index 6b2a40de..390c7b76 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -62,7 +62,7 @@ to Python 2.x. Look for another tour of Python 3 soon! +True and False #=> False +False or True #=> True + -+# Note with ints ++# Note using Bool operators with ints +0 and 2 #=> 0 +-5 or 0 #=> -5 +0 == False #=> True -- cgit v1.2.3 From 0f00fb2b7ae0118bd52bfc675f0e8561c083d241 Mon Sep 17 00:00:00 2001 From: kaernyk Date: Mon, 8 Sep 2014 09:02:45 -0400 Subject: Edit tmux.conf - Added additional content to terminal management - Sectioned each category, and restructured how they were grouped - Removed 'set -g mode-bg' as users can read the manual page if this becomes something they want to customize --- tmux.html.markdown | 154 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 103 insertions(+), 51 deletions(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index b9257edc..2850a64b 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -16,16 +16,33 @@ from a screen and continue running in the background, then later reattached. ```bash # We’ll start off with managing tmux from a terminal: - tmux new # Create a new session + tmux + + new # Create a new session -s "Session" # Create named session -n "Window" # Create named Window -c "/dir" # Start in target directory - tmux attach # Attach last/available session + attach # Attach last/available session -t "#" # Attach target session -d # Detach the session from other instances - tmux ls # List open sessions + ls # List open sessions + -a # List all open sessions + + lsw # List windows + -a # List all windows + -s # List all windows in session + + lsp # List panes + -a # List all panes + -s # List all panes in session + -t # List app panes in target + + kill-window # Kill current window + -t "#" # Kill target window + -a # Kill all windows + -a -t "#" # Kill all windows but the target kill-session # Kill current session -t "#" # Kill target session @@ -104,58 +121,75 @@ from a screen and continue running in the background, then later reattached. like how .vimrc or init.el are used. -# Example tmux.conf -# 2014.9 - -## Prefix Adjustment - -# Unbind C-b as the default prefix -unbind-key C-b - -# Set ` as the default prefix -set-option -g prefix ` - -# Set C-a as the default prefix -#set option -g prefix C-a - -# Return to previous window when prefix is pressed twice +# Example tmux.conf +# 2014.9 + + +### Keybinds +########################################################################### + +# Unbind C-b as the default prefix +unbind-key C-b + +# Set new default prefix +set-option -g prefix ` +#set-option -g prefix C-a + +# Return to previous window when prefix is pressed twice bind-key C-a last-window -bind-key ` last-window - -# Allow swapping C-a and ` using F11/F12 +bind-key ` last-window + +# Allow swapping C-a and ` using F11/F12 bind-key F11 set-option -g prefix C-a bind-key F12 set-option -g prefix ` -bind-key C-a send-prefix -## Index Start +# Activate inner-most session (when nesting tmux) +# to send commands +bind-key a send-prefix + +# Index Start set -g base-index 1 -## Window Cycle/Swap +# Window Cycle/Swap bind e previous-window bind f next-window bind E swap-window -t -1 bind F swap-window -t +1 - -## Statusbar Color Palatte + +# easy-to-remember split pane commands +bind | split-window -h +bind - split-window -v +unbind '"' +unbind % + +# moving between panes with vim movement keys +bind h select-pane -L +bind j select-pane -D +bind k select-pane -U +bind l select-pane -R + + +### Theme +########################################################################### + +# Statusbar Color Palatte set-option -g status-justify left set-option -g status-bg black set-option -g status-fg white set-option -g status-left-length 40 set-option -g status-right-length 80 -## Pane Border Color Palette +# Pane Border Color Palette set-option -g pane-active-border-fg green set-option -g pane-active-border-bg black set-option -g pane-border-fg white set-option -g pane-border-bg black -## Message Color Palette +# Message Color Palette set-option -g message-fg black set-option -g message-bg green -#setw -g mode-bg black - -## Window Status Color Palette +# Window Status Color Palette setw -g window-status-bg black setw -g window-status-current-fg green setw -g window-status-bell-attr default @@ -164,34 +198,52 @@ setw -g window-status-content-attr default setw -g window-status-content-fg yellow setw -g window-status-activity-attr default setw -g window-status-activity-fg yellow - -## Window Interface Adjustments + + +### UI +########################################################################### + +# Statusbar set-option -g status-utf8 on + +# Keybind preference setw -g mode-keys vi -setw -g mode-mouse on +set-option -g status-keys vi + +# Notification setw -g monitor-activity on +set -g visual-activity on +set-option -g bell-action any +set-option -g visual-bell off +# Mouse +setw -g mode-mouse on set-option -g mouse-select-pane on -set-option -g status-keys vi -set-option -g bell-action any +set -g mouse-resize-pane on +set -g mouse-select-window on + +# Automatically set window titles set-option -g set-titles on set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) -set-option -g visual-bell off - -## Statusbar Adjustments -set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' -set -g status-interval 5 - -# Statusbar with right-aligned Date / Time -set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' - -## Show performance counters in statusbar -# Requires https://github.com/thewtex/tmux-mem-cpu-load/ + +# Statusbar Adjustments +set -g status-left '#[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' +set -g status-interval 3 + +# Statusbar with right-aligned Date / Time +#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' + +# Show performance counters in statusbar +# Requires https://github.com/thewtex/tmux-mem-cpu-load/ #set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' - -## Scrollback/History limit -set -g history-limit 4096 - + + +### Misc +########################################################################### + +# Scrollback/History limit +set -g history-limit 4096 + bind r source-file ~/.tmux.conf ``` -- cgit v1.2.3 From e8e9f08c540123eec124afd3a2666b52a48d0a4a Mon Sep 17 00:00:00 2001 From: kaernyk Date: Mon, 8 Sep 2014 09:12:30 -0400 Subject: Fix terminal commands Header syntax has been corrected --- tmux.html.markdown | 68 +++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index 2850a64b..ae68ed75 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -14,40 +14,40 @@ from a screen and continue running in the background, then later reattached. ```bash -# We’ll start off with managing tmux from a terminal: - - tmux - - new # Create a new session - -s "Session" # Create named session - -n "Window" # Create named Window - -c "/dir" # Start in target directory - - attach # Attach last/available session - -t "#" # Attach target session - -d # Detach the session from other instances - - ls # List open sessions - -a # List all open sessions - - lsw # List windows - -a # List all windows - -s # List all windows in session - - lsp # List panes - -a # List all panes - -s # List all panes in session - -t # List app panes in target - - kill-window # Kill current window - -t "#" # Kill target window - -a # Kill all windows - -a -t "#" # Kill all windows but the target - - kill-session # Kill current session - -t "#" # Kill target session - -a # Kill all sessions - -a -t "#" # Kill all sessions but the target + + tmux [command] # Run a command + # 'tmux' with no commands will create a new session + + new # Create a new session + -s "Session" # Create named session + -n "Window" # Create named Window + -c "/dir" # Start in target directory + + attach # Attach last/available session + -t "#" # Attach target session + -d # Detach the session from other instances + + ls # List open sessions + -a # List all open sessions + + lsw # List windows + -a # List all windows + -s # List all windows in session + + lsp # List panes + -a # List all panes + -s # List all panes in session + -t # List app panes in target + + kill-window # Kill current window + -t "#" # Kill target window + -a # Kill all windows + -a -t "#" # Kill all windows but the target + + kill-session # Kill current session + -t "#" # Kill target session + -a # Kill all sessions + -a -t "#" # Kill all sessions but the target -- cgit v1.2.3 From eff9acfdde8a40bcdd6c6bcec96381064c1060b4 Mon Sep 17 00:00:00 2001 From: kaernyk Date: Mon, 8 Sep 2014 09:21:59 -0400 Subject: Remove syntax highlighting bash syntax highlighting created issues with an open " originating in Keybinds. I have disabled syntax highlighting so the display is consistent --- tmux.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index ae68ed75..b4a9fde9 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -13,7 +13,7 @@ created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached. -```bash +``` tmux [command] # Run a command # 'tmux' with no commands will create a new session -- cgit v1.2.3 From db5d1eaf136d2a61b905520fe668eeee2c7a2ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Polykanine=20A=2EK=2EA=2E=20Menelion=20Elens=C3=BA?= =?UTF-8?q?l=C3=AB?= Date: Tue, 9 Sep 2014 13:56:35 +0300 Subject: [python-ru] Updating Russian translation of the Python 2.x tutorial --- ru-ru/python-ru.html.markdown | 266 +++++++++++++++++++++++++++++------------- 1 file changed, 186 insertions(+), 80 deletions(-) diff --git a/ru-ru/python-ru.html.markdown b/ru-ru/python-ru.html.markdown index 204eb357..d59d3e21 100644 --- a/ru-ru/python-ru.html.markdown +++ b/ru-ru/python-ru.html.markdown @@ -5,25 +5,29 @@ contributors: - ["Louie Dinh", "http://ldinh.ca"] translators: - ["Yury Timofeev", "http://twitter.com/gagar1n"] + - ["Andre Polykanine", "https://github.com/Oire"] filename: learnpython-ru.py --- -Язык Python был создан Гвидо ван Россумом в начале 90-х. Сейчас это один из самых популярных -языков. Я люблю его за его понятный и доходчивый синтаксис - это почти что исполняемый псевдокод. +Язык Python был создан Гвидо ван Россумом в начале 90-х. Сейчас это один из +самых популярных языков. Я люблю его за понятный и доходчивый синтаксис — это +почти что исполняемый псевдокод. -С благодарностью жду ваших отзывов: [@louiedinh](http://twitter.com/louiedinh) или louiedinh [at] [google's email service] +С благодарностью жду ваших отзывов: [@louiedinh](http://twitter.com/louiedinh) +или louiedinh [at] [почтовый сервис Google] -Замечание: Эта статья относится к Python 2.7, но должно работать и в Python 2.x. Скоро будет версия и для Python 3! +Замечание: Эта статья относится к Python 2.7, но должно работать и в Python 2.x. +Скоро будет версия и для Python 3! ```python -# Однострочные комментарии начинаются с hash-символа. +# Однострочные комментарии начинаются с символа решётки. """ Многострочный текст может быть записан, используя 3 знака " и обычно используется в качестве комментария """ #################################################### -## 1. Примитивные типы данных и операторов +## 1. Примитивные типы данных и операторы #################################################### # У вас есть числа @@ -36,17 +40,31 @@ filename: learnpython-ru.py 35 / 5 #=> 7 # А вот деление немного сложнее. В этом случае происходит деление -# целых чисел и результат автоматически округляется в меньшую сторону. +# целых чисел, и результат автоматически округляется в меньшую сторону. 5 / 2 #=> 2 -# Чтобы научиться делить, сначала нужно немного узнать о дробных числах. -2.0 # Это дробное число +# Чтобы научиться делить, сначала нужно немного узнать о числах +# с плавающей запятой. +2.0 # Это число с плавающей запятой 11.0 / 4.0 #=> 2.75 Вооот... Так гораздо лучше +# Результат целочисленного деления округляется в меньшую сторону +# как для положительных, так и для отрицательных чисел. +5 // 3 # => 1 +5.0 // 3.0 # => 1.0 # работает и для чисел с плавающей запятой +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 + +# Остаток от деления +7 % 3 # => 1 + +# Возведение в степень +2 ** 4 # => 16 + # Приоритет операций указывается скобками (1 + 3) * 2 #=> 8 -# Логические значения являются примитивами +# Логические (булевы) значения являются примитивами True False @@ -54,15 +72,15 @@ False not True #=> False not False #=> True -# Равенство это == +# Равенство — это == 1 == 1 #=> True 2 == 1 #=> False -# Неравенство это != +# Неравенство — это != 1 != 1 #=> False 2 != 1 #=> True -# Еще немного сравнений +# Ещё немного сравнений 1 < 10 #=> True 1 > 10 #=> False 2 <= 2 #=> True @@ -85,9 +103,10 @@ not False #=> True # Символ % используется для форматирования строк, например: "%s могут быть %s" % ("строки", "интерполированы") -# Новый метод форматирования строк - использование метода format. +# Новый способ форматирования строк — использование метода format. # Это предпочитаемый способ. "{0} могут быть {1}".format("строки", "форматированы") + # Если вы не хотите считать, можете использовать ключевые слова. "{name} хочет есть {food}".format(name="Боб", food="лазанью") @@ -95,7 +114,7 @@ not False #=> True None #=> None # Не используйте оператор равенства '=='' для сравнения -# объектов с None. Используйте для этого 'is' +# объектов с None. Используйте для этого «is» "etc" is None #=> False None is None #=> True @@ -113,17 +132,18 @@ None is None #=> True ## 2. Переменные и коллекции #################################################### -# Печатать довольно просто -print "Я Python. Приятно познакомиться!" - +# У Python есть функция Print, доступная в версиях 2.7 и 3, +print("Я Python. Приятно познакомиться!") +# ...и старый оператор print, доступный в версиях 2.x, но удалённый в версии 3. +print "И я тоже Python!" # Необязательно объявлять переменные перед их инициализацией. -some_var = 5 # По соглашению используется нижний_регистр_с_подчеркиваниями +some_var = 5 # По соглашению используется нижний_регистр_с_подчёркиваниями some_var #=> 5 -# При попытке доступа к неинициализированной переменной, +# При попытке доступа к неинициализированной переменной # выбрасывается исключение. -# См. раздел "Поток управления" для информации об исключениях. +# См. раздел «Поток управления» для информации об исключениях. some_other_var # Выбрасывает ошибку именования # if может быть использован как выражение @@ -149,24 +169,30 @@ li[0] #=> 1 # Обратимся к последнему элементу li[-1] #=> 3 -# Попытка выйти за границы массива приведет к IndexError -li[4] # Выдает IndexError +# Попытка выйти за границы массива приведёт к ошибке индекса +li[4] # Выдаёт IndexError # Можно обращаться к диапазону, используя "кусочный синтаксис" (slice syntax) -# (Для тех, кто любит математику, это называется замкнуто/открытый интервал.) +# (Для тех, кто любит математику, это называется замкнуто-открытый интервал). li[1:3] #=> [2, 4] # Опускаем начало li[2:] #=> [4, 3] # Опускаем конец li[:3] #=> [1, 2, 4] +# Выбираем каждый второй элемент +li[::2] # =>[1, 4] +# Переворачиваем список +li[::-1] # => [3, 4, 2, 1] +# Используйте сочетания всего вышеназванного для выделения более сложных кусков +# li[начало:конец:шаг] # Удаляем произвольные элементы из списка оператором del del li[2] # [1, 2, 3] # Вы можете складывать списки -li + other_li #=> [1, 2, 3, 4, 5, 6] - Замечание: li и other_li остаются нетронутыми +li + other_li #=> [1, 2, 3, 4, 5, 6] — Замечание: li и other_li не изменяются -# Конкатенировать списки можно методом extend +# Объединять списки можно методом extend li.extend(other_li) # Теперь li содержит [1, 2, 3, 4, 5, 6] # Проверить элемент на вхождение в список можно оператором in @@ -176,12 +202,12 @@ li.extend(other_li) # Теперь li содержит [1, 2, 3, 4, 5, 6] len(li) #=> 6 -# Кортежи - это такие списки, только неизменяемые +# Кортежи — это такие списки, только неизменяемые tup = (1, 2, 3) tup[0] #=> 1 -tup[0] = 3 # Выдает TypeError +tup[0] = 3 # Выдаёт TypeError -# Все то же самое можно делать и с кортежами +# Всё то же самое можно делать и с кортежами len(tup) #=> 3 tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) tup[:2] #=> (1, 2) @@ -203,33 +229,33 @@ filled_dict = {"one": 1, "two": 2, "three": 3} # Значения ищутся по ключу с помощью оператора [] filled_dict["one"] #=> 1 -# Можно получить все ключи в виде списка +# Можно получить все ключи в виде списка с помощью метода keys filled_dict.keys() #=> ["three", "two", "one"] -# Замечание - сохранение порядка ключей в словаре не гарантируется +# Замечание: сохранение порядка ключей в словаре не гарантируется # Ваши результаты могут не совпадать с этими. -# Можно получить и все значения в виде списка +# Можно получить и все значения в виде списка, используйте метод values filled_dict.values() #=> [3, 2, 1] -# То же самое замечание насчет порядка ключей справедливо и здесь +# То же самое замечание насчёт порядка ключей справедливо и здесь # При помощи оператора in можно проверять ключи на вхождение в словарь "one" in filled_dict #=> True 1 in filled_dict #=> False -# Попытка получить значение по несуществующему ключу выбросит KeyError +# Попытка получить значение по несуществующему ключу выбросит ошибку ключа filled_dict["four"] # KeyError # Чтобы избежать этого, используйте метод get filled_dict.get("one") #=> 1 filled_dict.get("four") #=> None -# Метод get также принимает аргумент default, значение которого будет +# Метод get также принимает аргумент по умолчанию, значение которого будет # возвращено при отсутствии указанного ключа filled_dict.get("one", 4) #=> 1 filled_dict.get("four", 4) #=> 4 -# Метод setdefault - это безопасный способ добавить новую пару ключ-значение в словарь +# Метод setdefault вставляет пару ключ-значение, только если такого ключа нет filled_dict.setdefault("five", 5) #filled_dict["five"] возвращает 5 -filled_dict.setdefault("five", 6) #filled_dict["five"] по прежнему возвращает 5 +filled_dict.setdefault("five", 6) #filled_dict["five"] по-прежнему возвращает 5 # Множества содержат... ну, в общем, множества @@ -237,8 +263,8 @@ empty_set = set() # Инициализация множества набором значений some_set = set([1,2,2,3,4]) # some_set теперь равно set([1, 2, 3, 4]) -# Начиная с Python 2.7, вы можете использовать {} чтобы обьявить множество -filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} +# Начиная с Python 2.7, вы можете использовать {}, чтобы объявить множество +filled_set = {1, 2, 2, 3, 4} # => {1, 2, 3, 4} # Добавление новых элементов в множество filled_set.add(5) # filled_set равно {1, 2, 3, 4, 5} @@ -262,33 +288,33 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6} ## 3. Поток управления #################################################### -# Для начала заведем переменную +# Для начала заведём переменную some_var = 5 # Так выглядит выражение if. Отступы в python очень важны! -# результат: "some_var меньше, чем 10" +# результат: «some_var меньше, чем 10» if some_var > 10: - print "some_var намного больше, чем 10." + print("some_var намного больше, чем 10.") elif some_var < 10: # Выражение elif необязательно. - print "some_var меньше, чем 10." + print("some_var меньше, чем 10.") else: # Это тоже необязательно. - print "some_var равно 10." + print("some_var равно 10.") """ Циклы For проходят по спискам Результат: - собака это млекопитающее - кошка это млекопитающее - мышь это млекопитающее + собака — это млекопитающее + кошка — это млекопитающее + мышь — это млекопитающее """ for animal in ["собака", "кошка", "мышь"]: # Можете использовать оператор % для интерполяции форматированных строк - print "%s это млекопитающее" % animal + print("%s — это млекопитающее" % animal) """ -`range(number)` возвращает список чисел +«range(число)» возвращает список чисел от нуля до заданного числа Результат: 0 @@ -297,7 +323,7 @@ for animal in ["собака", "кошка", "мышь"]: 3 """ for i in range(4): - print i + print(i) """ Циклы while продолжаются до тех пор, пока указанное условие не станет ложным. @@ -309,19 +335,24 @@ for i in range(4): """ x = 0 while x < 4: - print x - x += 1 # То же самое, что x = x + 1 + print(x) + x += 1 # Краткая запись для x = x + 1 -# Обрабывайте исключения блоками try/except +# Обрабатывайте исключения блоками try/except # Работает в Python 2.6 и выше: try: - # Для выбора ошибки используется raise - raise IndexError("Это IndexError") + # Чтобы выбросить ошибку, используется raise + raise IndexError("Это ошибка индекса") except IndexError as e: # pass это просто отсутствие оператора. Обычно здесь происходит - # восстановление от ошибки. + # восстановление после ошибки. pass +except (TypeError, NameError): + pass # Несколько исключений можно обработать вместе, если нужно. +else: # Необязательное выражение. Должно следовать за последним блоком except + print("Всё хорошо!") # Выполнится, только если не было никаких исключений + #################################################### @@ -330,23 +361,23 @@ except IndexError as e: # Используйте def для создания новых функций def add(x, y): - print "x равен %s, а y равен %s" % (x, y) + print("x равен %s, а y равен %s" % (x, y)) return x + y # Возвращайте результат выражением return # Вызов функции с аргументами -add(5, 6) #=> prints out "x равен 5, а y равен 6" и возвращает 11 +add(5, 6) #=> выводит «x равен 5, а y равен 6» и возвращает 11 -# Другой способ вызова функции с аргументами +# Другой способ вызова функции — вызов с именованными аргументами add(y=6, x=5) # Именованные аргументы можно указывать в любом порядке. -# Вы можете определить функцию, принимающую неизвестное количество аргументов +# Вы можете определить функцию, принимающую изменяемое число аргументов def varargs(*args): return args varargs(1, 2, 3) #=> (1,2,3) -# А также можете определить функцию, принимающую изменяющееся количество +# А также можете определить функцию, принимающую изменяемое число # именованных аргументов def keyword_args(**kwargs): return kwargs @@ -356,8 +387,8 @@ keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} # Если хотите, можете использовать оба способа одновременно def all_the_args(*args, **kwargs): - print args - print kwargs + print(args) + print(kwargs) """ all_the_args(1, 2, a=3, b=4) выводит: (1, 2) @@ -368,11 +399,28 @@ all_the_args(1, 2, a=3, b=4) выводит: # Используйте символ * для передачи кортежей и ** для передачи словарей args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} -all_the_args(*args) # эквивалент foo(1, 2, 3, 4) -all_the_args(**kwargs) # эквивалент foo(a=3, b=4) -all_the_args(*args, **kwargs) # эквивалент foo(1, 2, 3, 4, a=3, b=4) +all_the_args(*args) # эквивалентно foo(1, 2, 3, 4) +all_the_args(**kwargs) # эквивалентно foo(a=3, b=4) +all_the_args(*args, **kwargs) # эквивалентно foo(1, 2, 3, 4, a=3, b=4) -# Python имеет функции первого класса +# Область определения функций +x = 5 + +def setX(num): + # Локальная переменная x — это не то же самое, что глобальная переменная x + x = num # => 43 + print (x) # => 43 + +def setGlobalX(num): + global x + print (x) # => 5 + x = num # Глобальная переменная x теперь равна 6 + print (x) # => 6 + +setX(43) +setGlobalX(6) + +# В Python есть функции первого класса def create_adder(x): def adder(y): return x + y @@ -388,7 +436,7 @@ add_10(3) #=> 13 map(add_10, [1,2,3]) #=> [11, 12, 13] filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] -# Мы можем использовать списки для удобного отображения и фильтрации +# Для удобного отображения и фильтрации можно использовать списочные включения [add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] @@ -402,7 +450,11 @@ class Human(object): # Атрибут класса. Он разделяется всеми экземплярами этого класса species = "H. sapiens" - # Обычный конструктор + # Обычный конструктор, вызывается при инициализации экземпляра класса + # Обратите внимание, что двойное подчёркивание в начале и в конце имени + # означает объекты и атрибуты, которые используются Python, но находятся + # в пространствах имён, управляемых пользователем. + # Не придумывайте им имена самостоятельно. def __init__(self, name): # Присваивание значения аргумента атрибуту класса name self.name = name @@ -423,17 +475,17 @@ class Human(object): return "*grunt*" -# Инстанцирование класса +# Инициализация экземпляра класса i = Human(name="Иван") -print i.say("привет") # "Иван: привет" +print(i.say("привет")) # Выводит: «Иван: привет» -j = Human("Петр") -print j.say("Привет") # "Петр: привет" +j = Human("Пётр") +print(j.say("Привет")) # Выводит: «Пётр: привет» # Вызов метода класса i.get_species() #=> "H. sapiens" -# Присвоение разделяемому атрибуту +# Изменение разделяемого атрибута Human.species = "H. neanderthalensis" i.get_species() #=> "H. neanderthalensis" j.get_species() #=> "H. neanderthalensis" @@ -448,12 +500,12 @@ Human.grunt() #=> "*grunt*" # Вы можете импортировать модули import math -print math.sqrt(16) #=> 4 +print(math.sqrt(16)) #=> 4 # Вы можете импортировать отдельные функции модуля from math import ceil, floor -print ceil(3.7) #=> 4.0 -print floor(3.7) #=> 3.0 +print(ceil(3.7)) #=> 4.0 +print(floor(3.7)) #=> 3.0 # Можете импортировать все функции модуля. # (Хотя это и не рекомендуется) @@ -463,7 +515,7 @@ from math import * import math as m math.sqrt(16) == m.sqrt(16) #=> True -# Модули в Python это обычные файлы с кодом python. Вы +# Модули в Python — это обычные Python-файлы. Вы # можете писать свои модули и импортировать их. Название # модуля совпадает с названием файла. @@ -472,18 +524,72 @@ math.sqrt(16) == m.sqrt(16) #=> True import math dir(math) +#################################################### +## 7. Дополнительно +#################################################### + +# Генераторы помогут выполнить ленивые вычисления +def double_numbers(iterable): + for i in iterable: + yield i + i + +# Генератор создаёт значения на лету. +# Он не возвращает все значения разом, а создаёт каждое из них при каждой +# итерации. Это значит, что значения больше 15 в double_numbers +# обработаны не будут. +# Обратите внимание: xrange — это генератор, который делает то же, что и range. +# Создание списка чисел от 1 до 900000000 требует много места и времени. +# xrange создаёт объект генератора, а не список сразу, как это делает range. +# Если нам нужно имя переменной, совпадающее с ключевым словом Python, +# мы используем подчёркивание в конце +xrange_ = xrange(1, 900000000) + +# Будет удваивать все числа, пока результат не будет >= 30 +for i in double_numbers(xrange_): + print(i) + if i >= 30: + break + + +# Декораторы +# В этом примере beg оборачивает say +# Метод beg вызовет say. Если say_please равно True, +# он изменит возвращаемое сообщение +from functools import wraps + + +def beg(target_function): + @wraps(target_function) + def wrapper(*args, **kwargs): + msg, say_please = target_function(*args, **kwargs) + if say_please: + return "{} {}".format(msg, " Пожалуйста! У меня нет денег :(") + return msg + + return wrapper + + +@beg +def say(say_please=False): + msg = "Вы не купите мне пива?" + return msg, say_please + + +print(say()) # Вы не купите мне пива? +print(say(say_please=True)) # Вы не купите мне пива? Пожалуйста! У меня нет денег :( ``` -## Хотите еще? +## Хотите ещё? ### Бесплатные онлайн-материалы * [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) * [Dive Into Python](http://www.diveintopython.net/) -* [The Official Docs](http://docs.python.org/2.6/) +* [Официальная документация](http://docs.python.org/2.6/) * [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) * [Python Module of the Week](http://pymotw.com/2/) +* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182) ### Платные -- cgit v1.2.3 From b71c8fb2f69cc009fd89ba17ed222bcd10e3166a Mon Sep 17 00:00:00 2001 From: kaernyk Date: Tue, 9 Sep 2014 19:52:28 -0400 Subject: Remove tmux.conf The linked resources have a few examples for users to build their own, and example configurations can be shared as links. This makes including it not too vital at the moment. - Removed a handful of terminal commands & keybinds from the list - Trimmed some overly-verbose paragraphs --- tmux.html.markdown | 236 +++++++++++------------------------------------------ 1 file changed, 48 insertions(+), 188 deletions(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index b4a9fde9..ed606998 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -8,74 +8,67 @@ filename: LearnTmux.txt -tmux is a terminal multiplexer: it enables a number of terminals to be -created, accessed, and controlled from a single screen. tmux may be detached +tmux is a terminal multiplexer: it enables a number of terminals to be +created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached. ``` + tmux [command] # Run a command + # 'tmux' with no commands will create a new session - tmux [command] # Run a command - # 'tmux' with no commands will create a new session - - new # Create a new session - -s "Session" # Create named session - -n "Window" # Create named Window - -c "/dir" # Start in target directory - - attach # Attach last/available session - -t "#" # Attach target session - -d # Detach the session from other instances - - ls # List open sessions - -a # List all open sessions - - lsw # List windows - -a # List all windows - -s # List all windows in session - - lsp # List panes - -a # List all panes - -s # List all panes in session - -t # List app panes in target - - kill-window # Kill current window - -t "#" # Kill target window - -a # Kill all windows - -a -t "#" # Kill all windows but the target - - kill-session # Kill current session - -t "#" # Kill target session - -a # Kill all sessions - -a -t "#" # Kill all sessions but the target + new # Create a new session + -s "Session" # Create named session + -n "Window" # Create named Window + -c "/dir" # Start in target directory + + attach # Attach last/available session + -t "#" # Attach target session + -d # Detach the session from other instances + + ls # List open sessions + -a # List all open sessions + + lsw # List windows + -a # List all windows + -s # List all windows in session + + lsp # List panes + -a # List all panes + -s # List all panes in session + -t # List app panes in target + + kill-window # Kill current window + -t "#" # Kill target window + -a # Kill all windows + -a -t "#" # Kill all windows but the target + + kill-session # Kill current session + -t "#" # Kill target session + -a # Kill all sessions + -a -t "#" # Kill all sessions but the target ## Key Bindings -# The method of controlling an attached tmux session is fairly simple, but -# there are two keys we will see repeated throughout this write-up (as well as -# the man pages). - - (C-b) == Ctrl + b +# The method of controlling an attached tmux session is via key combinations +# called 'Prefix' keys. - (M-1) == Alt + b - -or- - Meta + b +------------------------------------------------------------------------------ + (C-b) = Ctrl + b # 'Prefix' combination required to use keybinds -# The default keybinds are as follows: + (M-1) = Alt + 1 + -or- + Meta + 1 + +------------------------------------------------------------------------------ - C-b # Send the prefix key (C-b) through to the application. - C-o # Rotate the panes in the current window forwards. - C-z # Suspend the tmux client. ? # List all key bindings. : # Enter the tmux command prompt. r # Force redraw of the attached client. - c # Create a new window. - $ # Rename the current session. - , # Rename the current window. ! # Break the current pane out of the window. % # Split the current pane into two, left and right. @@ -92,14 +85,14 @@ from a screen and continue running in the background, then later reattached. d # Detach the current client. D # Choose a client to detach. - + & # Kill the current window. x # Kill the current pane. Up, Down # Change to the pane above, below, left, or right. Left, Right - M-1 to M-5 # Arrange panes: + M-1 to M-5 # Arrange panes: # 1) even-horizontal # 2) even-vertical # 3) main-horizontal @@ -108,149 +101,16 @@ from a screen and continue running in the background, then later reattached. C-Up, C-Down # Resize the current pane in steps of one cell. C-Left, C-Right - + M-Up, M-Down # Resize the current pane in steps of five cells. M-Left, M-Right - - - -### Configuring ~/.tmux.conf - - tmux.conf can be used to set options automatically on start up, much -like how .vimrc or init.el are used. - - -# Example tmux.conf -# 2014.9 - - -### Keybinds -########################################################################### - -# Unbind C-b as the default prefix -unbind-key C-b - -# Set new default prefix -set-option -g prefix ` -#set-option -g prefix C-a - -# Return to previous window when prefix is pressed twice -bind-key C-a last-window -bind-key ` last-window - -# Allow swapping C-a and ` using F11/F12 -bind-key F11 set-option -g prefix C-a -bind-key F12 set-option -g prefix ` - -# Activate inner-most session (when nesting tmux) -# to send commands -bind-key a send-prefix - -# Index Start -set -g base-index 1 - -# Window Cycle/Swap -bind e previous-window -bind f next-window -bind E swap-window -t -1 -bind F swap-window -t +1 - -# easy-to-remember split pane commands -bind | split-window -h -bind - split-window -v -unbind '"' -unbind % - -# moving between panes with vim movement keys -bind h select-pane -L -bind j select-pane -D -bind k select-pane -U -bind l select-pane -R - - -### Theme -########################################################################### - -# Statusbar Color Palatte -set-option -g status-justify left -set-option -g status-bg black -set-option -g status-fg white -set-option -g status-left-length 40 -set-option -g status-right-length 80 - -# Pane Border Color Palette -set-option -g pane-active-border-fg green -set-option -g pane-active-border-bg black -set-option -g pane-border-fg white -set-option -g pane-border-bg black - -# Message Color Palette -set-option -g message-fg black -set-option -g message-bg green - -# Window Status Color Palette -setw -g window-status-bg black -setw -g window-status-current-fg green -setw -g window-status-bell-attr default -setw -g window-status-bell-fg red -setw -g window-status-content-attr default -setw -g window-status-content-fg yellow -setw -g window-status-activity-attr default -setw -g window-status-activity-fg yellow - - -### UI -########################################################################### - -# Statusbar -set-option -g status-utf8 on - -# Keybind preference -setw -g mode-keys vi -set-option -g status-keys vi - -# Notification -setw -g monitor-activity on -set -g visual-activity on -set-option -g bell-action any -set-option -g visual-bell off - -# Mouse -setw -g mode-mouse on -set-option -g mouse-select-pane on -set -g mouse-resize-pane on -set -g mouse-select-window on - -# Automatically set window titles -set-option -g set-titles on -set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) - -# Statusbar Adjustments -set -g status-left '#[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' -set -g status-interval 3 - -# Statusbar with right-aligned Date / Time -#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' - -# Show performance counters in statusbar -# Requires https://github.com/thewtex/tmux-mem-cpu-load/ -#set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' - - -### Misc -########################################################################### - -# Scrollback/History limit -set -g history-limit 4096 - -bind r source-file ~/.tmux.conf ``` ### External Resources Tmux | Home
-Manual page
-Display CPU/MEM % in statusbar
+Tmux Manual page
Archlinux Wiki
Gentoo Wiki
+Display CPU/MEM % in statusbar
-- cgit v1.2.3 From b492bf6f379d9b8ff9d29872b970f2c0f7e5fa41 Mon Sep 17 00:00:00 2001 From: kaernyk Date: Tue, 9 Sep 2014 21:19:53 -0400 Subject: Add tmux.conf Re-add example ~/.tmux.conf --- tmux.html.markdown | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 3 deletions(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index ed606998..516bee4d 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -59,9 +59,7 @@ from a screen and continue running in the background, then later reattached. (C-b) = Ctrl + b # 'Prefix' combination required to use keybinds - (M-1) = Alt + 1 - -or- - Meta + 1 + (M-1) = Meta + 1 -or- Alt + 1 ------------------------------------------------------------------------------ @@ -105,6 +103,133 @@ from a screen and continue running in the background, then later reattached. M-Up, M-Down # Resize the current pane in steps of five cells. M-Left, M-Right + + +### Configuring ~/.tmux.conf + + tmux.conf can be used to set options automatically on start up, much +like how .vimrc or init.el are used. + +# Example tmux.conf +# 2014.9 + + +### Keybinds +########################################################################### + +# Unbind C-b as the default prefix +unbind-key C-befix C-a + +# Return to previous window when prefix is pressed twice +bind-key C-a last-window +bind-key ` last-window + +# Allow swapping C-a and ` using F11/F12 +bind-key F11 set-option -g prefix C-a +bind-key F12 set-option -g prefix ` + +# Activate inner-most session (when nesting tmux) +# to send commands +bind-key a send-prefix + +# Index Start +set -g base-index 1 + +# Window Cycle/Swap +bind e previous-window +bind f next-window +bind E swap-window -t -1 +bind F swap-window -t +1 + +# easy-to-remember split pane commands +bind | split-window -h +bind - split-window -v +unbind '"' +unbind % + +# moving between panes with vim movement keys +bind h select-pane -L +bind j select-pane -D +bind k select-pane -U +bind l select-pane -R + + +### Theme +########################################################################### + +# Statusbar Color Palette +set-option -g status-justify left +set-option -g status-bg black +set-option -g status-fg white +set-option -g status-left-length 40 +set-option -g status-right-length 80 + +# Pane Border Color Palette +set-option -g pane-active-border-fg green +set-option -g pane-active-border-bg black +set-option -g pane-border-fg white +set-option -g pane-border-bg black + +# Message Color Palette +set-option -g message-fg black +set-option -g message-bg green + +# Window Status Color Palette +setw -g window-status-bg black +setw -g window-status-current-fg green +setw -g window-status-bell-attr default +setw -g window-status-bell-fg red +setw -g window-status-content-attr default +setw -g window-status-content-fg yellow +setw -g window-status-activity-attr default +setw -g window-status-activity-fg yellow + + +### UI +########################################################################### + +# Statusbar +set-option -g status-utf8 on + +# Keybind preference +setw -g mode-keys vi +set-option -g status-keys vi + +# Notification +setw -g monitor-activity on +set -g visual-activity on +set-option -g bell-action any +set-option -g visual-bell off + +# Mouse +setw -g mode-mouse on +set-option -g mouse-select-pane on +set -g mouse-resize-pane on +set -g mouse-select-window on + +# Automatically set window titles +set-option -g set-titles on +set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) + +# Statusbar Adjustments +set -g status-left '#[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' +set -g status-interval 3 + +# Statusbar with right-aligned Date / Time +#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' + +# Show performance counters in statusbar +# Requires https://github.com/thewtex/tmux-mem-cpu-load/ +#set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' + + +### Misc +########################################################################### + +# Scrollback/History limit +set -g history-limit 4096 + +bind r source-file ~/.tmux.conf ``` ### External Resources -- cgit v1.2.3 From f45dbf6a81faf3d44cc9120915cb8a49794154bc Mon Sep 17 00:00:00 2001 From: kaernyk Date: Tue, 9 Sep 2014 21:54:24 -0400 Subject: test --- learntmux.html.markdown | 200 ---------------------------------------- tmux.html.markdown | 241 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 200 deletions(-) delete mode 100644 learntmux.html.markdown create mode 100644 tmux.html.markdown diff --git a/learntmux.html.markdown b/learntmux.html.markdown deleted file mode 100644 index 65ead04f..00000000 --- a/learntmux.html.markdown +++ /dev/null @@ -1,200 +0,0 @@ ---- -category: tool -tool: tmux -contributors: - - ["kaernyk", "http://github.com/kaernyk"] -filename: LearnTmux.txt ---- - -```bash -# tmux is a terminal multiplexer: it enables a number of terminals to be -# created, accessed, and controlled from a single screen. tmux may be detached -# from a screen and continue running in the background, then later reattached. - -# Once you feel comfortable manipulating tmux to suit your needs, I strongly -# suggest you read the man pages, as there are TONS of features I’m not going -# to go over. - - -# We’ll start of with managing tmux from a terminal, then introduce you to the -# keybinds: - - tmux | tmux new # Create an unamed session - -s "Session" # Create named session - -n "Window" # Create named Window - -c "/dir" # Start in target directory - - tmux attach # Attach last/available session - -t "#" # Attach target session - -d # Detach the session from other instances - - tmux ls # List open sessions - - kill-session # Kill current session - -t "#" # Kill target session - -a # Kill all sessions - -a -t "#" # Kill all sessions but the target - - - -## Key Bindings - -# The method of controlling an attached tmux session is fairly simple, but -# there are two keys we will see repeated throughout this write-up (as well as -# the man pages). - - (C-b) == Ctrl + b THEN ? to list all keybinds - -# The default keybinds are as follows: - - C-b # Send the prefix key (C-b) through to the application. - C-o # Rotate the panes in the current window forwards. - C-z # Suspend the tmux client. - ? # List all key bindings. - : # Enter the tmux command prompt. - r # Force redraw of the attached client. - - c # Create a new window. - $ # Rename the current session. - , # Rename the current window. - - ! # Break the current pane out of the window. - % # Split the current pane into two, left and right. - " # Split the current pane into two, top and bottom. - - n # Change to the next window. - p # Change to the previous window. - { # Swap the current pane with the previous pane. - } # Swap the current pane with the next pane. - - s # Select a new session for the attached client interactively. - w # Choose the current window interactively. - 0 to 9 # Select windows 0 to 9. - - d # Detach the current client. - D # Choose a client to detach. - - & # Kill the current window. - x # Kill the current pane. - - Up, Down # Change to the pane above, below, left, or right. - Left, Right - - M-1 to M-5 # Arrange panes: - # 1) even-horizontal - # 2) even-vertical - # 3) main-horizontal - # 4) main-vertical - # 5) tiled. - - C-Up, C-Down # Resize the current pane in steps of one cell. - C-Left, C-Right - - M-Up, M-Down # Resize the current pane in steps of five cells. - M-Left, M-Right -``` - - -### Configuring ~/.tmux.conf - - tmux.conf can be used to set options automatically on start up, much -like how .vimrc or init.el are used. - -```bash -# Example tmux.conf -# http://dev.gentoo.org/~wired/conf/tmux.conf -# http://wiki.gentoo.org/wiki/Tmux -# https://wiki.archlinux.org/index.php/Tmux - -### Keybinds -########################################################################### - -## Prefix Adjustment -unbind-key C-b -set-option -g prefix ` -#set option -g prefix C-a - -bind-key C-a last-window -bind-key ` last-window -bind-key a send-prefix -bind-key F11 set-option -g prefix C-a -bind-key F12 set-option -g prefix ` - -## Index Start -set -g base-index 1 - -## Window Cycle/Swap -bind e previous-window -bind f next-window -bind E swap-window -t -1 -bind F swap-window -t +1 - - -### Theme -########################################################################### - -## Statusbar Color Palatte -#set-option -g status-justify centre -set-option -g status-justify left -set-option -g status-bg black -set-option -g status-fg white -set-option -g status-left-length 40 -set-option -g status-right-length 80 - - - -## Pane Border Color Palette -set-option -g pane-active-border-fg green -set-option -g pane-active-border-bg black -set-option -g pane-border-fg white -set-option -g pane-border-bg black - - -## Message Color Palette -set-option -g message-fg black -set-option -g message-bg green - - -#setw -g mode-bg black - - -## Window Status Color Palette -setw -g window-status-bg black -setw -g window-status-current-fg green -setw -g window-status-bell-attr default -setw -g window-status-bell-fg red -setw -g window-status-content-attr default -setw -g window-status-content-fg yellow -setw -g window-status-activity-attr default -setw -g window-status-activity-fg yellow - - -### UI -########################################################################### - -## Window Interface Adjustments -set-option -g status-utf8 on -setw -g mode-keys vi -setw -g mode-mouse on -setw -g monitor-activity on - -set-option -g mouse-select-pane on -set-option -g status-keys vi -set-option -g bell-action any -set-option -g set-titles on -set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) -set-option -g visual-bell off - - -## Statusbar Adjustments -set -g status-left ' #[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' -set -g status-interval 5 -#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' -set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' - - -### Misc -########################################################################### -set -g history-limit 4096 -bind r source-file ~/.tmux.conf -``` diff --git a/tmux.html.markdown b/tmux.html.markdown new file mode 100644 index 00000000..516bee4d --- /dev/null +++ b/tmux.html.markdown @@ -0,0 +1,241 @@ +--- +category: tool +tool: tmux +contributors: + - ["kaernyk", "http://github.com/kaernyk"] +filename: LearnTmux.txt +--- + + + +tmux is a terminal multiplexer: it enables a number of terminals to be +created, accessed, and controlled from a single screen. tmux may be detached +from a screen and continue running in the background, then later reattached. + + +``` + tmux [command] # Run a command + # 'tmux' with no commands will create a new session + + new # Create a new session + -s "Session" # Create named session + -n "Window" # Create named Window + -c "/dir" # Start in target directory + + attach # Attach last/available session + -t "#" # Attach target session + -d # Detach the session from other instances + + ls # List open sessions + -a # List all open sessions + + lsw # List windows + -a # List all windows + -s # List all windows in session + + lsp # List panes + -a # List all panes + -s # List all panes in session + -t # List app panes in target + + kill-window # Kill current window + -t "#" # Kill target window + -a # Kill all windows + -a -t "#" # Kill all windows but the target + + kill-session # Kill current session + -t "#" # Kill target session + -a # Kill all sessions + -a -t "#" # Kill all sessions but the target + + + +## Key Bindings + +# The method of controlling an attached tmux session is via key combinations +# called 'Prefix' keys. + +------------------------------------------------------------------------------ + + (C-b) = Ctrl + b # 'Prefix' combination required to use keybinds + + (M-1) = Meta + 1 -or- Alt + 1 + +------------------------------------------------------------------------------ + + ? # List all key bindings. + : # Enter the tmux command prompt. + r # Force redraw of the attached client. + c # Create a new window. + + ! # Break the current pane out of the window. + % # Split the current pane into two, left and right. + " # Split the current pane into two, top and bottom. + + n # Change to the next window. + p # Change to the previous window. + { # Swap the current pane with the previous pane. + } # Swap the current pane with the next pane. + + s # Select a new session for the attached client interactively. + w # Choose the current window interactively. + 0 to 9 # Select windows 0 to 9. + + d # Detach the current client. + D # Choose a client to detach. + + & # Kill the current window. + x # Kill the current pane. + + Up, Down # Change to the pane above, below, left, or right. + Left, Right + + M-1 to M-5 # Arrange panes: + # 1) even-horizontal + # 2) even-vertical + # 3) main-horizontal + # 4) main-vertical + # 5) tiled. + + C-Up, C-Down # Resize the current pane in steps of one cell. + C-Left, C-Right + + M-Up, M-Down # Resize the current pane in steps of five cells. + M-Left, M-Right + + + +### Configuring ~/.tmux.conf + + tmux.conf can be used to set options automatically on start up, much +like how .vimrc or init.el are used. + +# Example tmux.conf +# 2014.9 + + +### Keybinds +########################################################################### + +# Unbind C-b as the default prefix +unbind-key C-befix C-a + +# Return to previous window when prefix is pressed twice +bind-key C-a last-window +bind-key ` last-window + +# Allow swapping C-a and ` using F11/F12 +bind-key F11 set-option -g prefix C-a +bind-key F12 set-option -g prefix ` + +# Activate inner-most session (when nesting tmux) +# to send commands +bind-key a send-prefix + +# Index Start +set -g base-index 1 + +# Window Cycle/Swap +bind e previous-window +bind f next-window +bind E swap-window -t -1 +bind F swap-window -t +1 + +# easy-to-remember split pane commands +bind | split-window -h +bind - split-window -v +unbind '"' +unbind % + +# moving between panes with vim movement keys +bind h select-pane -L +bind j select-pane -D +bind k select-pane -U +bind l select-pane -R + + +### Theme +########################################################################### + +# Statusbar Color Palette +set-option -g status-justify left +set-option -g status-bg black +set-option -g status-fg white +set-option -g status-left-length 40 +set-option -g status-right-length 80 + +# Pane Border Color Palette +set-option -g pane-active-border-fg green +set-option -g pane-active-border-bg black +set-option -g pane-border-fg white +set-option -g pane-border-bg black + +# Message Color Palette +set-option -g message-fg black +set-option -g message-bg green + +# Window Status Color Palette +setw -g window-status-bg black +setw -g window-status-current-fg green +setw -g window-status-bell-attr default +setw -g window-status-bell-fg red +setw -g window-status-content-attr default +setw -g window-status-content-fg yellow +setw -g window-status-activity-attr default +setw -g window-status-activity-fg yellow + + +### UI +########################################################################### + +# Statusbar +set-option -g status-utf8 on + +# Keybind preference +setw -g mode-keys vi +set-option -g status-keys vi + +# Notification +setw -g monitor-activity on +set -g visual-activity on +set-option -g bell-action any +set-option -g visual-bell off + +# Mouse +setw -g mode-mouse on +set-option -g mouse-select-pane on +set -g mouse-resize-pane on +set -g mouse-select-window on + +# Automatically set window titles +set-option -g set-titles on +set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) + +# Statusbar Adjustments +set -g status-left '#[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' +set -g status-interval 3 + +# Statusbar with right-aligned Date / Time +#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' + +# Show performance counters in statusbar +# Requires https://github.com/thewtex/tmux-mem-cpu-load/ +#set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' + + +### Misc +########################################################################### + +# Scrollback/History limit +set -g history-limit 4096 + +bind r source-file ~/.tmux.conf +``` + +### External Resources + +Tmux | Home
+Tmux Manual page
+Archlinux Wiki
+Gentoo Wiki
+Display CPU/MEM % in statusbar
-- cgit v1.2.3 From 870289faa8083b496ad539d531529a64b66bf434 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 10 Sep 2014 20:17:03 +0700 Subject: Add OCaml tutorial. --- ocaml.html.markdown | 309 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 ocaml.html.markdown diff --git a/ocaml.html.markdown b/ocaml.html.markdown new file mode 100644 index 00000000..ae862f08 --- /dev/null +++ b/ocaml.html.markdown @@ -0,0 +1,309 @@ +--- +language: "OCaml" +contributors: + - ["Daniil Baturin", "http://baturin.org/"] +--- + +OCaml is a strictly evaluated functional language with some imperative +features. + +Along with StandardML and its dialects it belongs to ML language family. +Just like StandardML, there are both a compiler and an interpreter +for OCaml. The interpreter binary is normally called "ocaml" and +the compiler is "ocamlc.opt". There is also a bytecode compiler, "ocamlc", +but there are few reasons to use it. + +It is strongly and statically typed, but instead of using manually written +type annotations it infers types of expressions using Hindley-Milner algorithm. +It makes type annotations unnecessary in most cases, but can be a major +source of confusion for beginners. + +When you are in the top level loop, OCaml will print the inferred type +after you enter an expression. +``` +# let inc x = x + 1 ;; +val inc : int -> int = +# let a = 99 ;; +val a : int = 99 +``` + +For a source file you can use "ocamlc -i /path/to/file.ml" command +to print all names and signatures. + +``` +$ cat sigtest.ml +let inc x = x + 1 +let add x y = x + y + +let a = 1 + +$ ocamlc -i ./sigtest.ml +val inc : int -> int +val add : int -> int -> int +val a : int +``` +Note that type signatures of functions of multiple arguments are +written in curried form. + +```ocaml +(* Comments are enclosed in (* and *). It's fine to nest comments. *) + +(* There are no single-line comments *) + + +(** Variables and functions *) + +(* Statements can be separated by double + semicolon symbol, ";;". + In many cases it's redundant, but in this tutorial we use it after + every expression for easy pasting into the interpreter shell. *) + +(* Variable and function declarations use "let" keyword. *) +let x = 10 ;; + +(* Since OCaml uses type inference, you normally don't need to + specify argument types explicitly. However, you can do it + if you want or need to. *) +let inc_int (x: int) = x + 1 ;; + +(* You need to mark recursive function definitions as such with "rec" keyword. *) +let rec factorial n = + if n = 0 then 1 + else factorial n * factorial (n-1) +;; + +(* Function application usually doesn't need parantheses around arguments *) +let fact_5 = factorial 5 ;; + +(* ...unless the argument is an expression *) +let fact_4 = factorial (5-1) ;; +let sqr2 = sqr (-2) ;; + +(* You can use multiple statements separated by semicolon in function body, + but the last expression becomes its return value. This is useful when + writing in imperative style. The simplest form of it is inserting a + debug print. *) +let print_and_return x = + print_endline (string_of_int x); + x +;; + +(* Every function must have at least one argument. + Since some funcions naturally don't take any arguments, there's + "unit" type for it that has the only one value written as "()" *) +let print_hello () = print_endline "hello world" ;; + +(* Note that you must specify "()" as argument when calling it. *) +print_hello () ;; + +(* Calling a function with insufficient number of arguments + does not cause an error, it produces a new function. *) +let make_inc x y = x + y ;; (* make_inc is int -> int -> int *) +let inc_2 = make_inc 2 ;; (* inc_2 is int -> int *) +inc_2 3 ;; (* Evaluates to 5 *) + + +(* Since OCaml is a functional language, it lacks "procedures". + Every function must return something. So functions that + do not really return anything and are called solely for their + side effects, like print_endline, return value of "unit" type. *) + + +(* Definitions can be chained with "let ... in" construct. + This is roughly the same to assigning values to multiple + variables before using them in expressions in imperative + languages. *) +let x = 10 in +let y = 20 in +x + y ;; + +(* Alternatively you can use "let ... in and ..." construct. + This is especially useful for mutually recursive functions, + with ordinary "let .. in" the compiler will complain about + unbound values. + It's hard to come up with a meaningful but self-contained + example of mutually recursive functions, but that syntax + works for non-recursive definitions too. *) +let a = 3 and b = 4 in a * b ;; + + +(** Operators **) + +(* There is little distintion between operators and functions. + Every operator can be called as a function. *) + +(+) 3 4 (* Same as 3 + 4 *) + +(* There's a number of built-in operators. One of unusual features is + that OCaml doesn't just refrain from any implicit conversions + between integers and floats, it also uses different operators + for floats. *) +12 + 3 ;; (* Integer addition *) +12.0 +. 3.0 ;; (* Floating point addition *) + +12 / 3 ;; (* Integer division *) +12.0 /. 3.0 ;; (* Floating point division *) +5 mod 2 ;; (* Remainder *) + +(* Unary minus is a notable exception, it's polymorphic. + However, it also has "pure" integer and float forms. *) +- 3 ;; (* Polymorphic, integer *) +- 4.5 ;; (* Polymorphicm float *) +~- 3 (* Integer only *) +~- 3.4 (* Type error *) +~-. 3.4 (* Float only *) + +(* You can define your own operators or redefine existing ones. + Unlike SML or Haskell, only selected symbols can be used + for operator names and first symbol defines associativity + and precedence rules. *) +let (+) a b = a - b ;; (* Surprise maintenance programmers *) + +(* More useful: a reciprocal operator for floats. + Unary operators must start with "~" *) +let (~/) x = 1.0 /. x ;; +~/4.0 (* = 0.25 *) + + +(** Built-in datastructures *) + +(* Lists are enclosed in square brackets, items are separated by + semicolons. *) +let my_list = [1; 2; 3] ;; + +(* Tuples are (optionally) enclosed in parantheses, items are separated + by commas *) +let first_tuple = 3, 4 ;; +let second_tuple = (4, 5) ;; + +(* Corollary: if you try to separate list items by commas, you get a list + with a tuple inside, probably not what you want. *) +let bad_list = [1, 2] ;; (* Becomes [(1, 2)] *) + +(* You can access individual list items with List.nth function *) +List.nth my_list 1 ;; + +(* You can add an item to the beginning of a list with "::" constructor + often referred to as "cons". *) +1 :: [2; 3] ;; (* Gives [1; 2; 3] *) + +(* Arrays are enclosed in [| |] *) +let my_array = [| 1; 2; 3 |] ;; + +(* You can access array items like this: *) +my_array.(0) ;; + + + +(** Data types *) + +(* You can define types with "type some_type =" construct. Like in this + useless type alias: *) +type my_int = int ;; + +(* More interesting types include so called type constructors. + Constructors must start with a capital letter. *) +type ml = OCaml | StandardML ;; +let lang = OCaml ;; (* Has type "ml" *) + +(* Type constructors don't need to be empty. *) +type my_number = PlusInfinity | MinusInfinity | Real of float ;; +let r0 = Real -3.4 ;; (* Has type "my_number" *) + +(* Can be used to implement polymorphic arithmetics. *) +type number = Int of int | Float of float ;; + +(* Point on a plane, essentially a type-constrained tuple *) +type point2d = Point of float * float ;; +let my_point = Point (2.0, 3.0) ;; + +(* Types can be parametrized, like in this type for "list of lists + of anything". 'a can be substituted with any type. *) +type 'a list_of_lists = 'a list list ;; +type int_list_list = int list_of_lists ;; + +(* Types also can be recursive. Like in this type analogous to + built-in list of integers. *) +type my_int_list = EmptyList | IntList of int * my_int_list ;; +let l = Cons (1, EmptyList) ;; + + +(** Pattern matching *) + +(* Pattern matching is somewhat similar to switch statement in imperative + languages, but offers a lot more expressive power. + + Even though it may look complicated, it really boils down to matching + an argument against an exact value, a predicate, or a type constructor. The type system + is what makes it so powerful. *) + +(* Matching exact values. "_" means "anything" *) +let is_zero x = + match x with + | 0 -> true + | _ -> false +;; + +(* Alternatively, you can use "function" keyword *) +let is_one x = function +| 1 -> true +| _ -> false +;; + +(* Matching predicates, aka "guarded pattern matching" *) +let abs x = + match x with + | x when x < 0 -> -x + | _ -> x +;; + +abs 5 ;; (* 5 *) +abs (-5) (* 5 again *) + +(* Matching type constructors *) + +type animal = Dog of string | Cat of string ;; + +let say x = + match x with + | Dog x -> x ^ " says woof" + | Cat x -> x ^ " says meow" +;; + +say (Cat "Fluffy") ;; (* "Fluffy says meow" *) + +(* Traversing data structures *) + +(* Recursive types can be traversed with pattern matching easily. + The cons thing ("::") that is used with built-in lists is actually a + type constructor, except it can be used in infix form, unlike + user-defined constructors. So you can use it like this: *) + +let rec sum_list l = + match l with + | [] -> 0 + | head :: tail -> head + (sum_list tail) +;; + +sum_list [1; 2; 3] ;; + +(* Built-int syntax for cons obscures the structure a bit, so we'll make + our own list for demonstration. *) + +type int_list = Nil | Cons of int * int_list ;; +let rec sum_int_list l = + match l with + | Nil -> 0 + | Cons (head, tail) -> head + (sum_int_list tail) +;; + +let t = Cons (1, Cons (2, Cons (3, Nil))) ;; +sum_int_list t ;; + +``` + +## Further reading + +* Visit the official website to get the compiler and read the docs: http://ocaml.org/ +* Try interactive tutorials and a web-based interpreter by OCaml Pro: http://try.ocamlpro.com/ +* Read "OCaml for the skeptical" course: http://www2.lib.uchicago.edu/keith/ocaml-class/home.html -- cgit v1.2.3 From 9f3bff9ce85ea95073f8a90397195c841d9fce23 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 10 Sep 2014 21:33:41 +0700 Subject: Fix some typos/errors in the OCaml tutorial. --- ocaml.html.markdown | 112 ++++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/ocaml.html.markdown b/ocaml.html.markdown index ae862f08..3c9593d4 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -14,7 +14,7 @@ the compiler is "ocamlc.opt". There is also a bytecode compiler, "ocamlc", but there are few reasons to use it. It is strongly and statically typed, but instead of using manually written -type annotations it infers types of expressions using Hindley-Milner algorithm. +type annotations, it infers types of expressions using Hindley-Milner algorithm. It makes type annotations unnecessary in most cases, but can be a major source of confusion for beginners. @@ -46,24 +46,24 @@ Note that type signatures of functions of multiple arguments are written in curried form. ```ocaml +(*** Comments ***) + (* Comments are enclosed in (* and *). It's fine to nest comments. *) -(* There are no single-line comments *) +(* There are no single-line comments. *) -(** Variables and functions *) +(*** Variables and functions ***) -(* Statements can be separated by double - semicolon symbol, ";;". +(* Expressions can be separated by a double semicolon symbol, ";;". In many cases it's redundant, but in this tutorial we use it after every expression for easy pasting into the interpreter shell. *) (* Variable and function declarations use "let" keyword. *) let x = 10 ;; -(* Since OCaml uses type inference, you normally don't need to - specify argument types explicitly. However, you can do it - if you want or need to. *) +(* Since OCaml compiler infers types automatically, you normally don't need to + specify argument types explicitly. However, you can do it if you want or need to. *) let inc_int (x: int) = x + 1 ;; (* You need to mark recursive function definitions as such with "rec" keyword. *) @@ -72,22 +72,13 @@ let rec factorial n = else factorial n * factorial (n-1) ;; -(* Function application usually doesn't need parantheses around arguments *) +(* Function application usually doesn't need parentheses around arguments *) let fact_5 = factorial 5 ;; -(* ...unless the argument is an expression *) +(* ...unless the argument is an expression. *) let fact_4 = factorial (5-1) ;; let sqr2 = sqr (-2) ;; -(* You can use multiple statements separated by semicolon in function body, - but the last expression becomes its return value. This is useful when - writing in imperative style. The simplest form of it is inserting a - debug print. *) -let print_and_return x = - print_endline (string_of_int x); - x -;; - (* Every function must have at least one argument. Since some funcions naturally don't take any arguments, there's "unit" type for it that has the only one value written as "()" *) @@ -102,6 +93,15 @@ let make_inc x y = x + y ;; (* make_inc is int -> int -> int *) let inc_2 = make_inc 2 ;; (* inc_2 is int -> int *) inc_2 3 ;; (* Evaluates to 5 *) +(* You can use multiple expressions in function body. + The last expression becomes the return value. All other + expressions must be of the "unit" type. + This is useful when writing in imperative style, the simplest + form of it is inserting a debug print. *) +let print_and_return x = + print_endline (string_of_int x); + x +;; (* Since OCaml is a functional language, it lacks "procedures". Every function must return something. So functions that @@ -117,7 +117,7 @@ let x = 10 in let y = 20 in x + y ;; -(* Alternatively you can use "let ... in and ..." construct. +(* Alternatively you can use "let ... and ... in" construct. This is especially useful for mutually recursive functions, with ordinary "let .. in" the compiler will complain about unbound values. @@ -127,28 +127,28 @@ x + y ;; let a = 3 and b = 4 in a * b ;; -(** Operators **) +(*** Operators ***) (* There is little distintion between operators and functions. Every operator can be called as a function. *) (+) 3 4 (* Same as 3 + 4 *) -(* There's a number of built-in operators. One of unusual features is +(* There's a number of built-in operators. One unusual feature is that OCaml doesn't just refrain from any implicit conversions between integers and floats, it also uses different operators for floats. *) -12 + 3 ;; (* Integer addition *) -12.0 +. 3.0 ;; (* Floating point addition *) +12 + 3 ;; (* Integer addition. *) +12.0 +. 3.0 ;; (* Floating point addition. *) -12 / 3 ;; (* Integer division *) -12.0 /. 3.0 ;; (* Floating point division *) -5 mod 2 ;; (* Remainder *) +12 / 3 ;; (* Integer division. *) +12.0 /. 3.0 ;; (* Floating point division. *) +5 mod 2 ;; (* Remainder. *) (* Unary minus is a notable exception, it's polymorphic. However, it also has "pure" integer and float forms. *) - 3 ;; (* Polymorphic, integer *) -- 4.5 ;; (* Polymorphicm float *) +- 4.5 ;; (* Polymorphic, float *) ~- 3 (* Integer only *) ~- 3.4 (* Type error *) ~-. 3.4 (* Float only *) @@ -156,34 +156,34 @@ let a = 3 and b = 4 in a * b ;; (* You can define your own operators or redefine existing ones. Unlike SML or Haskell, only selected symbols can be used for operator names and first symbol defines associativity - and precedence rules. *) -let (+) a b = a - b ;; (* Surprise maintenance programmers *) + and precedence rules. *) +let (+) a b = a - b ;; (* Surprise maintenance programmers. *) (* More useful: a reciprocal operator for floats. - Unary operators must start with "~" *) + Unary operators must start with "~". *) let (~/) x = 1.0 /. x ;; ~/4.0 (* = 0.25 *) -(** Built-in datastructures *) +(*** Built-in datastructures ***) (* Lists are enclosed in square brackets, items are separated by semicolons. *) let my_list = [1; 2; 3] ;; -(* Tuples are (optionally) enclosed in parantheses, items are separated - by commas *) -let first_tuple = 3, 4 ;; +(* Tuples are (optionally) enclosed in parentheses, items are separated + by commas. *) +let first_tuple = 3, 4 ;; (* Has type "int * int". *) let second_tuple = (4, 5) ;; (* Corollary: if you try to separate list items by commas, you get a list with a tuple inside, probably not what you want. *) let bad_list = [1, 2] ;; (* Becomes [(1, 2)] *) -(* You can access individual list items with List.nth function *) +(* You can access individual list items with the List.nth function. *) List.nth my_list 1 ;; -(* You can add an item to the beginning of a list with "::" constructor +(* You can add an item to the beginning of a list with the "::" constructor often referred to as "cons". *) 1 :: [2; 3] ;; (* Gives [1; 2; 3] *) @@ -195,20 +195,20 @@ my_array.(0) ;; -(** Data types *) +(*** User-defined data types ***) -(* You can define types with "type some_type =" construct. Like in this +(* You can define types with the "type some_type =" construct. Like in this useless type alias: *) type my_int = int ;; (* More interesting types include so called type constructors. Constructors must start with a capital letter. *) type ml = OCaml | StandardML ;; -let lang = OCaml ;; (* Has type "ml" *) +let lang = OCaml ;; (* Has type "ml". *) (* Type constructors don't need to be empty. *) type my_number = PlusInfinity | MinusInfinity | Real of float ;; -let r0 = Real -3.4 ;; (* Has type "my_number" *) +let r0 = Real (-3.4) ;; (* Has type "my_number". *) (* Can be used to implement polymorphic arithmetics. *) type number = Int of int | Float of float ;; @@ -222,13 +222,13 @@ let my_point = Point (2.0, 3.0) ;; type 'a list_of_lists = 'a list list ;; type int_list_list = int list_of_lists ;; -(* Types also can be recursive. Like in this type analogous to +(* Types can also be recursive. Like in this type analogous to built-in list of integers. *) type my_int_list = EmptyList | IntList of int * my_int_list ;; let l = Cons (1, EmptyList) ;; -(** Pattern matching *) +(*** Pattern matching ***) (* Pattern matching is somewhat similar to switch statement in imperative languages, but offers a lot more expressive power. @@ -237,20 +237,21 @@ let l = Cons (1, EmptyList) ;; an argument against an exact value, a predicate, or a type constructor. The type system is what makes it so powerful. *) -(* Matching exact values. "_" means "anything" *) +(** Matching exact values. **) + let is_zero x = match x with | 0 -> true - | _ -> false + | _ -> false (* The "_" pattern means "anything else". *) ;; -(* Alternatively, you can use "function" keyword *) +(* Alternatively, you can use the "function" keyword. *) let is_one x = function | 1 -> true | _ -> false ;; -(* Matching predicates, aka "guarded pattern matching" *) +(* Matching predicates, aka "guarded pattern matching". *) let abs x = match x with | x when x < 0 -> -x @@ -260,7 +261,7 @@ let abs x = abs 5 ;; (* 5 *) abs (-5) (* 5 again *) -(* Matching type constructors *) +(** Matching type constructors **) type animal = Dog of string | Cat of string ;; @@ -270,24 +271,23 @@ let say x = | Cat x -> x ^ " says meow" ;; -say (Cat "Fluffy") ;; (* "Fluffy says meow" *) +say (Cat "Fluffy") ;; (* "Fluffy says meow". *) -(* Traversing data structures *) +(** Traversing datastructures with pattern matching **) (* Recursive types can be traversed with pattern matching easily. - The cons thing ("::") that is used with built-in lists is actually a - type constructor, except it can be used in infix form, unlike - user-defined constructors. So you can use it like this: *) - + Let's see how we can traverse a datastructure of the built-in list type. + Even though the built-in cons ("::") looks like an infix operator, it's actually + a type constructor and can be matched like any other. *) let rec sum_list l = match l with | [] -> 0 | head :: tail -> head + (sum_list tail) ;; -sum_list [1; 2; 3] ;; +sum_list [1; 2; 3] ;; (* Evaluates to 6 *) -(* Built-int syntax for cons obscures the structure a bit, so we'll make +(* Built-in syntax for cons obscures the structure a bit, so we'll make our own list for demonstration. *) type int_list = Nil | Cons of int * int_list ;; -- cgit v1.2.3 From b2cf2228b3843a2015b3badbb550faf47fcb0404 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 10 Sep 2014 21:40:47 +0700 Subject: One more fix in the OCaml tutorial. --- ocaml.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocaml.html.markdown b/ocaml.html.markdown index 3c9593d4..fd7ca36e 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -217,7 +217,7 @@ type number = Int of int | Float of float ;; type point2d = Point of float * float ;; let my_point = Point (2.0, 3.0) ;; -(* Types can be parametrized, like in this type for "list of lists +(* Types can be parameterized, like in this type for "list of lists of anything". 'a can be substituted with any type. *) type 'a list_of_lists = 'a list list ;; type int_list_list = int list_of_lists ;; -- cgit v1.2.3 From 146e6a4ac13f0ff6353879e7b42dfaa2a6725011 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Wed, 10 Sep 2014 17:35:30 +0200 Subject: Update ocaml.html.markdown --- ocaml.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ocaml.html.markdown b/ocaml.html.markdown index fd7ca36e..3dbf5a10 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -1,5 +1,5 @@ --- -language: "OCaml" +language: OCaml contributors: - ["Daniil Baturin", "http://baturin.org/"] --- @@ -20,6 +20,7 @@ source of confusion for beginners. When you are in the top level loop, OCaml will print the inferred type after you enter an expression. + ``` # let inc x = x + 1 ;; val inc : int -> int = @@ -42,6 +43,7 @@ val inc : int -> int val add : int -> int -> int val a : int ``` + Note that type signatures of functions of multiple arguments are written in curried form. -- cgit v1.2.3 From 1d5533f45b4b123920e633064b23332039335f7e Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Wed, 10 Sep 2014 17:37:17 +0200 Subject: Fix up links. --- ocaml.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ocaml.html.markdown b/ocaml.html.markdown index 3dbf5a10..c104823a 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -306,6 +306,6 @@ sum_int_list t ;; ## Further reading -* Visit the official website to get the compiler and read the docs: http://ocaml.org/ -* Try interactive tutorials and a web-based interpreter by OCaml Pro: http://try.ocamlpro.com/ -* Read "OCaml for the skeptical" course: http://www2.lib.uchicago.edu/keith/ocaml-class/home.html +* Visit the official website to get the compiler and read the docs: +* Try interactive tutorials and a web-based interpreter by OCaml Pro: +* Read "OCaml for the skeptical" course: -- cgit v1.2.3 From c9f6a959307ee4e7766456f015c0ce1482a34e8a Mon Sep 17 00:00:00 2001 From: m90 Date: Wed, 10 Sep 2014 18:07:19 +0200 Subject: clarify availability of strikethrough text and fix false assumptions about alt attributes --- markdown.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/markdown.html.markdown b/markdown.html.markdown index 805255b8..3d4d0af6 100644 --- a/markdown.html.markdown +++ b/markdown.html.markdown @@ -39,7 +39,7 @@ This is an h2 ------------- - + *This text is in italics.* _And so is this text._ @@ -52,7 +52,7 @@ __And so is this text.__ *__And this!__* +Github, we also have strikethrough: --> ~~This text is rendered with strikethrough.~~ @@ -201,11 +201,11 @@ can be anything so long as they are unique. --> -![This is hover-text (alt text) for my image](http://imgur.com/myimage.jpg "An optional title") +![This is the alt-attribute for my image](http://imgur.com/myimage.jpg "An optional title") -![This is the hover-text.][myimage] +![This is the alt-attribute.][myimage] [myimage]: relative/urls/cool/image.jpg "if you need a title, it's here" -- cgit v1.2.3 From 9e76a70a6edd5ca3c33213730acdb795881ea422 Mon Sep 17 00:00:00 2001 From: kaernyk Date: Wed, 10 Sep 2014 12:42:05 -0400 Subject: Improve formatting I noticed that quite a bit of the text was not wrapped appropriately and was ruining the display. I have since reduced all lines possible to wrap at/before 72 columns --- tmux.html.markdown | 81 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/tmux.html.markdown b/tmux.html.markdown index 516bee4d..8d7aa752 100644 --- a/tmux.html.markdown +++ b/tmux.html.markdown @@ -8,14 +8,16 @@ filename: LearnTmux.txt -tmux is a terminal multiplexer: it enables a number of terminals to be -created, accessed, and controlled from a single screen. tmux may be detached -from a screen and continue running in the background, then later reattached. +tmux is a terminal multiplexer: it enables a number of terminals +to be created, accessed, and controlled from a single screen. tmux +may be detached from a screen and continue running in the background +then later reattached. ``` tmux [command] # Run a command - # 'tmux' with no commands will create a new session + # 'tmux' with no commands will create a new + session new # Create a new session -s "Session" # Create named session @@ -52,42 +54,41 @@ from a screen and continue running in the background, then later reattached. ## Key Bindings -# The method of controlling an attached tmux session is via key combinations -# called 'Prefix' keys. - ------------------------------------------------------------------------------- +# The method of controlling an attached tmux session is via key +# combinations called 'Prefix' keys. +---------------------------------------------------------------------- (C-b) = Ctrl + b # 'Prefix' combination required to use keybinds (M-1) = Meta + 1 -or- Alt + 1 +---------------------------------------------------------------------- ------------------------------------------------------------------------------- - - ? # List all key bindings. - : # Enter the tmux command prompt. - r # Force redraw of the attached client. - c # Create a new window. + ? # List all key bindings + : # Enter the tmux command prompt + r # Force redraw of the attached client + c # Create a new window ! # Break the current pane out of the window. - % # Split the current pane into two, left and right. - " # Split the current pane into two, top and bottom. + % # Split the current pane into two, left and right + " # Split the current pane into two, top and bottom - n # Change to the next window. - p # Change to the previous window. - { # Swap the current pane with the previous pane. - } # Swap the current pane with the next pane. + n # Change to the next window + p # Change to the previous window + { # Swap the current pane with the previous pane + } # Swap the current pane with the next pane - s # Select a new session for the attached client interactively. - w # Choose the current window interactively. - 0 to 9 # Select windows 0 to 9. + s # Select a new session for the attached client + interactively + w # Choose the current window interactively + 0 to 9 # Select windows 0 to 9 - d # Detach the current client. - D # Choose a client to detach. + d # Detach the current client + D # Choose a client to detach - & # Kill the current window. - x # Kill the current pane. + & # Kill the current window + x # Kill the current pane - Up, Down # Change to the pane above, below, left, or right. + Up, Down # Change to the pane above, below, left, or right Left, Right M-1 to M-5 # Arrange panes: @@ -95,12 +96,12 @@ from a screen and continue running in the background, then later reattached. # 2) even-vertical # 3) main-horizontal # 4) main-vertical - # 5) tiled. + # 5) tiled - C-Up, C-Down # Resize the current pane in steps of one cell. + C-Up, C-Down # Resize the current pane in steps of one cell C-Left, C-Right - M-Up, M-Down # Resize the current pane in steps of five cells. + M-Up, M-Down # Resize the current pane in steps of five cells M-Left, M-Right @@ -115,7 +116,7 @@ like how .vimrc or init.el are used. ### Keybinds -########################################################################### +###################################################################### # Unbind C-b as the default prefix unbind-key C-befix C-a @@ -155,7 +156,7 @@ bind l select-pane -R ### Theme -########################################################################### +##################################################################### # Statusbar Color Palette set-option -g status-justify left @@ -186,7 +187,7 @@ setw -g window-status-activity-fg yellow ### UI -########################################################################### +###################################################################### # Statusbar set-option -g status-utf8 on @@ -209,22 +210,24 @@ set -g mouse-select-window on # Automatically set window titles set-option -g set-titles on -set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not) + +# window number,program name,active (or not) +set-option -g set-titles-string '#H:#S.#I.#P #W #T' # Statusbar Adjustments -set -g status-left '#[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default] ' +set -g status-left '#[fg=red]#H#[fg=green]:#[fg=white]#S #[fg=green]][#[default]' set -g status-interval 3 # Statusbar with right-aligned Date / Time -#set -g status-right ' #[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default] ' +#set -g status-right '#[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default]' # Show performance counters in statusbar # Requires https://github.com/thewtex/tmux-mem-cpu-load/ -#set -g status-right ' #[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default] ' +#set -g status-right '#[fg=green]][#[fg=white] #(tmux-mem-cpu-load 5 4) #[fg=green]][ #[fg=yellow]%H:%M#[default]' ### Misc -########################################################################### +###################################################################### # Scrollback/History limit set -g history-limit 4096 -- cgit v1.2.3 From a05547486450bb8918bce322da2c2a8402989ba2 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Wed, 10 Sep 2014 18:04:18 -0400 Subject: Created c++ tutorial c++ --- c++.html.markdown | 349 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 349 insertions(+) create mode 100644 c++.html.markdown diff --git a/c++.html.markdown b/c++.html.markdown new file mode 100644 index 00000000..8cf72e47 --- /dev/null +++ b/c++.html.markdown @@ -0,0 +1,349 @@ +--- +language: c++ +filename: learncpp.cpp +contributors: + - ["Steven Basart", "http://github.com/xksteven"] +lang: en +--- + +I am writing this to highlight the differences and +additions that C++ has with respect to C. My +suggestion would be to follow the C tutorial first +then look here for the additions and differences. + +```c++ +/////////////////////////////////////// +// C++ differences +/////////////////////////////////////// + + +//In C++ +//cannot use void main() +int main() { //or int main(int argc, char **argv) + //cannot end with return; + return 0; + //Can also end without return statement +} + +//In C++ +/* + //This could lead to compiler errors and is discouraged + //#if 0 #endif pairs are encouraged instead +*/ + +//In C++ +sizeof(10) //Typically 4 +sizeof('c') == 1 + +//In C +sizeof('c') == sizeof(10) //true chars are passed as ints + + +//In C++ strict prototyping +void func(); //function which accepts no arguments + +//In C +void func(); //function which may accept arguments + + +//In C++ +for(int i = 0; i < 10; i++) {;} +//In C must int i must be declared before + + +//C++ Supports Function overloading +//Provided each function takes different +//parameters + +void printing(char const *myString) +{printf("String %s\n",myString);} //Hello + +void printing(int myInt) +{printf("My int is %d",myInt);} //15 + +int main () +{ + printing("Hello"); + printing(15); +} + + + +//C++ Default Function Arguments +void two_ints(int a = 1, int b = 4); + +int main() +{ + two_ints(); // arguments: 1, 4 + two_ints(20); // arguments: 20, 4 + two_ints(20, 5); // arguments: 20, 5 +} + + +//C++ added the nullptr which is different from 0 +int *ip = nullptr; // OK +int value = nullptr; // error: value is no pointer + + +/////////////////////////////////////// +// C++ Additions ontop of C +/////////////////////////////////////// + + +/////////////////////////////////////// +// C++ Namespace +/////////////////////////////////////// + +//Namespaces allow you to define your own +//functions and variables for use + +// Use '::' to change variable (or function) scope +// Putting '::' before a function or variable will +// reference a global scope + +// This allows you to make normal c library calls +// std is for standard library +using namespace std; + +#include + +int counter = 50; // global variable + +int main() +{ + for (int counter = 1; // this refers to the + counter < 2; // local variable + counter++) + { + printf("Global var %d local var %d\n", + ::counter, // global variable + counter); // local variable + // => Global var 50 local var 1 + } +} + +// Namespaces can be nested + + +namespace myFirstNameSpace +{ + namespace myInnerSoul + { + cos(int x) + { + printf("My inner soul was made to program."); + } + } +} + +namespace anotherNameSpace +{ + cos(int x) {;} //does nothing +} + +int main() +{ + //Specify the full path because main is outside of both namespaces. + //Will print out My inner soul was made to program. + myFirstNameSpace::myInnerSoul::cos(60); +} + + +/////////////////////////////////////// +// C++ Strings +/////////////////////////////////////// + +//Strings in C++ are Objects and have many functions +myString = "Hello"; +myOtherString = " World"; + +myString + myOtherString; // => "Hello World" + +myString + ' You'; // => "Hello You" + +myString != myOtherString; //True + +//An example of a string method +myString.append(" Dog"); // => "Hello Dog" + + +/////////////////////////////////////// +// C++ Input Output +/////////////////////////////////////// + +//C++ input and output streams +//cin, cout, cerr, << is insertion and >> is extraction operator +#include + +using namespace std; + +int main() +{ + + int myInt; + + //Prints to stdout (or terminal/screen) + cout << "Enter your fav number:\n" + //Takes in input + cin >> myInt; + + //cout can also be formatted + cout << "Your fav number is " << myInt << "\n" + //Your fav number is ## + + cerr << "Used for error messages" +} + + +/////////////////////////////////////// +// C++ Classes +/////////////////////////////////////// + + +//First example of classes +#include + +//define a class +class Doggie +{ + std::string name; + int weight; + + // These are only the declarations + //Can also have private and protected + public: + //The public methods (can also include variables) + + // Default constructor + Doggie(); + + void setName(std::string dogsName); + void setWeight(int dogsWeight); + void printDog(); + + //Can define functions within class declaration too + void dogBark() {std::cout << "Bark Bark\n"} + + //Destructors are methods that free the allocated space + ~doggieDestructor(); + //if no destructor compiler defines the trivial destructor + +//Classes are similar to structs and must close the } with ; +}; + +// This is the implementation of the class methods +// Also called the definition +void Doggie::Doggie () { + std::cout << "A doggie is born. Woof!\n"; +} + +void Doggie::setName (std::string doggie_name) { + name = doggie_name; +} + +void Doggie::setWeight (int doggie_weight) { + weight = doggie_weight; +} + +void Doggie::printDog () { + std::cout << "Dog is " << name << " weighs" << weight << "\n"; +} + +void Doggie::~doggieDestructor () { + delete[] name; + delete weight; +} + +int main () { + Doggie deedee; // prints out a doggie is born. Woof! + deedee.setName ("Barkley"); + deedee.setWeight(1000000); + deedee.printDog; + //prints => Dog is Barkley weighs 1000000 + return 0; +} + + +//C++ Class inheritance + +class German_Sheperd +{ + //This class now inherits everything public and protected from Doggie class + Doggie d_dog; + + //Good practice to put d_ in front of datatypes in classes + std::string d_type; + + public: + void dogType() {d_type = "German Sheperd";} +}; + + + +/////////////////////////////////////// +// C++ Exception Handling +/////////////////////////////////////// + +try { + throw 12.25; // throws a double no handler declared +} catch (int errorNum) +{ + std::cout << "I caught an int " << errorNum << "\n"; +//default catcher +} catch (...) +{ + std::cout << "I got an error. Not sure what but I can pass it up."; + throw; +} + + +/////////////////////////////////////// +// C++ Operator Overloading +/////////////////////////////////////// + +// In C++ you can overload operators such as +, -, new, etc. + +#include +using namespace std; + +class Vector { + public: + double x,y; + Vector () {}; + Vector (double a, double b) : x(a), y(b) {} + Vector operator + (const CVector&); + Vector operator += (const CVector&); +}; + +Vector Vector::operator+ (const Vector& rhs) +{ + Vector temp; + temp.x = x + rhs.x; + temp.y = y + rhs.y; + return temp; +} + +Vector Vector::operator+= (const Vector& rhs) +{ + x += rhs.x; + y += rhs.y; + return *this; +} + +int main () { + Vector up (0,1); + Vector right (1,0); + Vector result; + // This calls the Vector + operator + // Vector up calls the + (function) with right as its paramater + result = up + right; + // prints out => Result is upright (1,1) + cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; + return 0; +} + +``` +Futher Reading + +for more resources see: http://www.icce.rug.nl/documents/cplusplus/ +for other reference material: http://www.cplusplus.com/doc/tutorial/ -- cgit v1.2.3 From faa297157a80f4aeac2681ebd5ba2edc11f07fa7 Mon Sep 17 00:00:00 2001 From: Daniel Hodan Date: Thu, 11 Sep 2014 12:16:25 +0200 Subject: Fix typo in words calculate and area --- erlang.html.markdown | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/erlang.html.markdown b/erlang.html.markdown index 64b62f05..58656982 100644 --- a/erlang.html.markdown +++ b/erlang.html.markdown @@ -260,22 +260,22 @@ spawn(F). % <0.44.0> % For all of this to be useful we need to be able to receive messages. This is % achieved with the `receive` mechanism: --module(caculateGeometry). +-module(calculateGeometry). -compile(export_all). -caculateAera() -> +calculateArea() -> receive {rectangle, W, H} -> W * H; {circle, R} -> 3.14 * R * R; _ -> - io:format("We can only caculate area of rectangles or circles.") + io:format("We can only calculate area of rectangles or circles.") end. -% Compile the module and create a process that evaluates `caculateAera` in the shell -c(caculateGeometry). -CaculateAera = spawn(caculateGeometry, caculateAera, []). -CaculateAera ! {circle, 2}. % 12.56000000000000049738 +% Compile the module and create a process that evaluates `calculateArea` in the shell +c(calculateGeometry). +CalculateArea = spawn(calculateGeometry, calculateAera, []). +CalculateArea ! {circle, 2}. % 12.56000000000000049738 % The shell is also a process, you can use `self` to get the current pid self(). % <0.41.0> -- cgit v1.2.3 From e2885a8ff8017ab727114fb7190b9156e4bbff4c Mon Sep 17 00:00:00 2001 From: Daniel Hodan Date: Thu, 11 Sep 2014 12:20:32 +0200 Subject: Fix typo in words calculate and area, one more --- erlang.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erlang.html.markdown b/erlang.html.markdown index 58656982..04086aeb 100644 --- a/erlang.html.markdown +++ b/erlang.html.markdown @@ -274,7 +274,7 @@ calculateArea() -> % Compile the module and create a process that evaluates `calculateArea` in the shell c(calculateGeometry). -CalculateArea = spawn(calculateGeometry, calculateAera, []). +CalculateArea = spawn(calculateGeometry, calculateArea, []). CalculateArea ! {circle, 2}. % 12.56000000000000049738 % The shell is also a process, you can use `self` to get the current pid -- cgit v1.2.3