# Libtmux_unix.Client.move_window_at

- **Module:** Libtmux_unix
- **Package:** libtmux-unix
- **Language:** OCaml
- **Kind:** function
- **Source:** https://github.com/libtmux/libtmux-ocaml/blob/205dfda49742592b479247049d186956e4a4cfe9/unix/libtmux_unix.mli#L634
- **Public owner:** Libtmux_unix
- **Page:** https://libtmux.org/en/ocaml/latest/reference/libtmux_unix-client-move_window_at/

```
val move_window_at :
  ?cancel:Cancellation.t ->
  t ->
  Libtmux.Session.target ->
  from_index:int ->
  session:Libtmux.Session.target ->
  index:int -> (unit, Libtmux.Error.t) result
```

Move the source session's current link at `from_index`. Destination collisions fail.

## Examples

Move a window between sessions. This complete program creates a private daemon, checks its result and closes its clients. Use OCaml 5.5.2 on Linux with tmux 3.3a through 3.8. Run it from the source checkout after following the installation guide.

[Source example](https://github.com/libtmux/libtmux-ocaml/blob/205dfda49742592b479247049d186956e4a4cfe9/examples/showcase_move_window.ml).

```ocaml
open Libtmux
module Client = Libtmux_unix.Client
module Workspace = Libtmux_unix.Workspace
module Config = Libtmux_workspace.Config
module Plan = Libtmux_workspace.Plan
module Execution = Libtmux_workspace.Execution

let get = function
  | Ok value -> value
  | Error error -> failwith (Format.asprintf "%a" Error.pp error)

let move_window client (initial : Created.t) spawn =
  let moving =
    get (Client.create_window client initial.session ~name:"build" ~spawn)
  in
  let destination =
    get
      (Client.create_session client ~name:"destination" ~window_name:"landing"
         ~spawn ())
  in
  get
    (Client.move_window_at client initial.session
       ~from_index:moving.window_index ~session:destination.session ~index:7);
  let window =
    Snapshot.find_window
      (get (Client.capture client))
      (Window.Target.id moving.window)
    |> Option.get
  in
  let link =
    Query.exactly_one (Iarray.to_seq (Window.links window)) |> Result.get_ok
  in
  assert (Window_link.index link = 7);
  assert (
    Session.Id.equal
      (Session.id (Window_link.session link))
      (Session.Target.id destination.session));
  Printf.printf "moved: %s at %d\n" (Window.name window)
    (Window_link.index link)

let with_demo binary use =
  let directory = Filename.temp_dir "libtmux-ocaml-showcase-" "" in
  Fun.protect
    ~finally:(fun () ->
      Sys.readdir directory
      |> Array.iter (fun name -> Sys.remove (Filename.concat directory name));
      Unix.rmdir directory)
    (fun () ->
      let socket = Filename.concat directory "server" in
      (* Give this private daemon a lifetime even if startup fails. *)
      let config_file = Filename.concat directory "tmux.conf" in
      Out_channel.with_open_text config_file (fun output ->
          output_string output "set-option -s exit-unattached on\n");
      let wrapper = Filename.concat directory "tmux" in
      Out_channel.with_open_text wrapper (fun output ->
          Printf.fprintf output "#!/bin/sh\nexec %s -f %s \"$@\"\n"
            (Filename.quote binary)
            (Filename.quote config_file));
      Unix.chmod wrapper 0o700;
      let channel = Unix.open_process_args_in binary [| binary; "-V" |] in
      let version =
        Fun.protect
          ~finally:(fun () ->
            if Unix.close_process_in channel <> Unix.WEXITED 0 then
              failwith "tmux version discovery failed")
          (fun () -> String.trim (input_line channel))
      in
      if not (String.starts_with ~prefix:"tmux " version) then
        failwith "invalid tmux version";
      let version = String.sub version 5 (String.length version - 5) in
      let ready = Filename.concat directory "ready.txt" in
      Out_channel.with_open_text ready (fun output ->
          output_string output "showcase ready\n");
      let spawn =
        Spawn.make (Exec { program = "/bin/cat"; args = [ "-u"; ready; "-" ] })
      in
      let config =
        match
          Config.make ~directory ~session:"showcase"
            [ Config.window ~name:"editor" [ Config.pane ~spawn () ] ]
        with
        | Ok value -> value
        | Error error -> failwith (Format.asprintf "%a" Config.pp_error error)
      in
      let plan =
        match
          Plan.make
            ~endpoint:(Endpoint.socket_path socket)
            ~version ~observed:None config
        with
        | Ok value -> value
        | Error error -> failwith (Format.asprintf "%a" Plan.pp_error error)
      in
      let operation = get (Workspace.submit_bootstrap ~binary:wrapper plan) in
      Fun.protect
        ~finally:(fun () -> get (Workspace.close operation))
        (fun () ->
          let receipt = get (Workspace.await operation) in
          if receipt.status <> Execution.Succeeded then
            failwith "workspace setup failed";
          let client = Option.get (Workspace.client operation) in
          let initial = List.hd (Iarray.get receipt.steps 0).created in
          Printf.printf "socket=%s\n%!" socket;
          let pane =
            Snapshot.find_pane
              (get (Client.capture client))
              (Pane.Target.id initial.pane)
            |> Option.get
          in
          ignore
            (get
               (Client.wait_for_text ~timeout:0.5 client pane
                  ~text:"showcase ready"));
          use client initial spawn))

let () =
  let binary = if Array.length Sys.argv = 2 then Sys.argv.(1) else "tmux" in
  with_demo binary move_window
```

Run the recorded entry point:

```console
$ opam exec -- dune exec examples/showcase_move_window.exe -- tmux
```

Expected outcome line, after the temporary socket path:

```text
moved: build at 7
```

## Parameters

- `cancel?` (`Cancellation.t`)
- `arg1` (`t`)
- `arg2` (`Libtmux.Session.target`)
- `from_index` (`int`)
- `session` (`Libtmux.Session.target`)
- `index` (`int`)

## tmux command

The tmux commands behind `move_window_at`.

- [`move-window`](/en/tmux/latest/manual/move-window/)

## tmux C API

The C declarations in tmux that implement these commands.

- [`cmd_move_window_entry`](/en/tmux/latest/reference/c-variable-cmd_move_window_entry/) (attribute)
- [`cmd_move_window_exec`](/en/tmux/latest/reference/c-function-cmd-move-window-c-cmd_move_window_exec/) (function)

## In other ports

Move a window to another session or index

| Language | Equivalent |
| --- | --- |
| Python | [`libtmux.Window.move_window`](/en/py/latest/reference/libtmux-window-move_window/) (method) |
| Lua | [`libtmux.WindowLink:move`](/en/lua/latest/reference/libtmux-windowlink-move/) (method) |
| Erlang | [`tmux_window_link:move/3`](/en/erlang/latest/reference/tmux_window_link-move-3/) (function) |
| Haskell | No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing. |
| TypeScript | [`window.Window.move`](/en/ts/latest/reference/window-window-move/) (method) |
| Rust | [`window.Window.move_to`](/en/rs/latest/reference/window-window-move_to/) (method) |
| Go | [`tmux.Window.Move`](/en/go/latest/reference/tmux-window-move/) (method) |
| Java | [`io.github.libtmux.Window.moveTo`](/en/java/latest/reference/io-github-libtmux-window-window-moveto/) (method) |
| C# | [`LibTmux.Window.MoveAsync`](/en/csharp/latest/reference/libtmux-window-moveasync/) (method) |
| C++ | [`libtmux::Window::move_to`](/en/cxx/latest/reference/libtmux-window-move_to/) (method) |
| Swift | [`Server.move(_:to:at:)`](/en/swift/latest/reference/server-move(_-to-at-)/) (method) |
| Perl | [`LibTmux::Server::move_window`](/en/perl/latest/reference/libtmux-server-move_window/) (method). Requires an explicit WindowLink or session_id:index context; a physical window alone does not select a session placement. |

No equivalent is recorded in Ruby, PowerShell, Julia, Elixir, Common Lisp, Clojure, Kotlin, Scala or F#.
